在hibernate中相同表的不同表示

时间:2015-11-13 21:04:45

标签: java hibernate

我对hibernate有疑问。我有类User,其中包含有关用户的基本信息。

[InsuredName] = [INDEX(Data,RetrievalRow,InsuredNameIndex)]

现在我需要使用有关用户

的扩展信息创建类UserFull
public class User {

@Id
@GeneratedValue
@Column(name = "id")
private int id;

@Column(name = "name")
private String name;

@Column(name = "surname")
private String surname;

@Column(name = "photo_url")
private String photoURL;

我如何用hibernate实现这样的扩展?两个类都引用相同的表" user"。

1 个答案:

答案 0 :(得分:0)

此代码将执行您想要实现的目标

 @Entity  
    @Table(name="User")  
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE) //Least normalisation strategy  
    public class User
    { 
        @Id
        @GeneratedValue
        @Column(name = "id")
        private int id;

        @Column(name = "name")
        private String name;

        @Column(name = "surname")
        private String surname;

        @Column(name = "photo_url")
        private String photoURL;
    }

    @Entity  
    @Table(name="UserFull")  
    public class UserFull extends User {

        @Column(name = "email")
        private String email;

        @Column(name = "date_birthday")
        private Date dateBirthday;

        @Column(name = "date_registration")
        private Date dateRegistration;
   }