@prePersist不使用hibernate4 + spring4

时间:2015-09-16 22:28:51

标签: java maven

我创建了一个基于maven的Spring4 + hibernate4项目。 我创建了我的模型: 但是我想设置已创建和最后修改的属性以自动获取它们的值。

我已经使用@prePersist和@preUpdate创建了一个带注释的方法,我将这些属性设置为new date(),但它不起作用。

这是我的模特:

@Entity
@Table(name="users")
@Inheritance(strategy=InheritanceType.JOINED)
@EntityListeners(User.class)
public class User {
    private int user_id;
    private String last_name;
    private String first_name;
    private String email,tel,login,password,profil_pic;
    private String birth_day;
    private Date created,last_modified;


    private City city;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    private List<Rating> ratings;

    private List<Ad> ads;



    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id=user_id;
    }



    @Column(nullable=false,unique=true)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(nullable=false,length=14,unique=true)
    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }



    @Column(nullable=false,length=150,unique=true)
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    @Column(nullable=false,length=255)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getProfil_pic() {
        return profil_pic;
    }

    public void setProfil_pic(String profil_pic) {
        this.profil_pic = profil_pic;
    }





    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getBirth_day() {
        return birth_day;
    }

    public void setBirth_day(String birth_day) {
        this.birth_day = birth_day;
    }

    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable=false,updatable=false)
    public Date getLast_modified() {
        return last_modified;
    }

    public void setLast_modified(Date last_modified) {
        this.last_modified = last_modified;
    }

    @ManyToOne
    @JoinColumn(name="city_id")
    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    public List<Rating> getRatings() {
        return ratings;
    }

    public void setRatings(List<Rating> rating) {
        this.ratings = rating;
    }

    @OneToMany(targetEntity=Ad.class,mappedBy="user")
    public List<Ad> getAds() {
        return ads;
    }

    public void setAds(List<Ad> ads) {
        this.ads = ads;
    }




        @PreUpdate
        @PrePersist
        public void updateTimeStamps() {
            last_modified = new Date();
            if (created==null) {
              created = new Date();
            }
        }




}

这是我的测试:

public class TestDao {




    private static ApplicationContext ac;

    public static void main(String[]_tyu){


        ac = new ClassPathXmlApplicationContext("spring/config/servlet-context.xml");
        UserDao ud=(UserDao)ac.getBean("userDao");
        User u=new User();
        u.setLogin("nakala");
        u.setFirst_name("eren");
        u.setLast_name("yegar");
        u.setLogin("killtitans");
        u.setPassword("mikasasister");
        u.setBirth_day("31/05/2000");
        u.setTel("123654789");
        u.setProfil_pic("bonhomme");
        u.setEmail("erenygar@hotmail.com");
        ud.signUp(u);
    }
}

这是我的Dao方法

@Override
    @Transactional
    public void signUp(User u) {
        sf.getCurrentSession().persist(u);

    }

0 个答案:

没有答案