tags:

views:

256

answers:

2

New to EJB3, please help/explain.

Inside a session bean I declare an EntityManager as follow

@PersistenceContext(unitName="ScheduleUnit")
private EntityManager em;

and this works. But when I do this

private EntityManager em;
private EntityManagerFactory emf;

public void myFunction() {
  emf = Persistence.createEntityManagerFactory("ScheduleUnit");
  em = emf.createEntityManager();
}

I get the following error:

A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property
A: 

It is unclear where you use the second code snippet (is it in an EJB? if yes, you're not supposed to use the EntityManagerFactory in a managed environment like an EJB container). Could you clarify?

Also please show your persistence.xml (the error message is about this file not containing required information).

Pascal Thivent
Thanks for replying Pascal.Yes, it's in an EJB. For my application each client has their own database but the bussiness logic is the same for all, and I was thinking I could create a persistence unit (PU) for each client and select the corresponding PU at run time.Here is my persistence.xml:<persistence-unit name="ScheduleUnit" transaction-type="JTA"><provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <jta-data-source>myDatasource</jta-data-source> <class>com.skomarcorp.Schedule</class> </persistence-unit>
bovo
A: 

I think, EntityManagerFactory can't find data source, specified in your persistence unit. As for Glassfish, this information is stored in sun-resources.xml file. Is it j2ee application? If so, it is better to use dependency injection with @PersistenceContext annotation (as Pascal said).

Also, you can try to use method createEntityManagerFactory(String persistenceUnitName, Map properties) and specify "ConnectionDriverName" propety in properties map:

private EntityManager em;
private EntityManagerFactory emf;

public void myFunction() {
  HashMap<String, String> properties = new HashMap<String, String>();
  properties.put("ConnectionDriverName", "org.postgresql.Driver");  //as for Postgres
  emf = Persistence.createEntityManagerFactory("ScheduleUnit", properties);
  em = emf.createEntityManager();
}
Dmitry
And see this article, if you write j2se app: http://java.sun.com/developer/technicalArticles/J2SE/Desktop/persistenceapi/
Dmitry
Thanks Dmitry,I'm using Genonimo instead of Glassfish for the J2EE server. I've set up the data source correctly. <br /> Here's my persistence.xml: <persistence-unit name="ScheduleUnit" transaction-type="JTA"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <jta-data-source>myDatasource</jta-data-source> <class>com.skomarcorp.Schedule</class> </persistence-unit>. Can you explain the difference between using @PersistenceContext and EntityManagerFactory and why one is better than the other?
bovo
Using annotations makes your code simpler :) In this case container (geronimo) resolves link to your entity manager automatically and the life cycle of EntityManager instance is managed by container. See this http://java.sun.com/javaee/5/docs/tutorial/doc/bnbqw.html
Dmitry