开发者

Auto timestamp update not working in hibernate

开发者 https://www.devze.com 2023-02-19 02:26 出处:网络
HI, My POJO class is @Entity @Table(name = \"seed\") public class SeedUrl { @Id SeedUrlPrimaryKey primaryKey = new SeedUrlPrimaryKey();

HI,

My POJO class is

@Entity
@Table(name = "seed")
public class SeedUrl {

    @Id
    SeedUrlPrimaryKey primaryKey = new SeedUrlPrimaryKey();

     @Temporal(TemporalType.TIMESTAMP)
    @Column
    private Date lastUpdated;



    public SeedUrl(String url){
        this.url = url;
    }
    public SeedUrl(){}

    public SeedUrlPrimaryKey getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(SeedUrlPrimaryKey primaryKey) {
        this.primaryKey = primaryKey;
    }


    @PrePersist
    @PreUpdate
    public void onLastUpdated() {
        this.lastUpdated = new Date();
    }
}

I want to update the same column either on insert or update ..

I am saving the object as follows:

SeedUrlPrimaryKey primaryKey = new SeedUrlPrimaryKey();
            primaryKey.setSeedId("111121123");
        开发者_如何转开发    seedUrl.setPrimaryKey(primaryKey);
            session.save(seedUrl);
                    session.getTransaction().commit()

But in the Database the value of lastUpdated is coming as null.. In DB the column is declared as TIMESTAMP.

Where am i going wrong ?


When you use Hibernate via Session interface, JPA callback methods (@PrePersist, etc) don't work. You can use Hibernate listeners instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消