I'm trying to use JPA from Play 2.0 Scala application. In my code I have:
val factory = Persistence.createEntityManagerFactory("devcrowd")
I also have persistence.xml in package META-INF:
<?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_2_0.xsd" version="2.0">
<persistence-unit name="devcrowd" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="xxxx"/>
<property name="hibernate.connection.password" value="xxxx"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/devcrowd"/>
</properties>
</persistence-unit>
</persistence>
This persistence.xml seems valid to me, but the factory cannot be created:
PersistenceException: No Persistence provider for EntityManager named devcrowd
You are missing the Hibernate library/ies at deploy time.
Related
I am trying to setup Hibernate OGM to work with Play Framework 2.5.x(17) in my case but I keep getting "Cannot connect to database [default]" error. Apparantly Play takes MySQL driver as default and I am not able to find a driver configuration specifically for Neo4J.
Here is my persistance.xml file content:-
<?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="defaultPersistenceUnit" transaction-type="JTA">
<!-- Use Hib77ernate OGM provider: configuration will be transparent -->
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.transaction.jta.platform"
value="JBossTS" />
<property name="hibernate.ogm.datastore.provider" value="neo4j_http"/>
<property name="hibernate.ogm.datastore.host" value="localhost:7474"/>
<property name="hibernate.ogm.datastore.username" value="neo4j"/>
<property name="hibernate.ogm.datastore.password" value="neo4j"/>
</properties>
</persistence-unit>
</persistence>
And application.conf content:
db.default.jndiName=DefaultDS
jpa{
default=defaultPersistenceUnit
}
Any help is appreciated. Thanks in advance.
I was facing the same problem then I found one solution that we can use neo4j jdbc driver.
application.conf :-
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit
db.default.driver=org.neo4j.jdbc.Driver
db.default.url="jdbc:neo4j:http://localhost"
db.default.user= neo4j
db.default.password="password"
persistence.xml
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<class>domain class name</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="JBossTS" />
<property name="hibernate.ogm.datastore.provider" value="neo4j_http"/>
<property name="hibernate.ogm.datastore.host" value="localhost:7474"/>
<property name="hibernate.ogm.datastore.username" value="neo4j"/>
<property name="hibernate.ogm.datastore.password" value="password"/>
</properties>
</persistence-unit>
I know this question has been asked before but I still can't get JPA to work with my project.
I am getting the PersistenceException No provider found.
I have the persistence.xml in my src/main/resources/META-INF folder and I am using m2 for building but the persistence file is not moved to the build directory.
Here is my persistence.xml
<?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="test" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.entities.User</class>
<class>com.entities.Group</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/BandwidthX"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
</properties>
</persistence-unit>
And I am trying to persist using this code:
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("test");
EntityManager em = emf.createEntityManager();
The persistence.xml should be inside the src/main/resources/META-INF folder for the build to work correctly.
See Section 8.2.1 of the JPA spec.
I am very new to glassfish, JPA and so on and I have really problems with setting that up. What I am planning to do is a simple RESTful service with a persistent backend. I am using glassfish3 as application server and already deployed a simple REST service with the jersey-library. Now I want to provide access to a database via JPA. Glassfish is shipped with JavaDB/derby and EclipseLink, is that right? So, I want to use that :-)
I created a persistence.xml in META-INF:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="myPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDataSource" /> <!-- org.apache.derby.jdbc.EmbeddedDriver -->
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample;create=true" />
<property name="javax.persistence.jdbc.user" value="APP" />
<property name="javax.persistence.jdbc.password" value="APP" />
<property name="eclipselink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
</persistence>
Then I created a field in my resource, where I want to access/store som data:
#PersistenceUnit(unitName = "myPU")
EntityManagerFactory emf;
But "emf" is always NULL :-(
I guess that my persistence.xml is not configured appropriate.
Would be really glad if someone has a hint, what I am doing wrong...
thanks!
I think it is better to create JNDI for db connection . You can do it easly with GlassFish.
Firstly create connection pool (you will set db connection settings);
Resources->JDBC->JDBC Connection Pools
After that crate JNDI name for this pool ;
Resources->JDBC->JDBC Resources
So lets say you set JNDI name as "dbCon"
And here your persistence.xml ;
<?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="myPU" transaction-type="JTA">
<jta-data-source>dbCon</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Note : You must copy your jdbc jar to \glassfish-3.1.1\glassfish\domains\domain1\lib\ext
I have the solution now for my problem.
Here is the corresponding configuration:
glassfish 3.1.1
built-in JavaDB/derby database: jdbc/__default
glassfish's JPA, which is eclipselink
(JAX RS: Jersey, which is shipped with glassfish)
So, you have to create the folder "META-INF" wihtin your src folder and put the persistence.xml there:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="myPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/__default</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
</properties>
</persistence-unit>
</persistence>
I created the .xml previously in the META-INF of WebContent, and that is wrong.
You also do not have to reference any additional libraries, since you have the glassfish modules added.
Now I have created a JavaBean, where I do inject the PersistenceUnit:
#Stateless
public class StorageService {
#PersistenceContext(unitName = "myPU")
EntityManager em;
...
}
And this one is injected in my Resource-Classes of the Jersey-Servlets:
#Path("/someres")
#Produces(MediaType.APPLICATION_XML)
#Stateless
public class SomeRes {
#EJB
StorageService storageService;
...
}
The injections do only work if the classes are marked as "#Stateless".
I have not tried with RESTful service, but I guess that should not matter. I noticed you are using persistence.xml for version 1. Any specific reason?
Following persistence.xml works for me:
<?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="myPU">
<properties>
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
Hope this helps.
I need to make some tests with JPQL, so I'm trying do that with Hibernate Tools, but when I try open the session factory appears this : Could not locate TransactionManager as showed below:
Here 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">
<jta-data-source>jdbc/suaparte_ds</jta-data-source>
<class>entity.Area</class>
//classes..
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<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.connection.datasource" value="jdbc/suaparte_ds"/>
</properties>
</persistence-unit>
</persistence>
I had the same problem and after a long search for solution I found one publication.
I tried the advice given in the link Hibernate tools: Could not find datasource and it worked.
I'm new to JPA, and to try to teach myself, I'm setting up a tiny web application and deploying to Glassfish 3.1.
JPA works fine when I refer to a JNDI DataSource in persistence.xml, such as this:
<?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="foo" transaction-type="JTA">
<jta-data-source>jdbc/foo</jta-data-source>
<class>my.app.Foo</class>
</persistence-unit>
</persistence>
But as far as I understand, it is supposed to be possible to put all my database connection settings into properties in persistence.xml. This may not be good practice, but it seems like it could be handy when I'm just experimenting, and perhaps during unit testing.
However, when I follow the examples I have found for this, persistence.xml seems to be just ignored and instead the default container-managed DataSource, jndi/__default is used. This is a Derby instance that is not running.
I've tried this file for an ephemeral in-memory Derby instance:
<?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_2_0.xsd"
version="2.0">
<persistence-unit name="foo" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>my.app.Foo</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:NxtMv;create=true"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
I have also tried this for a PostgreSQL server (which works when accessed through JNDI):
<?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_2_0.xsd"
version="2.0">
<persistence-unit name="foo" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>my.app.Foo</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/foo"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.user" value="myuser"/>
<property name="javax.persistence.jdbc.password" value="secret"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
There's probably some irrelevant cruft in those files which has accumulated during my countless tries and retries.
What am I missing here?
You cannot use manually configured datasource with transaction-type="JTA".
JPA Spec says:
A transaction-type of JTA
assumes that a JTA data source will be provided—either as specified by the jta-data-source element
or provided by the container.
Try to use transaction-type="RESOURCE_LOCAL" instead (though I'm not sure how would it work with container-managed transactions, if you use them).