How to map one class with multiple tables in Hibernate/javax.persistance?

前端 未结 3 1273
南笙
南笙 2020-12-02 23:35

I want to use one class to map three tables. I know javax.persistance provides the @SecondaryTable annotation to map two tables to one class.

Below is

3条回答
  •  离开以前
    2020-12-03 00:11

    I want to use one class to map three tables, From what I know is that javax.persistance provides @SecondaryTable annotation to map two tables to one class

    use @SecondaryTables to map more than one table.

    You can map a single entity bean to several tables using the @SecondaryTables class level annotations. To express that a column is in a particular table, use the table parameter of @Column or @JoinColumn.


    for example there is 3 entity's namely: Name , Address & Student:

    Name entity will look like:

    @Entity
    @Table(name="name")
    public class Name implements Serializable {
    
        @Id
        @Column(name="id")
        private int id;
        @Column(name="name")
        private String name;
    
        public Name(){}
    
        public Name(int id,String name){
            this.id=id;
            this.name=name;
        }
            //getters and setters
    }
    

    Address entity will look like:

    @Entity
    @Table(name="address")
    public class Address implements Serializable {
    
        @Id
        @Column(name="id")
        private int id;
        @Column(name="address")
        private String address;
    
        public Address(){}
    
        public Address(int id, String address) {
            super();
            this.id = id;
            this.address = address;
        }
            //getters and setters
    }
    

    Student entity will look like:

    @Entity
    @Table(name="student")
    @SecondaryTables({
        @SecondaryTable(name="name", pkJoinColumns={
            @PrimaryKeyJoinColumn(name="id", referencedColumnName="student_id") }),
        @SecondaryTable(name="address", pkJoinColumns={
            @PrimaryKeyJoinColumn(name="id", referencedColumnName="student_id") })
    })
    public class Student implements Serializable {
    
        @Id
        @Column(name="student_id")
        private int studentId;
    
        @Column(table="name")
        private String name;
    
        @Column(table="address")
        private String address;
    
        public Student(){}
    
        public Student(int studentId){
            this.studentId=studentId;
        }
            //getters and setters
    }
    

    Store like:

        Student s= new Student(1);
        session.save(s);
    
        Name n=new Name(s.getStudentId(),"Bilal Hasan");
        session.save(n);    
    
        Address address = new Address(s.getStudentId(), "India");
        session.save(address);
    
        Student ob = (Student)session.get(Student.class, s.getStudentId());
    
        System.out.println(ob.getStudentId());
        System.out.println(ob.getName());
        System.out.println(ob.getAddress());
    

    ouput:

    1
    Bilal Hasan
    India
    

提交回复
热议问题