Can't create EntityManager
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
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();
}
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).
精彩评论