#PersistenceUnit annotation not working - java

I want to use the #PersistenceUnit annotation in my app to create an application managed EntityManager
#PersistenceUnit(unitName="primary")
private static EntityManagerFactory entityManagerFactory;
EntityManager entityManager = entityManagerFactory.createEntityManager();
This doesn't seem to be working. I run my code through a debugger and discover that entityManagerFactory is null. My guess is that the injection of Persistence context with the #PersistenceUnit annotation is not working.
My app is a CDI app. It was not previously a CDI application - I converted it to CDI by creating a beans.xml file in WEB-INF, I needed to in order to do something like this.
Is there anything I need to configure within CDI to get the annotation to work? Thanks.

I have a JPA application running with only Java SE. I don't have a WEB-INF/beans.xml, but I do have a META-INF/persistence.xml configuration 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="JPAPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>jpa.Container</class>
<class>jpa.Item</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:D:\NetBeansProjects\JPA\jpaTestDB;create=true"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Container and Item are the two persistence classes in my jpa package.
This was generated automatically by Netbeans. There is also some information about using JPA without Java EE in the official (Sun/Oracle) Java EE tutorial in the persistence chapter.

Related

WAS Liberty 8.5.5.4, JPA 2.0 - discovered implementations: NONE

I was able to get JPA working with eclipse Luna and installing eclipseLink (and created a User Lib with the 3 jars).
However, I have now moved to Rational Application Developer (RAD) and attempting to use the JPA that is delivered with WAS Liberty. I initially used RAD JPA facet to configure JPA and then edited persistence.xml.
I am getting this error:
Caused by: javax.persistence.PersistenceException: No persistence providers available for "test2" after trying the following discovered implementations: NONE
I assume (always dangerous) the NONE is a configuration error and I cannot do anything until that is resolved...
I have copied the wlp\dev\api\spec\com.ibm.ws.javaee.persistence.2.0_1.0.1.jar into WEB-INF/lib and included that in my java build path.
As you can see below, I was guessing my provider was not correct, and tried several values...
<?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="test2" transaction-type="RESOURCE_LOCAL">
<!-- provider>org.eclipse.persistence.jpa.PersistenceProvider</provider-->
<!-- provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider-->
<provider>com.ibm.websphere.persistence.PersistenceProviderImpl</provider>
<non-jta-data-source>java:comp/env/New Generic JDBC</non-jta-data-source>
<class>com.ibm.analytics.jpa.Dimtask</class>
<properties>
<property name="openjpa.jdbc.Schema" value="RM"/>
<property name="javax.persistence.jdbc.url" value="jdbc:netezza://netezza-1.site.ibm.com:5480/DATABASENAME"/>
<property name="javax.persistence.jdbc.user" value="jspoon"/>
<property name="javax.persistence.jdbc.password" value="xxxxxxx"/>
<property name="javax.persistence.jdbc.driver" value="org.netezza.Driver"/>
</properties>
</persistence-unit>
</persistence>
What else can I do to get past the NONE?
Here is the updated version of persistence.xml, I just commented out the provider.
<?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="test2" transaction-type="RESOURCE_LOCAL">
<!-- provider>org.eclipse.persistence.jpa.PersistenceProvider</provider-->
<!-- provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider-->
<!-- provider>com.ibm.websphere.persistence.PersistenceProviderImpl</provider-->
<non-jta-data-source>java:comp/env/New Generic JDBC</non-jta-data-source>
<class>com.ibm.analytics.jpa.Dimtask</class>
<properties>
<property name="openjpa.jdbc.Schema" value="RM"/>
<property name="javax.persistence.jdbc.url" value="jdbc:netezza://netezza-1.boulder.ibm.com:5480/BACC_TST_ISHANGO_DW"/>
<property name="javax.persistence.jdbc.user" value="jspoon"/>
<property name="javax.persistence.jdbc.password" value="C0ke4y0u"/>
<property name="javax.persistence.jdbc.driver" value="org.netezza.Driver"/>
</properties>
</persistence-unit>
</persistence>
I created my Entity class using JPA Tooling, and using a simple junit test to call a named query. The first step is to create the EMF and it is a static in my class (so none of the test is actually running, it is failing at instantiation creating the static variable):
static EntityManagerFactory emf = Persistence.createEntityManagerFactory( "test2" );
This is the exact same code as used before with eclipse and eclipseLink.

Java persistence entity manager factory returning null

I am using MySql database with JPA. my persistence.xml looks like
<?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="citheph">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>persistence.citheph.pTerm</class>
<class>persistence.citheph.pPublicationterm</class>
<class>persistence.citheph.pPublicationauthor</class>
<class>persistence.citheph.pPublication</class>
<class>persistence.citheph.pCoauthorship</class>
<class>persistence.citheph.pCitation</class>
<class>persistence.citheph.pAuthor</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cithepth" />
<property name="javax.persistence.jdbc.password" value="amer1" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
</properties>
</persistence-unit>
</persistence>
in my java code i do the following
EntityManagerFactory emf = Persistence.createEntityManagerFactory("citheph");
EntityManager em = emf.createEntityManager();
emf always come back null. the database is connected and pings successfully. I have even tried to change the persistence-unit name to something that doesn't match the entry in the persistance.xml and I am still getting null. It is not throwing any exceptions. ofcourse when it gets to the second line in the code
EntityManager em = emf.createEntityManager();
I get a null pointer exception.
any help will be greatly appreciated.
Turn logging on finest and ensure no errors are occurring loading the persistence unit.
What environment are you using, do you have all the required jars on your classpath?
Are you sure that the persistence.xml files resides in the META-INF folder?

How do injection with JPA?

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>

JPA Entity found in unit tests, not in EJB

I have a DAO that uses JPA with a non-JTA data source and RESOURCE_LOCAL transactions. All of my unit tests that exercise the DAO work perfectly (data is inserted and retrieved from the database).
When I deploy my EJB to my Weblogic 10.3.3 server, however, I get the following exception:
Caused by: java.lang.IllegalArgumentException: Unknown entity bean class: class com.foo.bar.CatalogEntity, please verify that this class has been marked with the #Entity annotation.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.find(EntityManagerImpl.java:576)
My persistence.xml (in WEB-INF/classes/META-INF):
<?xml version="1.0" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="cmf-awe-service" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>MyDataSource</non-jta-data-source>
<class>com.foo.bar.CatalogEntity</class>
<properties>
<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.query-results-cache" value="false"/>
<property name="eclipselink.target-server" value="WebLogic_10" />
<property name="eclipselink.logging.level" value="FINEST" />
</properties>
</persistence-unit>
</persistence>
I have confirmed that the CatalogEntity class is in the WEB-INF/classes directory. Any ideas about why this works in unit tests but not when deployed to the application server?
Brian, I had the same issue here... and I solved by adding a PreDestroy method on my bean that closes EntityManagerFactory:
#PreDestroy
public void close()
{
emf.close();
}
I hope this can help you! Please vote if it works...

how inject a EntityManager between ear

I'm using JBoss 4.2.3 and I deployed two ears called triad-1.0.ear and reportservices-1.0.ear, the thing is that I want to use the entity manager of the project triad in the project reportservices. This is the architecture JBoss follows:
triad-1.0.ear:
triad-core-1.0.jar:
META-INF:
MANIFEST.MF
components.xml
ejb-jar.xml
jboss.xml
persistence.xml
reportservices-1.0.ear:
reportservices-core-1.0.jar:
META-INF:
MANIFEST.MF
components.xml
ejb-jar.xml
jboss.xml
persistence.xml
this is my attempt to make the entitymanager global between ear in the persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Attention: Generated code! Do not modify by hand!
Generated by: persistence.xml.vsl in andromda-ejb3-cartridge.
-->
<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="triad">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jdbc/triad</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
<property name="jboss.entity.manager.jndi.name" value="java:/triadFactory"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/triadFactory"/>
</properties>
</persistence-unit>
</persistence>
I'm sorry but the question doesn't make much sense in my opinion (I don't even get what you mean by "make public the persistence.xml"). Just in case, it is possible:
to use the same datasource in several persistence units.
to (re)use a persistence unit in several applications (provided the packaging make it possible).
My suggestion would be to explain what you would like to do in plain english (forget the technical details for now).
i finally solved mi problem with the injection, you need to set this properties in the persistence.xml and check in jboss jmx console, in the option of jdni view if the injection was correct the properties here's an example of the 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_1_0.xsd" version="1.0">
<persistence-unit name="example">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jdbc/example</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
<property name="jboss.entity.manager.jndi.name" value="java:/example"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/example"/>
</properties>
</persistence-unit>
</persistence>`
the properties that allowing the injection are "jboss.entity.manager.jndi.name" and "jboss.entity.manager.factory.jndi.name"
notes the data have the same name that the data source but called by his jdni name sets in the xml of the datasource project.

Categories