1

Using Hibernate with MySQL, I like to use same class and store in two different table. How can i map one class with multiple tables?

I came across this thread, hibernate two tables per one entity

which gives a suggestion to use two different Identity Name for same class through XML configuration. Can some one give me pointers how should i do this mapping in Java configuration? Or can it be done using annotation?

example,

There is Student Class

@Entity
@Table(name="Student_1")
public class Student{

    @Id
    private int StudentId;
    private int StudentName;
    private int studentMailId;

}

I like to have similar Table with same fields with the table name as Student_2. How should I annotate and use it to save in different tables?

Community
  • 1
  • 1
SKay
  • 147
  • 1
  • 22
  • Maybe it's not an option, but couldn't you create a view for this in the DB? This way you can get along without abusing JPA. – meskobalazs Dec 08 '15 at 13:40
  • i might get different data for other similar table, it is not used to just to have copy of that table. – SKay Dec 08 '15 at 13:46
  • 2
    Well, if they *are* different, then why would you want to map it to the same entity? Just create two subclasses, and annotate the `Student`˛class with `@MappedSuperclass` and the new classes with `@Entity` and `@Table`. – meskobalazs Dec 08 '15 at 13:49
  • Fields are same, data of the fields will be different. Let me check whether it is possible with @MappedSuperClass – SKay Dec 08 '15 at 14:02
  • in @MappedSuperClass i have to inherit and have have master Record, i have 15+ tables , i want to create different table name for different set of data for the same program – SKay Dec 09 '15 at 12:38
  • That does not sound reasonable for me. Why not add a new column? – meskobalazs Dec 09 '15 at 12:39
  • it is data which comes from hardware. want to store data that is periodic in one set of tables and the data that sent while testing or in some other state of hardware in another set of tables. suggested to include a column to differentiate, but as the data is huge, it is good to have in separate table to generate report little more easily. The data get stored on every 15 minute, large data will be stored on daily. so one set of tables be used by testing team, as the data will be different, another set of tables shall be used for live report. Both set of tables have same fields ! – SKay Dec 09 '15 at 13:23

1 Answers1

0

I know this question was asked long time ago, and a response was posted here . I would like to suggest an alternative way, without using any of hibernate things.

Declare an interface with getter and setter methods of commun columns, then make your Student_1 class and Student_2 classs implement this interface. Leave your JPA mapping in this two class as usual, then in your code, you can invoke method of this interface instead.

Qianlong
  • 269
  • 2
  • 12