eclipselink jpa版本控制,时间戳列在springboot中无法正常工作

wlp8pajw  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(179)

我试图在springboot(版本2.2.1)rest服务的eclipse链接jpa中使用乐观锁定,但即使对于单线程也面临乐观锁定异常,这意味着即使没有一条记录成功保存到数据库中,但相同的代码如果我在simple eclipselink项目而不是springboot项目中运行,也可以正常工作。在这个问题上坚持了一个星期没有任何成功,任何帮助将不胜感激。如果我在代码中使用setversion方法,那么它工作得很好,但是根据jpa,setter方法不应该在有版本的列的代码中使用,或者我的理解是错误的?
实体类

  1. import java.math.BigDecimal;
  2. import java.util.List;
  3. import javax.persistence.CascadeType;
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.FetchType;
  7. import javax.persistence.GeneratedValue;
  8. import javax.persistence.GenerationType;
  9. import javax.persistence.Id;
  10. import javax.persistence.OneToMany;
  11. import javax.persistence.SequenceGenerator;
  12. import javax.persistence.Table;
  13. import com.fasterxml.jackson.annotation.JsonIgnore;
  14. @Entity
  15. @Table(name = "PARENT")
  16. public class Parent extends Audit {
  17. @Id
  18. @SequenceGenerator(name = "Parent_seq_generator", sequenceName = "PARENT_SEQNUM", allocationSize = 1)
  19. @GeneratedValue(strategy=GenerationType.AUTO, generator="Parent_seq_generator")
  20. @JsonIgnore
  21. @Column(name = "PARENT_ID" )
  22. private BigDecimal id;
  23. @Column(name = "PARENT_TYPE" )
  24. private String parentType;
  25. @Column(name = "PARENT_DESC" )
  26. private String parentDesc;
  27. @OneToMany(mappedBy = "lookups", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
  28. private List<Child> childList;
  29. public List<Child> getChildList() {
  30. return childList;
  31. }
  32. public void setChildList(List<Child> childList) {
  33. this.childList = childList;
  34. }
  35. public BigDecimal getId() {
  36. return id;
  37. }
  38. public void setId(BigDecimal id) {
  39. this.id = id;
  40. }
  41. public String getParentType() {
  42. return parentType;
  43. }
  44. public void setParentType(String parentType) {
  45. this.parentType = parentType;
  46. }
  47. public String getLktDesc() {
  48. return lktDesc;
  49. }
  50. public void setLktDesc(String lktDesc) {
  51. this.lktDesc = lktDesc;
  52. }
  53. }
  54. import java.math.BigDecimal;
  55. import javax.persistence.Column;
  56. import javax.persistence.Entity;
  57. import javax.persistence.FetchType;
  58. import javax.persistence.GeneratedValue;
  59. import javax.persistence.GenerationType;
  60. import javax.persistence.Id;
  61. import javax.persistence.JoinColumn;
  62. import javax.persistence.ManyToOne;
  63. import javax.persistence.SequenceGenerator;
  64. import javax.persistence.Table;
  65. import com.fasterxml.jackson.annotation.JsonIgnore;
  66. @Entity
  67. @Table(name = "CHILD")
  68. public class Child extends ChildAudit {
  69. @Id
  70. @SequenceGenerator(name = "Child_seq_generator", sequenceName = "CHILD_SEQNUM", allocationSize = 1)
  71. @GeneratedValue(strategy=GenerationType.AUTO, generator="Child_seq_generator")
  72. @JsonIgnore
  73. @Column(name = "CHILD_ID" )
  74. private BigDecimal id;
  75. @Column(name = "CHILD_CODE" )
  76. private String childCode;
  77. @Column(name = "CHILD_DESC" )
  78. private String childDesc;
  79. @ManyToOne(fetch = FetchType.LAZY)
  80. @JoinColumn(name = "CHILD_PARENT_TYPE", referencedColumnName = "PARENT_TYPE")
  81. private Parent parents;
  82. public Parent getParents() {
  83. return parents;
  84. }
  85. public void setParents(Parent parents) {
  86. this.parents = parents;
  87. }
  88. public BigDecimal getId() {
  89. return id;
  90. }
  91. public void setId(BigDecimal id) {
  92. this.id = id;
  93. }
  94. public String getChildCode() {
  95. return childCode;
  96. }
  97. public void setChildCode(String childCode) {
  98. this.childCode = childCode;
  99. }
  100. public void setChildDesc(String childDesc) {
  101. this.childDesc = childDesc;
  102. }
  103. }
  104. import java.sql.Timestamp;
  105. import java.time.LocalDateTime;
  106. import javax.persistence.Column;
  107. import javax.persistence.MappedSuperclass;
  108. import javax.persistence.PrePersist;
  109. import javax.persistence.PreUpdate;
  110. import javax.persistence.Version;
  111. import org.slf4j.Logger;
  112. import org.slf4j.LoggerFactory;
  113. import com.fasterxml.jackson.annotation.JsonIgnore;
  114. @MappedSuperclass
  115. public class Audit {
  116. @Column(name = "CREATED_BY", nullable = false, length = 30, updatable = false)
  117. @JsonIgnore
  118. private String createdBy;
  119. @Column(name = "CREATION_DATE", updatable = false)
  120. @JsonIgnore
  121. private LocalDateTime createdDate = LocalDateTime.now();
  122. @Column(name = "LAST_UPDATED_BY", length = 30, updatable = true)
  123. @JsonIgnore
  124. private String lastModifiedBy;
  125. @Version
  126. @Column(name = "LAST_UPDATE_DATE")
  127. @JsonIgnore
  128. private Timestamp lastModifiedDate;
  129. public String getCreatedBy() {
  130. return createdBy;
  131. }
  132. public void setCreatedBy(String createdBy) {
  133. this.createdBy = createdBy;
  134. }
  135. public String getLastModifiedBy() {
  136. return lastModifiedBy;
  137. }
  138. public void setLastModifiedBy(String lastModifiedBy) {
  139. if(null == lastModifiedBy) {
  140. this.lastModifiedBy = "TEST";
  141. } else {
  142. this.lastModifiedBy = lastModifiedBy;
  143. }
  144. }
  145. public LocalDateTime getCreatedDate() {
  146. return createdDate;
  147. }
  148. public void setCreatedDate(LocalDateTime createdDate) {
  149. this.createdDate = createdDate;
  150. }
  151. public Timestamp getLastModifiedDate() {
  152. return lastModifiedDate;
  153. }
  154. @PrePersist
  155. @PreUpdate
  156. public void updateLastupdatedBy() {
  157. setLastModifiedBy("TEST");
  158. }
  159. }
  160. import java.time.LocalDateTime;
  161. import javax.persistence.Column;
  162. import javax.persistence.PrePersist;
  163. import javax.persistence.PreUpdate;
  164. import org.slf4j.Logger;
  165. import org.slf4j.LoggerFactory;
  166. import com.fasterxml.jackson.annotation.JsonIgnore;
  167. public class ChildAudit {
  168. @Column(name = "CREATED_BY", nullable = false, length = 30, updatable = false)
  169. @JsonIgnore
  170. private String createdBy;
  171. @Column(name = "CREATION_DATE", updatable = false)
  172. @JsonIgnore
  173. private LocalDateTime createdDate = LocalDateTime.now();
  174. @Column(name = "LAST_UPDATED_BY", length = 30, updatable = true)
  175. @JsonIgnore
  176. private String lastModifiedBy;
  177. @Column(name = "LAST_UPDATE_DATE")
  178. @JsonIgnore
  179. private LocalDateTime lastModifiedDate = LocalDateTime.now();
  180. public String getCreatedBy() {
  181. return createdBy;
  182. }
  183. public void setCreatedBy(String createdBy) {
  184. this.createdBy = createdBy;
  185. }
  186. public String getLastModifiedBy() {
  187. return lastModifiedBy;
  188. }
  189. public void setLastModifiedBy(String lastModifiedBy) {
  190. if(null == lastModifiedBy) {
  191. this.lastModifiedBy = "TEST";
  192. } else {
  193. this.lastModifiedBy = lastModifiedBy;
  194. }
  195. }
  196. public LocalDateTime getCreatedDate() {
  197. return createdDate;
  198. }
  199. public void setCreatedDate(LocalDateTime createdDate) {
  200. this.createdDate = createdDate;
  201. }
  202. public Timestamp getLastModifiedDate() {
  203. return lastModifiedDate;
  204. }
  205. public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
  206. this.lastModifiedDate = lastModifiedDate;
  207. }
  208. @PrePersist
  209. @PreUpdate
  210. public void updateLastupdatedBy() {
  211. setLastModifiedBy("TEST");
  212. }
  213. }

我的数据访问类

  1. import org.springframework.beans.BeanUtils;
  2. import javax.persistence.EntityManager;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.transaction.annotation.Transactional;
  10. @Service
  11. public class DataAccessLayer
  12. {
  13. @Transactional
  14. public void modifyRecord(ParentDto parentDto, String id) {
  15. EntityManager em = new MyEntityFactory().getEntity();
  16. Parent parent = em.find(Parent.class,new BigDecimal(id));
  17. List<Child> childList = new ArrayList<>();
  18. for (ChildDto childDto : ParentDto.getChildDto())
  19. {
  20. Child child = new Child();
  21. BeanUtils.copyProperties(childDto, child);
  22. childList.add(child);
  23. }
  24. parent.setChildList(childList);
  25. }
  26. BeanUtils.copyProperties(parentDto, parent);
  27. parent.setId(new BigDecimal(id));
  28. em.merge(parent);
  29. }
  30. }

持久性.xml

  1. <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  3. version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
  4. <persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL">
  5. <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
  6. <class>Parent</class>
  7. <class>Child</class>
  8. <exclude-unlisted-classes>false</exclude-unlisted-classes>
  9. <shared-cache-mode>NONE</shared-cache-mode>
  10. <validation-mode>NONE</validation-mode>
  11. <properties>
  12. <property name="eclipselink.weaving" value="false" />
  13. <property name="eclipselink.logging.exceptions" value="true" />
  14. <property name="eclipselink.logging.level" value="FINEST" />
  15. <property name="eclipselink.logging.level.sql" value="FINEST" />
  16. <property name="eclipselink.logging.parameters" value="true" />
  17. </properties>
  18. </persistence-unit>
  19. </persistence>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题