getReference之后@MappedSuperclass超类属性为空

时间:2016-11-30 10:14:55

标签: hibernate

// Super class
@MappedSuperclass
abstract class BaseModel implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private Long id;
    @Column(name = "create_time")
    @Temporal(TemporalType.TIMESTAMP)
    @CreationTimestamp
    private Date createTime;
    @Column(name = "update_time")
    @Temporal(TemporalType.TIMESTAMP)
    @UpdateTimestamp
    private Date updateTime;
    @Version
    private Long version;
    //getter and setter here
}


//Sub class
@Entity
@Table(name = "test")
public class Test extends BaseModel{
    private String name;
    @Enumerated
    @Column(name = "double_z")
    private X x;
    public enum X{Y,Z}
    //getter and setter
}

最奇怪的是当我调用方法entityManager#发现它似乎一切正常,但是当我调用entityManager#getReference时,超类的属性返回null。

Test ref = entityManager.getReference(Test.class, 11L);
System.out.println(ref.getId()); //null(property of super class)
System.out.println(ref.getUpdateTime());//null(property of super class)
System.out.println(ref.Name());//udf(property of sub class)
Test f = entityManager.find(Test.class, 11L);
System.out.println(f.getId()); //11
System.out.println(f.getUpdateTime());//2016-11-30 16:38:29.0
System.out.println(f.Name());//udf

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

让类BaseModel公开解决这个问题。