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

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

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

import java.math.BigDecimal;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name = "PARENT")
public class Parent  extends Audit {

    @Id 
    @SequenceGenerator(name = "Parent_seq_generator", sequenceName = "PARENT_SEQNUM", allocationSize = 1) 
    @GeneratedValue(strategy=GenerationType.AUTO, generator="Parent_seq_generator")  
    @JsonIgnore
    @Column(name = "PARENT_ID" )
    private BigDecimal id; 

    @Column(name = "PARENT_TYPE" )
    private String parentType; 

    @Column(name = "PARENT_DESC" )
    private String parentDesc; 

    @OneToMany(mappedBy = "lookups", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    private List<Child> childList;

    public List<Child> getChildList() {
      return childList;
    }
    public void setChildList(List<Child> childList) {
      this.childList = childList;
    } 
    public BigDecimal getId() {
        return id;
    }
    public void setId(BigDecimal id) {
        this.id = id;
    } 
    public String getParentType() {
        return parentType;
    }
    public void setParentType(String parentType) {
        this.parentType = parentType;
    } 
    public String getLktDesc() {
        return lktDesc;
    }
    public void setLktDesc(String lktDesc) {
        this.lktDesc = lktDesc;
    }
}

import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name = "CHILD")
public class Child  extends ChildAudit {

    @Id 
    @SequenceGenerator(name = "Child_seq_generator", sequenceName = "CHILD_SEQNUM", allocationSize = 1) 
    @GeneratedValue(strategy=GenerationType.AUTO, generator="Child_seq_generator") 
    @JsonIgnore
    @Column(name = "CHILD_ID" )
    private BigDecimal id;  

    @Column(name = "CHILD_CODE" )
    private String childCode;  

    @Column(name = "CHILD_DESC" )
    private String childDesc;      

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CHILD_PARENT_TYPE", referencedColumnName = "PARENT_TYPE")
    private Parent parents;    

    public Parent getParents() {
        return parents;
    }
    public void setParents(Parent parents) {
        this.parents = parents;
    } 
    public BigDecimal getId() {
        return id;
    }
    public void setId(BigDecimal id) {
        this.id = id;
    } 
    public String getChildCode() {
        return childCode;
    }
    public void setChildCode(String childCode) {
        this.childCode = childCode;
    } 
    public void setChildDesc(String childDesc) {
        this.childDesc = childDesc;
    }
}

import java.sql.Timestamp;
import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonIgnore;

@MappedSuperclass
public class Audit {

    @Column(name = "CREATED_BY", nullable = false, length = 30, updatable = false)
    @JsonIgnore
    private String createdBy;

    @Column(name = "CREATION_DATE", updatable = false)
    @JsonIgnore
    private LocalDateTime createdDate = LocalDateTime.now();

    @Column(name = "LAST_UPDATED_BY", length = 30, updatable = true)
    @JsonIgnore
    private String lastModifiedBy;

    @Version
    @Column(name = "LAST_UPDATE_DATE")
    @JsonIgnore
    private Timestamp lastModifiedDate;

    public String getCreatedBy() {
        return createdBy;
    }
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }
    public String getLastModifiedBy() {
        return lastModifiedBy;
    }
    public void setLastModifiedBy(String lastModifiedBy) {
        if(null == lastModifiedBy) {
            this.lastModifiedBy = "TEST";
        } else {
            this.lastModifiedBy = lastModifiedBy;
        }
    }
    public LocalDateTime getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(LocalDateTime createdDate) {
        this.createdDate = createdDate;
    }
    public Timestamp getLastModifiedDate() {
        return lastModifiedDate;
    }
    @PrePersist
    @PreUpdate
    public void updateLastupdatedBy() {
        setLastModifiedBy("TEST");
    }
}

import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class ChildAudit {

    @Column(name = "CREATED_BY", nullable = false, length = 30, updatable = false)
    @JsonIgnore
    private String createdBy;

    @Column(name = "CREATION_DATE", updatable = false)
    @JsonIgnore
    private LocalDateTime createdDate = LocalDateTime.now();

    @Column(name = "LAST_UPDATED_BY", length = 30, updatable = true)
    @JsonIgnore
    private String lastModifiedBy;

    @Column(name = "LAST_UPDATE_DATE")
    @JsonIgnore
    private LocalDateTime lastModifiedDate = LocalDateTime.now();

    public String getCreatedBy() {
        return createdBy;
    }
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }
    public String getLastModifiedBy() {
        return lastModifiedBy;
    }
    public void setLastModifiedBy(String lastModifiedBy) {
        if(null == lastModifiedBy) {
            this.lastModifiedBy = "TEST";
        } else {
            this.lastModifiedBy = lastModifiedBy;
        }
    }
    public LocalDateTime getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(LocalDateTime createdDate) {
        this.createdDate = createdDate;
    }
    public Timestamp getLastModifiedDate() {
        return lastModifiedDate;
    }
    public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }
    @PrePersist
    @PreUpdate
    public void updateLastupdatedBy() {
        setLastModifiedBy("TEST");
    }
}

我的数据访问类

import org.springframework.beans.BeanUtils;
import javax.persistence.EntityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DataAccessLayer
{
    @Transactional
    public void modifyRecord(ParentDto parentDto, String id) {
        EntityManager em = new MyEntityFactory().getEntity();          
        Parent parent = em.find(Parent.class,new BigDecimal(id));   
        List<Child> childList = new ArrayList<>();
            for (ChildDto childDto : ParentDto.getChildDto()) 
                {
                    Child child = new Child();
                    BeanUtils.copyProperties(childDto, child);  
                    childList.add(child);
                }
                parent.setChildList(childList);
            } 
        BeanUtils.copyProperties(parentDto, parent);
        parent.setId(new BigDecimal(id));

        em.merge(parent);
    }
}

持久性.xml

<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>Parent</class>
        <class>Child</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <shared-cache-mode>NONE</shared-cache-mode>
        <validation-mode>NONE</validation-mode>
        <properties>
            <property name="eclipselink.weaving" value="false" />
            <property name="eclipselink.logging.exceptions" value="true" />
            <property name="eclipselink.logging.level" value="FINEST" />
            <property name="eclipselink.logging.level.sql" value="FINEST" />
            <property name="eclipselink.logging.parameters" value="true" />
        </properties>
    </persistence-unit>
</persistence>

暂无答案!

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

相关问题