24

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 the code, where I have used @SecondaryTable. It allows me to define only one secondary table. But I need 3 tables to be used by the same class.

@Entity
@Table(name = "table1")
@SecondaryTable(name="table2")
public class TableConfig
    implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "mac", table= "table1")
    private String uniqueIdentifier;
  • I think here you can find your answer http://stackoverflow.com/questions/5215471/how-to-map-2-identical-tables-same-properties-to-1-entity – Phil_Charly Jul 26 '15 at 17:29
  • If the tables don't have the same structure and don't share same PKs, see this article : https://www.thoughts-on-java.org/hibernate-tips-how-to-map-an-entity-to-multiple-tables/ – Guillaume Husta Jun 07 '18 at 09:48

3 Answers3

27

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
Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64
  • 1
    Hi Rembo, Thanks for answering but in this example you are joining three tables into one. In my case I want to just map it. Like three tables are almost identical in DB, and I want only one class to represent them. – Muhammad Bilal Hasan Mar 27 '14 at 13:14
  • 1
    @BilalHasan i think that's not possible, research on it when you find a solution let me know. – Abhishek Nayak Mar 27 '14 at 13:49
12

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.

Aaron
  • 133
  • 2
  • 7
0

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).

Dherik
  • 17,757
  • 11
  • 115
  • 164
user3028989
  • 87
  • 13