1

I have a Hibernate project, and multiple entities. Each entity needs to connect to several database. (table1, table2, table3, table4) same schema.

Can this be accomplish? or do I need to create a separate entity for each of those?

My entity look something like this

@Entity
public class table1{
     @Id
     @Column(name="name")
     private String name;

     @Column(name="age")
     private String age;

     //getters setters
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1817081
  • 1,185
  • 3
  • 15
  • 21
  • Do you need to connect to several databases **or** you need to fetch data from multiple tables? I could not understand from your question since the additional explanation in the parenthesis is a bit confusing. – Matin Kh Dec 13 '12 at 07:17
  • I am connecting to the same database, with one entity class i want to connect to multiple tables within that database that has the same schema. – user1817081 Dec 13 '12 at 16:46

1 Answers1

2

You can use same entity for different databases having similar schema, but have to create EntityManager pointing to the specific database.

  • Creating persistence unit for each database.

Below is the sample code for persistence.xml

<persistence-unit name="DB_X"> 
<jta-data-source>java:/OracleDS</jta-data-source>  
... 
</persistence-unit>
<!-- Other Persistence Units -->

Creating EntityManager for specific unit

@PersistenceContext(unitName="DB_X")
private EntityManager xEM;

@PersistenceContext(unitName="DB_Y")
private EntityManager yEM;
  • Else, can also create it at runtime as below.

EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName); EntityManager em = emf.createEntityManager();

Afterwards, you can use same entity with different databases having similar schema ,table structure with appropriate EntityManager.


Edit : Based on your comments, which differed from what you had posted as question, it seems you are trying to use same entity for multiple tables. Below is the sample code for it.

@MappedSuperClass
public class abstract BaseEntity {
     @Id
     @Column(name="name")
     private String name;

     @Column(name="age")
     private String age;

     //-- accessor methods
}

@Entity
@Table(name = "XTable")
public class XEntity extends BaseEntity {
    public XEntity(){}
}

@Entity
@Table(name = "YTable")
public class YEntity extends BaseEntity {

    public YEntity(){}
}

Here, XEntity & YEntity are similar, but points to their respective tables.

Nayan Wadekar
  • 11,444
  • 4
  • 50
  • 73
  • I am using SessionFactory instead of EntityManagerFactory. Is Entity any different than session? – user1817081 Dec 13 '12 at 16:47
  • @user1817081 Based on you comment, updated post. Refer edit part – Nayan Wadekar Dec 14 '12 at 05:29
  • So are those classes all in the same file. Is there any way to configure it dynamically. Meaning that user change something in the front end and the back end points to a different table. I have a lot of tables that I need to map to and to hard code everything will not be very efficient – user1817081 Dec 14 '12 at 14:05
  • @user1817081 No, they are different classes. You can use appropriate class based on your logic for their respective tables. – Nayan Wadekar Dec 15 '12 at 11:32