I've been briefly looking at JPA recently, and I was wondering what the deal is with database schema migrations and staying lined up with the classes you've created.
Is there support in JPA for this stuff? Utilities? Best Practises?
Cheers!
I won't rely on JPA providers to update the database schema.
Check Liquibase for one of the good approaches.
The short answer is no.
If you change your beans, then you will have to migrate the existing schema by hand. So for Rails style database migrations you will have to look elsewhere.
You can however generate the initial ddl from your Java beans easily. The example below illustrates schema creation with EclipseLink version 2.0:
<?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="JPATestPU" transaction-type="RESOURCE_LOCAL">
<provider>
org.eclipse.persistence.jpa.PersistenceProvider
</provider>
<class>org.randompage.MyEntity</class>
<properties>
<property name="javax.persistence.jdbc.user" value="johndoe"/>
<property name="javax.persistence.jdbc.password" value="secret"/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:~/.h2/testdb;FILE_LOCK=NO"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
The key element here is
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
This tells EclipseLink to drop existing tables and generate new once from your JPA mapping. This procedure is highly vendor specific so for other JPA vendors (Hibernate, OpenJPA...) you will have to consult their specific documentation.
If you set the generateDdl to Hibernate (if it is the underlying implementation), then it generates the database schema according to your current dialect. So after changing the dialect it will automatically generate the database.
Other JPA providers may have different properties for this.
Related
I am creating a small JPA project.
But I am receiving the below error, when I run the main class.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named ClientAccount
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
at com.infinite.ClientAccountMain.main(ClientAccountMain.java:12)
the name in the persistence unit is the same used in the entity manager factory
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ClientAccount");
<?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="ClientAccount">
<description>My Persistence Unit</description>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.infinite.Order</class>
<class>com.infinite.Account</class>
<class>com.infinite.Client</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:3306;databaseName=testdatabase" />
<property name="javax.persistence.jdbc.user" value="test"/>
<property name="javax.persistence.jdbc.password" value="test"/>
</properties>
</persistence-unit>
I think that Neil Stockton is right and if you search a bit you will find your answer. Although I think that must be something in relation with your provider information.
Try to put something like this.
For OpenJPA
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
Source information ---> http://openjpa.apache.org/builds/1.0.3/apache-openjpa-
1.0.3/docs/manual/jpa_overview_persistence.html
For Hibernate
<provider>org.hibernate.ejb.HibernatePersistence</provider>
There are multiple causes for this error:
You do not have the specified JPA provider on your classpath (check the JARs).
You did not define the persistence unit in your persistence.xml file.
Your persistence provider did not find your persistence.xml file (usually it is in META-INF/persistence.xml). Check that it is on the right place.
I'm trying to integrate Batoo JPA in one of my projects using gradle and Jetty as a server.
What libraries do I have to integrate? Is there a sample available?
Currently I have these:
'org.batoo.jpa:persistence-api:2.0',
'javax.validation:validation-api:1.0.0.GA',
'com.jolbox:bonecp:0.8.0-rc1'
But these appear not to be enough. Before going further with "trial and error" I'd wanted to ask here at stackoverflow first, what libraries I need to get started with Batoo Jpa (together with gradle and Jetty).
Thanks
In one basic project i made, i had to configure these libraries:
-batoo-annotations-2.0.1.0-RTM.jar
-batoo-annotations-2.0.1.0-RTM-sources.jar
-batoo-jdbc-2.0.1.0-RTM.jar
-batoo-jpa-2.0.1.0-RTM.jar
-batoo-jpa-spi-2.0.1.0-RTM.jar
-jpql-0.1.6.jar
-parser-2.0.1.0-RTM.jar
-persistence-api-2.0.jar
-guava-14.0.1.jar
-commons-lang-2.6.jar
-validation-api-1.0.0.GA.jar
-bonecp-0.7.1.RELEASE.jar
-commons-dbutils-1.5.jar
-commons-io-2.4.jar
-asm-3.3.1.jar
-h2-1.3.171.jar <-- I add this one as database driver it could be changed.
If you have problem with transactions (if i remember correctly Batoo raise exceptions if you don't have a transaction control, but you can test it) i configured these libraries in order to have a CDI transaction control, but you can omit these if you want to use spring or Batoo does works well without a transaction control :-)
-deltaspike-cdictrl-api-0.3-incubating.jar
-deltaspike-cdictrl-weld-0.3-incubating.jar
-deltaspike-core-api-0.3-incubating.jar
-deltaspike-core-impl-0.3-incubating.jar
-deltaspike-jpa-module-api-0.3-incubating.jar
-deltaspike-jpa-module-impl-0.3-incubating.jar
-weld-api-2.0.0.jar
-weld-spi-2.0.0.jar
-weld-se-2.0.0.jar
Now, remember that Batoo uses standard properties in the persistence.xml file, like this:
<?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="your PU name" transaction-type="RESOURCE_LOCAL">
<provider>org.batoo.jpa.core.BatooPersistenceProvider</provider>
<class>here.you.add.your.Entities</class>
<properties>
<!-- here your driver-->
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<!-- here the URL of your database-->
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test" />
<!-- here your username-->
<property name="javax.persistence.jdbc.user" value="sa" />
<!-- here your password-->
<property name="javax.persistence.jdbc.password" value="" />
</properties>
</persistence-unit>
Hope this helps, cheers :-)
I'm using Eclipse Juno, Glassfish 3.1.2, and MySQL 5.1.
I'm building a simple EJB & JSF application. I created the following eclipse projects:
appEAR <-- the EAR file
appEJB <-- contains UserService.java EJB
appJPA <-- contains UserDAO.java EJB, and User.java object
appWeb <-- contains index.jsp
It's just a skeleton right now, but I can deploy the app and see the index.jsp
Next, I tried to add the following to the UserDAO ...
#PersistenceContext
EntityManager em;
But then when the app tries to republish, it gives me the error:
'Publishing to GlassFish 3.1.2 at localhost...' has encountered a problem. cannot Deploy appEar
There are no other details.
When I remove the two lines of #PersistenceContent code, the app deploys again.
Also, the persistence.xml file n the appJPA project 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="appJPA">
<class>app.model.User</class>
</persistence-unit>
</persistence>
Please help ... what am I missing? I'm rather stuck.
Your persistence.xml is incomplete , you need to provide Connection properties to specify the provider ,which DB to connect etc
Heres an example using hibernate as the JPA provided
<persistence-unit name="educationPU"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.coe.jpa.StudentProfile</class>
<properties>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/COE" />
<property name="hibernate.connection.username" value="root" />
<property name="show_sql" value="true" />
<property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
and heres a more Generic one
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>
I don't use glassfish. But I think the reason is that you didn't specify any datasource in the persistence.xml. you should do this in it, which you can use jndi or other way. and second, you should define the entityManagerFactory bean in spring context xml file.
Did you add the datasource in glasfish ? You will need to add the mysql jdbc drivers too. In Java EE, it's the persistence container (inside the server) which will create and manage the datasource for you.
See http://www.albeesonline.com/blog/2008/08/06/creating-and-configuring-a-mysql-datasource-in-glassfish-application-server/
All,
I'm trying to configure Google App Engine to work with my local MySQL database instance and JPA using Hibernate.
After setup I had a socket issue that I realized is discussed before (http://stackoverflow.com/questions/10585140/gae-cloudsql-with-mysql-access-denied) so I followed the instructions and tried to resolve my issue. however for some reason eclipse is doing something annoying and wipes out my changes made to persistence.xml file as it's instructed in the mentioned post.
To test this I installed a fresh Eclipse (Juno) and loaded the google plugin.
I create a test application
I add JPA to is and set it up to use hibernate.
I google app property I make sure it's using local mysql
So far so good. When I look at the generated persistence.xml I see 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="transactions-optional" transaction-type="RESOURCE_LOCAL">
<provider></provider>
<properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/shelem?user=root&password=gandom"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="gandom"/>
</properties>
</persistence-unit>
</persistence>
As you can see the provider is set to nothing (wrong) and URL and Driver attributes are also wrong (as per mentioned post).
If I run the application as is I'll get the socket error exception mentioned in the above post so I manually change the persistence.xml file to look like this (as per above post):
<?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="transactions-optional" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
<property name="javax.persistence.jdbc.driver" value="com.google.appengine.api.rdbms.AppEngineDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:google:rdbms://localhost/Guardian"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="gandom"/>
</properties>
</persistence-unit>
</persistence>
Then the crazy thing happens! As soon as I do this and I clean/build the application so I can start it Eclipse wipes out my changes and turn the persistence.xml file to look like what it was before my manually made changes and .....
Interestingly if I add comments or change anything else those changes are kept and are not lost so it seems for some reason eclipse only replaces the code that was suggested to fix my issues!!!
It's been driving me crazy, anyone has seen this? Any suggestions?
Thanks for your comments.
Amir
It seems at least with version 1.7 of the app engine you can not use anything but EclipseLink as the persistence provider. Using EL is not a major concern as regardless of what is used to access your local database Google uses its own provider on the app engine server farms.
That said it's maybe even for the best to use EclipseLink for local development as well since any persistence provider specific logic you may embed in your code that is not compatible with google's provider would eventually come back and break your code after deployment so safer approach is to use EclipesLink on local.
Hope this helps others so they don't waste time as I did.
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>