小编典典

@PreUpdate的使用问题

hibernate

我有以下课程:

    @MappedSuperclass
public abstract class MappedModel
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false, unique = true)
    private Long mId;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "rec_created_dtm", nullable = false, updatable = false)
    private Date recordCreatedDTM;

    @Column(name = "rec_cre_user_id", nullable = true, updatable = false)
    private Long recordCreatedUserId;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_update_dtm", nullable = false)
    private Date lastUpdateDTM;

    @Column(name = "last_update_user_id", nullable = true)
    private Long lastUpdateUserId;

//  @PrePersist
//  protected void onCreate()
//  {
//      this.lastUpdateDTM = this.recordCreatedDTM = new Date();
//  }
//
//  @PreUpdate
//  protected void onUpdate()
//  {
//      lastUpdateDTM = new Date();
//  }

  @PrePersist
  @PreUpdate
  protected void updateDates() {
    if (this.recordCreatedDTM == null) {
        this.recordCreatedDTM = new Date();
    }
    lastUpdateDTM = new Date();
  }

我的所有实体类都使用该类。

所以我有以下课程

@Entity
@Table(name="customer")
public class Customer extends MappedModel implements Serializable
{

    /**
   * 
   */
  private static final long serialVersionUID = -2543425088717298236L;


    @OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="address_id",nullable=true,updatable=true,insertable=true)
    private Address mAddress;

@Entity
@Table(name="address")
public class Address  extends MappedModel implements Serializable
{

    /**
   * 
   */
  private static final long serialVersionUID = -3505413538055124608L;

  @Column(name="address_line_1", length=150, nullable=false)
  private String mAddressLine;

  @Column(name="city", length=150, nullable=false)
  private String mCity;

  @Column(name="state", length=2, nullable=true)
  private String mState;

  @Column(name="postal_code", length=10, nullable=false)
  private String mPostalCode;

因此,当我创建一个新客户时,出现以下错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: vsg.ecotrak.dataaccess.domain.Address.lastUpdateDTM; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: vsg.ecotrak.dataaccess.domain.Address.lastUpdateDTM

org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:583)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

我在这里做错了。


阅读 889

收藏
2020-06-20

共1个答案

小编典典

至少在使用JPA 的API 时,PrePersistand
PreUpdate回调确实在MappedSuperclass…中工作EntityManager,这在您的情况下还不清楚。

您在使用EntityManager还是SessionAPI?在后一种情况下,使用注解的方法PrePersistPreUpdate从JPA注释将不会被调用(和我的建议是使用监听器或拦截器)。

2020-06-20