Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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; 
like image 663
Muhammad Bilal Hasan Avatar asked Mar 26 '14 17:03

Muhammad Bilal Hasan


People also ask

How do you map an entity to multiple tables in Hibernate?

Yes, you can map an entity to 2 database tables in 2 simple steps: You need to annotate your entity with JPA's @Table and @SecondaryTable annotations and provide the names of the first and second table as the value of the name parameters.

How do you map Java objects with database tables in Hibernate?

Define Hibernate Mapping FileThe <class> elements are used to define specific mappings from a Java classes to the database tables. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute.


1 Answers

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 
like image 112
Abhishek Nayak Avatar answered Oct 07 '22 13:10

Abhishek Nayak