@EntityListener是否也与@MappedSuperclass一起使用? [英] Does @EntityListener works with @MappedSuperclass as well?

查看:190
本文介绍了@EntityListener是否也与@MappedSuperclass一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我定义一个实体类并用 @MappedSuperclass 和一个 @EntityListener ,这个监听器是否也会被调用到子类中的生命周期事件?



示例:

  @MappedSuperclass 
@EntityListeners(class = LastUpdateListener.class)
public abstract class Animal {
@Id private Integer ID;
私人字符串名称;
私人日历dateOfBirth;
@Transient private int age;
私人日期lastUpdate;
// getters和setters

/ **
*根据计算在加载时设置我的瞬态属性,
*请注意,原生Hibernate公式映射是为此更好。
* /
@PostLoad
public void calculateAge(){
Calendar birth = new GregorianCalendar();
birth.setTime(dateOfBirth);
Calendar now = new GregorianCalendar();
now.setTime(new Date());
int adjust = 0; (now.get(Calendar.DAY_OF_YEAR) - birth.get(Calendar.DAY_OF_YEAR)< 0){
adjust = -1;
if
}
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR)+ adjust;



public class LastUpdateListener {
/ **
*在任何数据库持久化之前设置的自动属性
* /
@PreUpdate
@PrePersist
public void setLastUpdate(Cat o){
o.setLastUpdate(new Date());


$ / code $ / pre
$ b $ p感谢。
<解决方案是的,在映射超类中使用@PostLoad注释的方法和LastUpdateListener的实体监听器方法被调用。

映射超类的生命周期事件本身并不存在。像往常一样,它适用于子类。

Folks!

If I define an Entity Class and annotate it with @MappedSuperclass and an @EntityListener, does the listener also get called for lifecycle events within a child class?

Example:

@MappedSuperclass
@EntityListeners(class=LastUpdateListener.class)
public abstract class Animal {
    @Id private Integer id;
    private String name;
    private Calendar dateOfBirth;
    @Transient private int age;
    private Date lastUpdate;
    //getters and setters

    /**
     * Set my transient property at load time based on a calculation,
     * note that a native Hibernate formula mapping is better for this purpose.
     */
    @PostLoad
    public void calculateAge() {
        Calendar birth = new GregorianCalendar();
        birth.setTime(dateOfBirth);
        Calendar now = new GregorianCalendar();
        now.setTime( new Date() );
        int adjust = 0;
        if ( now.get(Calendar.DAY_OF_YEAR) - birth.get(Calendar.DAY_OF_YEAR) < 0) {
            adjust = -1;
        }
        age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR) + adjust;
    }
}

public class LastUpdateListener {
    /**
     * automatic property set before any database persistence
     */
    @PreUpdate
    @PrePersist
    public void setLastUpdate(Cat o) {
        o.setLastUpdate( new Date() );
    }
}

Thanks.

解决方案

Yes, method annotated with @PostLoad in mapped superclass and LastUpdateListener's entity listener methods get called.

Such a thing as lifecycle event for mapped superclass itself does not even exist. As usual, it applies to subclass.

这篇关于@EntityListener是否也与@MappedSuperclass一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆