In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to solve the problem of how to save JPA entities with Id and add a new data problem". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Make a deep copy of two different entity classes, then assign the id value back to update, and a new piece of data appears.
A updateA = DTOMapper.MAPPER.po2po (bAudit); updateA.setId (1) sheetRepository.save (updateA) 1.save method source code / * * (non-Javadoc) * @ see org.springframework.data.repository.CrudRepository#save (java.lang.Object) * / @ Transactional public S save (S entity) {if (entityInformation.isNew (entity)) {em.persist (entity); return entity } else {return em.merge (entity);}}
To mention here, spring has added @ Transactional to start the transaction on save. If you use spring to manage a transaction, you must open the transaction, otherwise you can't save it because of SET AUTOCOMMIT=0.
The first line of the save method is to determine whether the entity is new or updated.
two。 Look at the isNew implementation
The first is provided by AbstractPersistable, as we know it, to determine whether it is an update based on whether the id is empty or not.
/ * * Must be {@ link Transient} in order to ensure that no JPA provider complains because of a missing setter. * * @ see org.springframework.data.domain.Persistable#isNew () * / @ Transient / / DATAJPA-622 public boolean isNew () {return null = = getId ();}
The second one is provided by JpaMetamodelEntityInformation.
/ * * (non-Javadoc) * @ see org.springframework.data.repository.core.support.AbstractEntityInformation#isNew (java.lang.Object) * / @ Override public boolean isNew (T entity) {if (! versionAttribute.isPresent () | versionAttribute.map (Attribute::getJavaType) .map (Class::isPrimitive) .orElse (false)) {return super.isNew (entity) } BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper (entity); return versionAttribute.map (it-> wrapper.getPropertyValue (it.getName ()) = = null) .orElse (true);}
This is based on the version version number to determine whether it is an update. This is the problem. If we update, we must update the jpaVersion of the entity is the same as the Version in the database, otherwise it is a new operation.
Summary: I have been in a variety of printing id whether to assign a value, at first it is get from the object, and then assign a value to a variable, and then wonder if jpa is directly taking its own properties, rather than taking the public method (id is inherited by the parent class), anyway, it has been bothering with for a long time.
In fact, it's very simple to think about it, but jpa didn't report me wrong. I think it's jpa's fault, .
This is the end of the content of "how to solve the problem of how to save JPA entity classes with Id and add a new data". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.