2010-11-08 12 views
5

Ho la seguente classe:Problemi con l'utilizzo di @PreUpdate

@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(); 
    } 

Questa classe viene utilizzata da tutti i miei classi di entità.

così ho la seguente classe

@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; 

E

@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; 

Così, quando creo un nuovo cliente ottengo il seguente errore:

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) 

Che cosa sto facendo sbagliato qui

+0

Potete fornire lo stacktrace completo della vostra eccezione. Non penso che tu abbia un problema con 'PreUpdate' o' PrePersist'. La proprietà 'not-null 'fa riferimento a un valore nullo o transitorio: vsg.ecotrak.dataaccess.domain.Address.lastUpdateDTM' dice che' lastUpdateDTM' è 'null' e che probabilmente Hibernate fallisce prima di attivare qualsiasi' @Pre ... ' metodi. – kraftan

risposta

9

Il PrePersist e PreUpdate callback funzionano in un MappedSuperclass ... almeno quando si utilizza l'API EntityManager da APP, che non è chiaro nel tuo caso.

Si sta utilizzando l'API EntityManager o Session? Nel caso successivo, i metodi annotati utilizzando le annotazioni da JPA non verranno richiamati (e il mio suggerimento sarebbe quello di utilizzare a listener or an interceptor).

+0

Utilizzando Session - non si utilizza JPA – boyd4715

+0

beh allora non può funzionare –

+0

bene che abbia senso allora - credo di averlo perso nella documentazione - grazie – boyd4715