im facing following problem. In my application I'm trying to use #PersistenceContext injected into the NON ejb instance of DAO by Glassfish embedded server. So it's a pojo controlled by CDI. Like this :
#Named
public class DummyDAO implements Serializable {
#PersistenceContext private EntityManager entityManager;
public void testManager() {
if (entityManager == null) {
throw new RuntimeException("It's null!!");
}
System.out.println("Hello! I'm injected");
}
}
This dummy DAO injects into a JSF bean by CDI like this :
#ManagedBean
#SessionScoped
public class OrderImportManagerBean implements Serializable {
#Inject
DummyDAO dummyDAO;
public void testManager() {
dummyDAO.testManager();
}
}
testManager() called from an xhtml page like #{orderImportManagerBean.testManager}
entityManager is always null. The CDI itself works, all instances injected as they should but not this one. I have persistence.xml and orm.xml under resources/META-INF catalog. So after the project is packaged I get META-INF and stuff in right place (classes/META-INF). Here is my persistence.xml :
<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="persistance-unit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name = "hibernate.show_sql" value = "true" />
</properties>
</persistence-unit>
</persistence>
Now I'm wondering what is wrong with my configuration? Any suggestions?
Thanks.
Related
I am trying to run a very basic JPA example on Wildfly.
This is the structure of my project:
Person and Project are two JPA entities.
This is the content of my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="testJpa">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>entities.Person</class>
<class>entities.Project</class>
<properties>
<property name="eclipselink.target-database" value="Derby"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/JPADB"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="pwd"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Tables are correctly generated from the entity classes so I know the persistence.xml is valid.
I am trying to create an entity in the CreatePerson class, like this:
public class CreatePerson {
public static void main( String[ ] args ) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("testJpa");
EntityManager entitymanager = emfactory.createEntityManager( );
entitymanager.getTransaction( ).begin( );
Person usr = new Person( );
usr.setName("Bob");
entitymanager.persist( usr );
entitymanager.getTransaction( ).commit( );
entitymanager.close( );
emfactory.close( );
}
}
I get the following error
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named testJpa
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at services.CreatePerson.main(CreatePerson.java:11)
It is totally unclear what are you doing. Wildfly is an JavaEE application server, but I see JavaSE class public static void main.
You should create JavaEE project (for Eclipse, as far as I know you need to download Eclipse IDE for Java EE Developers ) then probably create DAO EJB, inject EntityManager in it with
#PersistenceContext(unitName = "puName")
private EntityManager em;
then you code should work
Person usr = new Person("blabla");
em.persist(usr)
IMHO, you should look for another tutorial.
I'm developing a simple application on JAVA EE using JSF, JPA and Maven.
the question is: Why the tomcat servlet on Heroku is returning null on the time to inject a ManagedBean while in Glassfish server everything is fine?
There are my codes:
#ManagedBean(name = "homeBean")
#SessionScoped
public class HomeBean {
//simple properties
#ManagedProperty(value="#{emFactoryBean}")
private EntityManagerFactoryBean factoryBean;
#PostConstruct
public void init(){
sitioService = new SitioService(factoryBean);
sitios = sitioService.getSitios();
}
//Getters and setters
}
The exception occurs when a new instace of SitioService is created, In other words. Here is the factorBean Class:
#ApplicationScoped
public class EntityManagerFactoryBean {
private EntityManagerFactory entityManagerFactory;
public EntityManagerFactoryBean() {
this.entityManagerFactory = Persistence.
createEntityManagerFactory("prueba");
}
//getter and setter
}
My persistence.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="prueba" transaction-type="RESOURCE_LOCAL">
<class>com.sertracen.sivt.modelo.Imagen</class>
<class>com.sertracen.sivt.modelo.Review</class>
<class>com.sertracen.sivt.modelo.Sitio</class>
<properties>
<property name="javax.persistence.jdbc.url" value="{url}"/>
<property name="javax.persistence.jdbc.user" value="{usuario}"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.password" value="{contraseña}"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation.scripts.action" value="create"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
</properties>
</persistence-unit>
Obviously my parameter connections are ok.
Hope for your answers!
Cheers!
----STACK TRACE:
om.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on managed bean homeBean
....
Caused by: java.lang.NullPointerException
at com.sertracen.sivt.negocio.SitioService.<init>(SitioService.java:26)
at com.sertracen.sivt.web.HomeBean.init(HomeBean.java:75)
In my project I use Seam 3 and I am having issues injecting the EntityManager with the #Inject annotation. I am pretty sure there is some kind of configuration to make sure the EnityManager knows which PersistenceUnit to use. For example with EJB you can type:
#PersistenceContext(unitName="MY_PERSISTENCE_UNIT_NAME")
private EntityManager eManager;
which persistence unit is configured in the persistence.xml file. Here is my pseudo configuration:
<?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="MY_PERSISTENCE_UNIT_NAME" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/TimeReportDS</jta-data-source>
<mapping-file>META-INF/orm.xml</mapping-file>
<class>....</class>
<class>....</class>
<class>....</class>
<properties>
<property name="jboss.entity.manager.factory.jndi.name"
value="java:/modelEntityManagerFactory" />
<!-- PostgreSQL Configuration File -->
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.connection.url" value="jdbc:postgresql://192.168.2.125:5432/t_report" />
<property name="hibernate.connection.username" value="username" />
<!-- Specifying DB Driver, providing hibernate cfg lookup
and providing transaction manager configuration -->
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<property name="hibernate.archive.autodetection" value="class" />
<!-- Useful configuration during development - developer can see structured SQL queries -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
I have read some articles about Seam 2 but there the configuration is made in components.xml file by adding the:
<persistence:managed-persistence-context
name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/modelEntityManagerFactory" />
inside the <components> tag. The next step in Seam 2 is to add the:
<property name="jboss.entity.manager.factory.jndi.name"
value="java:/modelEntityManagerFactory" />
in the persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence ...>
<persistence-unit name="MY_PERSISTENCE_UNIT_NAME" ...>
...
<properties>
...
<property name="jboss.entity.manager.factory.jndi.name"
value="java:/modelEntityManagerFactory" />
</properties>
</persistence-unit>
</persistence>
but it seam that in Seam 3 there is no file components.xml. Also there is no attribute unitName in #Inject annotation to specify the persistence unit.
So please help me to configure my project so I can use the #Inject with EntityManager as shown in many examples on the net.
I use Postgres database and JBoss AS 7.
EDIT: Adding an example. I don't use the EntityManager in an Entity class.
#Named("validateReportAction")
#SessionScoped
public class ValidateReportAction extends ReportAction implements Serializable {
private static final long serialVersionUID = -2456544897212149335L;
#Inject
private EntityManager em;
...
}
Here in this #Inject I get warning from Eclipse "No bean is eligible for injection to the injection point [JSR-299 §5.2.1]"
If I use the #Inject on some beans that are marked as Entity the #Inject works fine.
You can use #PersistenceContext on a CDI bean. It doesn't have to be an EJB.
If for some reason you want to use #Inject, you have to do more work. #Inject doesn't know about the EntityManager; it can only inject other managed beans. Happily, there is a simple workaround - use a producer method that acts as a simple trampoline.
#ApplicationScoped
public class EntityManagerProducer {
#PersistenceContext
private EntityManager entityManager;
#Produces
#RequestScoped
public EntityManager getEntityManager {
return entityManager;
}
public void closeEntityManager(#Disposes EntityManager em) {
if (em != null && em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (em != null && em.isOpen()) {
em.close();
}
}
}
You can now use #Inject to inject the EntityManager. The injected EntityManager will be RequestScoped, while the EntityManagerProducer is ApplicationScoped. Furthermore, the entityManager must be closed.
Is possible do injection in EntityManager with EclipseLink 2.3 ?
This is my persistence.xml file:
<?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="SuaParte" transaction-type="RESOURCE_LOCAL">
// classes..
<properties>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/schema"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
I'm new with JPA so I created this persistence.xml file at first just to test to see if it works, but now I would like to use #PersistenceContext to don't have to worry about manage the EntityMangerFactory and EntityManager.
I'm using Eclipse Indigo Java EE Web Developers with GlassFish v3.
UPDATE:
I follow #Andrei Bodnarescu approach and this tutorial too and everything is fine to get a connection with my database through GF3:
And i change my persistence.xml file:
<?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="SuaParte" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/mysql</jta-data-source>
//classes..
</persistence-unit>
</persistence>
So i try to persist something in my database:
#Stateless
#LocalBean
public class DaoUser {
#PersistenceContext(unitName="SuaParte")
private EntityManager em;
public void persist(User user){
try{
em.persist(user);
}catch(Exception e){
e.printStackTrace();
}
}
}
And it returns a java.lang.NullPointerException in em.persist(user);.
What am I doing wrong ?
You have to change the transaction-type to JTA as follows -
<persistence-unit name="SuaParte" transaction-type="JTA">
And then you can use #PersistenceContext as follows if you want Glassfish to inject the EntityManager into your EJB -
#Stateless
#LocalBean
public class MyEjb {
#PersistenceContext("SuaParte")
private EntityManager suaParteEM;
}
You can do in Servlet and ManagedBean too.
Also, on your Glassfish server you can create JDBC Connection Pool and JDBC Resource, give it a JNDI name and use it to declare your EntityManager in the persistence.xml as follows -
<persistence-unit name="SuaParte" transaction-type="JTA">
<jta-data-source>JNDI_Name_Of_JDBC_Resource</jta-data-source>
You can create the JDBC Resources on from the Glassfish Admin-Console.
BheshG's answer is very good, here's just some small addendums.
i think that in order to activate CDI and thus have dependency injection you need to create an empty beans.xml file that you must place in the WEB-INF folder of your project
To create a data source in GF3 and expose it via JNDI like BheshG sais, you must to basically this:
resources->JDBC->JDBC Connection pools and make a connection pool
Create a datasource that will use the pool.
(I wanted to post images with examples for conenction pool and datasource, but I can't since I don't have reputation points. In order to not seem spammy and trolly, I'm just gonna say: email me if you want more details or an example project, as I'm working on a tutorial on exactly that right now)
Now you can use that datasource in the persistence.xml file to attach it to a persistence unit:
<persistence-unit name="emJTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/postgre_ds_jta</jta-data-source>
<mapping-file>META-INF/emp-mappings.xml</mapping-file>
<class>model.Employee</class>
<class>model.ProjectManager</class>
<class>model.Department</class>
<class>model.Project</class>
<class>model.Phone</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
I'm getting an NPE for my EntityManager and can't figure out why - can anyone spot my mistake?
UserManagerImpl.java
#Stateless
public class UserManagerImpl implements UserManager {
#PersistenceContext(unitName = "persistenceUnit")
EntityManager em;
public List<User> findUsers() {
return (List<User>) em.createQuery("from User").getResultList();
}
}
META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_1_0.xsd" version="1.0">
<persistence-unit name="persistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
These are inside my ejb jar project, the UserManagerImpl is invoked from a JSP, not sure that should make a difference though.
Thanks in advance.
When you are using new UserManagerImpl() the container has no way to inject the EntityManager into this class. You need to let the container inject an instance into your object using
#EJB
private UserManager userManager;
This will only work for container managed objects like JSF managed beans, servlets or JSPs. Alternatively you can look up the ejb from jndi using something like
new InitialContext().lookup("UserManagerImpl");
The actual jndi name might be different.