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.
Related
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)
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.
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?
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.
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>