0

I have two tables in the database that have the same layout, however they are not related and each one is independent of the other. I have the entity class below that points to one of the tables. Can I somehow reuse it for the other table, or do I need to replicate the class and point it to the other table?

    import javax.persistence.Entity;
    import javax.persistence.Column;
    ...
    ....
    @Entity
    @Table(schema = "my_schema", name = "my_table1")
    //@Table(schema = "my_schema", name = "my_table2") - note table1 and table2 have the same schema
    public class MyTable {
      ...
      ...
        @Column(name = "my_col1")
        String myCol1;
      ...
      ...
      getters
      setters
      etc.
 } 
C J
  • 298
  • 4
  • 13
  • 1
    how about having a parent class declaring all the tables attributes and then subclass the parent? – willermo Jan 19 '18 at 21:12
  • Thank you @willermo. Your comment could very well be the answer. – C J Jan 19 '18 at 21:15
  • You can use xml mapping. Take a look at https://stackoverflow.com/questions/16755260/hibernate-two-tables-per-one-entity – ayortanli Jan 19 '18 at 21:17
  • @ayortanli, I rather stay with annotations. Also subclassing will allow me to use better descriptive names for each class. – C J Jan 19 '18 at 21:24

0 Answers0