I m trying to find good reference for how to do JPA project (with EJB 3.x).
Its been very difficult for me to find how to configure persistance unit according to jBoss. (in GlassFish its just a piece of cake).
is anyone of you know good reference
I just ported my app that I developed on NetBeans from Glassfish 3 to JBoss 6. I had to disable JTA for the persistance to work but since I don't need that at the moment something is better than nothing.
I am using container managed security and documented my configuration in this post.
I configured my JBoss 6 datasource by placing mysql-ds.xml into $JBOSS_HOME\server\default\deploy. Here are contents:
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/thor_ds</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/thor</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>mjolnir</user-name>
<password>mjolnir</password>
<exception-sorter-class-name>
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
Unlike Glassfish, in JBoss 6, I had to declare each of my persistence classes explicitly. 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="AvengersPU" transaction-type="RESOURCE_LOCAL">
<!--<persistence-unit name="AvengersPU" transaction-type="JTA">-->
<jta-data-source>java:/jdbc/thor_ds</jta-data-source>
<non-jta-data-source>java:/jdbc/thor_ds</non-jta-data-source>
<class>avenger.Grouptable</class>
<class>avenger.MyUser</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
There is no difference between application servers in terms of JPA. Just do the same thing you did for Glasfish.
The only difference is configuring the DataSource - but there are enough google results for that.
After configuring your persistence.xml you should be ready to go as JPA is a Java EE standard and should be application server independent.
Related
i was going through the hibernate examples, in some examples persistence.xml was used in some examples hibernate.cfg.xml was used. what is difference between these two files and how these files can affect our project behavior and in which scenario which file will be the best.
i don't have any issue with either of these files, i just want know that is there any internal behavior change in project when we use these files.
in persistence.xml we have
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="hibernate" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.test.jpa.Student</class>
<properties>
.....property tags
</properties>
</persistence-unit>
</persistence>
in hibernate_cfg.xml file we have
<hibernate-configuration>
<session-factory>
......property tags
</session-factory>
</hibernate-configuration>
both of my project run fine when i use these files, i just want to know the difference b/w these files.
persistence.xml is the configuratoin file from JPA standard and hibernate.conf.xml is the Hibernate specific file.
It's recommended to use JPA standard as far as possible to be independent from the underlying JPA implementation.
I got following local setting:
Eclipse Kepler
Maven 3
Glassfish 4
I want to run JSF with MySql.JSF 2.0 works fine on the server.The problem I got is the connection to the database.
I did all settings at the admin of glassfish and here is the persistence.xml I got so far:
<?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="actors" transaction-type="JTA">
<jta-data-source>jdbc/example</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
Maven dependency for MySQL:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
Get a jdbc not found exception...
The question is how can I setup Glassfish to use the maven dependency so that I don't need to integrate the jdbc.jar manually ?
I am afraid according to the official documentation you have to do it this way
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.