EJB annotation not working
sensorhound-nan Mar 27, 2017 2:26 PMI have a problem with my EJB annotations:
I have a DAO:
@Singleton
@Startup
public class CassandraDAO<T extends Serializable> extends AbstractDAO {
@PersistenceContext(unitName = "JPAService", type = PersistenceContextType.EXTENDED)
private EntityManager em;
@PostConstruct
public void init() {
System.out.println("***************** init CassandraDAO ***************************");
}
@PreDestroy
public void destroy() {
System.out.println("***************** destroying CassandraDAO *************************");
if (em.isOpen()) {
em.close();
}
}
@Override
public EntityManager getManager() {
return em;
}
@Override
public void setManager(EntityManager em) {
this.em = em;
}
}And there are several DAOs that extends this DAO:
@Stateless
public class IOConfigurationDAO extends CassandraDAO<IOConfiguration> {
...
}And here is how I use the DAO:
public class AddAlertTest extends TestCase {
@EJB
private IOConfigurationDAO ioConfigurationDAO;
public void test() {
ioConfigurationDAO.getManager().getTransaction().begin();
IOConfiguration ioConfig = new IOConfiguration("CH1");
ioConfigurationDAO.getManager().persist(ioConfig);
ioConfigurationDAO.getManager().getTransaction().commit();
int oldSize = ioConfig.getAlerts() == null ? 0 : ioConfig.getAlerts().size();
System.out.println("Created IO configuration with id " + ioConfig.getIoConfigurationId());
ioConfigurationDAO.getManager().getTransaction().begin();
Alert alert = ioConfigurationDAO.addAlert(ioConfig,
Operators[(int) (Math.random() * Operators.length)], new Double(Math.random() * 100));
ioConfigurationDAO.getManager().getTransaction().commit();
int newSize = ioConfig.getAlerts().size();
System.out.println("Created new Alert with id " + alert.getAlertId());
System.out.println("Alert list: " + ioConfig.getAlerts());
System.out.println("Alert is connected to " + alert.getIoConfiguration());
assertEquals(oldSize + 1, newSize);
assertNotNull(ioConfigurationDAO.getManager().find(Alert.class, alert.getAlertId()));
ioConfigurationDAO.getManager().getTransaction().begin();
ioConfigurationDAO.getManager().remove(ioConfig);
ioConfigurationDAO.getManager().getTransaction().commit();
System.out.println("Deleted IO configuration with id " + ioConfig.getIoConfigurationId());
}
But my IOConfigurationDAO is always null, so my persistenceContext annotation doesn't work at all. And there is nothing printed out in my console, so my postConstruct annotations doesn't work as well.
Here is my persistence.xml:
<?xml version="1.0"?> <persistence 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" version="2.0"> <persistence-unit name="JPAService"> <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider> <class>com.sensorhound.aigateway.domain.Alert</class> <class>com.sensorhound.aigateway.domain.DataSeriesMeta</class> <class>com.sensorhound.aigateway.domain.IOConfiguration</class> <class>com.sensorhound.aigateway.domain.NodeData</class> <properties> <property name="hibernate.transaction.jta.platform" value="JBossAS" /> <property name="jboss.as.jpa.providerModule" value="org.hibernate:5.0" /> <property name="hibernate.ogm.datastore.provider" value="cassandra_experimental"/> <property name="hibernate.ogm.datastore.host" value="127.0.0.1:9042"/> <property name="hibernate.ogm.datastore.database" value="dev"/> <property name="hibernate.hbm2ddl.auto" value="none"></property> <property name = "hibernate.show_sql" value = "true" /> <property name = "hibernate.format_sql" value = "true" /> </properties> </persistence-unit> </persistence>
I don't know what is wrong in my configuration. Someone please help me. If you need more information, I am willing to share.
Thanks!