Is it possible to use the entity-name attribute in class to set an entity and to reference it? I want to do this because I want to map to multiple tables with the same entity class.
Table 1 and adble 2 have the same schema
@Entity
public class POJO{
@Id
@Column(name="column1")
private String column1;
@Column(name="column2")
private String column2;
//getters and setters
}
<hibernate mapping>
<class name="package.POJO" entiy-name="EntityTable1" table="table1">
<id>.....</id>
<property>....</property>
<property>....</property>
</class>
<class name="package.POJO" entiy-name="EntityTable2" table="table2">
<id>.....</id>
<property>....</property>
<property>....</property>
</class>
</hibernate mapping>
Session s = SessionFactory.openSession();
List table1List = s.createQuery("FROM EntityTable1").list();
List table1List = s.createQuery("FROM EntityTable2").list();
I read in the Hibernate Documentation that this is only in the experimental stage. Has anyone used this method and work?
Yes, you can do this via XML, I didn’t experience any problems with that. Here’s an example from this project (since it’s being a long time since I touched that project, I’m not cleaning up the code to show the minimal working example):
And:
Here’s an example of usage #1
And here’s #2 respectively:
Note, that you can’t do that same with annotations, that’s where XML is more flexible.