Setting up Play Framework 2.5.x with Hibernate OGM Neo4j - jpa

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>

Related

JPA persistence.xml not found

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.

Cannot create EntityManager from valid persistence.xml

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.

TomEE Plus and JPA

I am trying to set up JPA in my TomEE Plus. I have got my persistence.xml as below
<persistence-unit name="test" transaction-type="JTA">
<jta-data-source>jdbc/testDB</jta-data-source>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.TestEntity</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
When i deploy my App, i can't see any errors in the console. I can also see the logs which binds the PU to the JNDI.
But no tables are created in the DB.
Can some please help me in this?
If you are using the default JPA implementation shipped with TomEE (OpenJPA) rather than Hibernate, you should:
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
If <provider> is not specified in the persistence.xml ,then the container will use its default persistence provider .For TomEE ,its default persistence provider is OpenJPA.
The <properties> should match the persistence provider you are using .
So , if you are using OpenJPA , your persistence.xml should look like this: (OpenJPA 's <properties> can be found at here)
<persistence-unit name="test" transaction-type="JTA">
<jta-data-source>jdbc/testDB</jta-data-source>
<class>com.TestEntity</class>
<properties>
<property name="openjpa.jdbc.DBDictionary" value="mysql"/>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
</properties>
</persistence-unit>
If you want to use hibernate as the persistence provider , your persistence.xml should look like this:
<persistence-unit name="test" transaction-type="JTA">
<jta-data-source>jdbc/testDB</jta-data-source>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.TestEntity</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>

Database settings in persistence.xml not used

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

eclipselink does not generate tables from annotated JPA classes

My IDE is eclipse -Helios and I am using mojarra jsf, mysql, eclipselink for jpa.
In my project, if I create the tables manually in mysql, I can see those tables in the "JPA Details" view. And if I don't create any table, the eclipse IDE shows an error, "Table "trainingsession" cannot be resolved".
I am not sure what's wrong. When would JPA create these tables ? and how ?
my persistence.xml is as follows,
<?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="wompower2" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>trainer</jta-data-source>
<class>com.jsfcompref.trainer.entity.User</class>
<class>com.jsfcompref.trainer.entity.TrainingSession</class>
<class>com.jsfcompref.trainer.entity.Event</class>
<class>com.jsfcompref.trainer.entity.AbstractEntity</class>
<validation-mode>NONE</validation-mode>
<properties>
<property name="eclipselink.target-database" value="MySQL"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
<property name="eclipselink.application-location" value="C:\wompower2\DDL"/>
<property name="eclipselink.create-ddl-jdbc-file-name" value="create.sql"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/demo"></property>
<property name="javax.persistence.jdbc.user" value="user"></property>
<property name="javax.persistence.jdbc.password" value="pwd"></property>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"></property>
</properties>
</persistence-unit>
</persistence>
Thank you,
Arindam.
First let me clarify that JPA is a standard spec for ORM and EclipseLink is one of the implementor of the spec (Hibernate is another example). The spec doesn't mandate the creation of the schema or tables though EclipseLink provides a mechanism to create the tables for you through configuration. Below are the two config properties controlling that
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
Go through this tutorial for more information (specifically 3.2 section)
GlassFish reads the property eclipselink.ddl-generation to decide whether it will use its own table generation functionality -- java2db. This only works during deployment and if the target is the DAS.
It doesn't matter the value of eclipselink.ddl-generation.output-mode you give, GlassFish will set it to "sql-script" if java2db is to be used (so that it can then run the scripts) or "none" if it is disabled. See glassfish3/persistence/jpa-connector/src/main/java/org/glassfish/persistence/jpa/PersistenceUnitLoader.java.
I saw "Table 'nnn' cannot be resolved" being reported after a couple of tests I made with JPA, MySQL, Eclipse.
My issue was caused by myself. I switched the data connection during my tests. But the tool - I assume I was the JPA plugin - was continuing to validate the table names against the first data connection i defined.
So, my solution was:
Open the project specific JPA properties (right click project -> JPA) and ensure that the connection settings refer to the correct database. Rebuild... that's it.
This is a bit late, but just in case anybody comes across this. You just need to run a query like an INSERT or a SELECT against the database and the tables will be created. It has worked for me before. I hope this help anybody with the same issue.
Just a persistence.xml example using eclipselink and mysql for others looking for a working solution:
<?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="java2curs" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>db.YourClasses</class>
<properties>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/dbname"/>
<property name="javax.persistence.jdbc.user" value="dbuser"/>
<property name="javax.persistence.jdbc.password" value="dbpassword"/>
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>