I am starting struts2 based application with Jboss provided JPA libraries. I have configured data-source in standalone.xml I can see from the jboss administration console that the datasource is created. and the presistence.xml files are read and processed. But if I check the EntityManager instance in Action Class. It always says null.
Here is my persistence.xml and Action class snippet
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="primary">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/mysqlDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
Struts2 Action Class:
public class RegistrationAction extends ActionSupport implements SessionAware,Preparable ,ModelDriven{
/**
*
*/
private static final long serialVersionUID = 1L;
@PersistenceContext(unitName="primary")
private EntityManager em;
@Override
public Object getModel() {
// TODO Auto-generated method stub
return null;
}
@Override
public void prepare() throws Exception {
if(em==null)
System.out.println(" EM is null still..");
//even Persistence.createEntityManagerFactory("primary"); returning NULL
}
@Override
public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
}
public EntityManager getEm() {
return em;
}
public void setEm(EntityManager em) {
this.em = em;
}
}