0

I have multiple tables in database with exactly same columns and signature. All the tables are created based on year like all the data for 2020 is in one table and for 2021 it's in other table.

Is it possible to use the a single entity for multiple tables?

aakarvind
  • 1
  • 2
  • 2
    https://stackoverflow.com/questions/18283919/is-possible-map-a-single-entity-with-multiple-tables-using-jpa Does this help you? – JCompetence Feb 11 '21 at 07:11

1 Answers1

-1

Yes, this is possible. You can specify your tables name and prefix them in a single entity class.

@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

}

You can use this piece of code.

Coder
  • 59
  • 3
  • I want to insert the records only in 1 table i.e. if the financial year is 2021 then the records should be inserted in 2021 table and not in 2020. If this can be achieved? Also how will I able to fetch and update the records specified on the year using a single entity? – aakarvind Feb 12 '21 at 04:36