JBoss Community

Re: jboss7 quickstarts entitymanager injection question

created by Scott Marlow in JBoss AS7 Development - View the full discussion

Robert,

 

For each separate datasource used in your application , there should be a separate persistence unit for that datasource. 

@PersistenceContext(unitName="PersistenceUnitForMyEmployeeDatasource"

EntityManager emEmployee;

 

@PersistenceContext(unitName="PersistenceUnitForMyAccountsDatasource"

EntityManager emAccounts;

 

 

When you use the above emEmployee, you will be creating/retrieving/updating/deleting employee datasource related entities.  When you use the above emAccounts, you will be dealing with the accounts datasource. 

 

Currently, if you were to leave the unit name out (for either EntityManager), the first persistence unit definition found (in the application), will be used.  There isn't any magic to make the right choice, between the two separate persistence unit definitions.  If you have an application with only one datasource and persistence unit definition, you could leave the UnitName blank. 

 

In my opinion, a best practice, is to always specify the unitName, as that will make it easier in the future, when you go from one Datasource to multiple.  I always want to make the code easy to maintain for the future generation.  Of course there are probably many applications that will always have just one Datasource and those applications are fine to leave the unitName blank.

Reply to this message by going to Community

Start a new discussion in JBoss AS7 Development at Community