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

前端 未结 3 1190
南笙
南笙 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-02 23:56

    you can define one class like below :

    @Entity
    @Table(name="table1")
    @SecondaryTables({
          @SecondaryTable(name="table2", pkColumnJoins={@PrimaryKeyJoinColumn(name = "id")}),
          @SecondaryTable(name="table3", pkColumnJoins={@PrimaryKeyJoinColumn(name = "id")})
      })
    public class TestEntity {
          @Id
          @GeneratedValue
          private int id;
    
          private String field1;
    
          @Column(name="column2", table="table2")
          private String field2;
    
          @Column(name="column3", table="table3")
          private String field3;
    
          getter and setter...
    }
    

    In your DB, should has three table, and all of them should has the same primary key "id".

    then, use can test like this:

    TestEntity test = new TestEntity();
    test.setField1("field1");
    test.setField2("field2");
    test.setField3("field3");
    
    em.merge(test);
    

    after test, in your DB, you will find one record in each table:

    table1:

     1, field1
    

    table2:

     1, field2
    

    table3:

     1, field3
    

    all of them will share the primary key value. Hope this will help you.

    0 讨论(0)
  • 2020-12-02 23:58

    In Hibernate mapping file you can specify the entity-name mapping with virtual name along with polymorphism="explicit" and class name would be physical class name. Like that you may do multiple mappings. While loading the object use entityname (virtual name).

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题