简体   繁体   中英

Different representation of same table in hibernate

I have question about hibernate. I have class User which contain base information about user.

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;

Now i need to create class UserFull with extended information about user

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;

How can i implement such extending with hibernate? Both classes reference on same table "user".

This code will do what you want to achieve

 @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;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM