glassfish-resource.xml and persistence unit in eclipse ang glassfish v3.0 - eclipse

I created a Web project in eclipse and adding a glassfish-resource.xml init and configure it with my database url.
also I added a persistence.xml unit in my project and add a jta-datasource for my datasource defined in glassfish-resource.xml but when the application is being deployed on glassfish in ide I got error that :
Lookup failed for 'java:module/jdbc/sportal' in SerialContext
my glassfish-resource.xml:
<resources>
<jdbc-connection-pool name="java:module/jdbc/sportalPool"
res-type="javax.sql.DataSource" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
pool-resize-quantity="1" max-pool-size="5" steady-pool-size="0"
statement-timeout-in-seconds="30">
<property name="User" value="root"></property>
<property name="Password" value="1234"></property>
<property name="portNumber" value="3306"></property>
<property name="dataBaseName" value="sportal"></property>
<property name="serverName" value="192.168.2.7"></property>
</jdbc-connection-pool>
<jdbc-resource pool-name="java:module/jdbc/sportalPool"
jndi-name="java:module/jdbc/sportal"></jdbc-resource>
and my persistence.xml is:
<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="PU" transaction-type="JTA">
<jta-data-source>java:module/jdbc/sportal</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.logging.level" value="FINE" />
</properties>
</persistence-unit>
I get the same error with "java:app/..." instead of "java:module/..." too.
My glassfish version is Open edition 3.0.1, JPA2, eclipse Helios
RGDS

Most likely the jdbc-resource is not created when Glassfish starts. Go to Glassfish Admin console and look under JDBC Resources to check whether your sportal resource has been created or not.
I don't know if Eclipse Glassfish module can do this automatically using glassfish-resources.xml, all I know is that Netbeans 7 created the jdbc resources automatically when Glassfish starts (or does it create it when I reverse engineer my database table?)?
Either way you have to create the JDBC Resource first.

Try using just "jdbc/sportal" as the JNDI and DS name, the prefix may be confusing things.

Related

JPA: configure persistence provider

I got a simple java project created with maven (quickstart archetype)
I am trying to configure JPA persistence for drools sessions (the code comes from drools documentation)
I added drools-persistence-jpa, Bitronix Transaction Manager and com.h2database dependencies to my pom.xml
I created a META-INF folder as Source-Folder in my Eclipse Project in "src/META-INF"
I added the persistence.xml and jndi.properties file there.
In my TestCase I have following code:
[...]
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.drools.persistence.jpa");
[...]
When running the test, I get the following Exception:
javax.persistence.PersistenceException: No Persistence provider for
EntityManager named org.drools.persistence.jpa at
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at com.sample.MyTest.testJPA(MyTest.java:112)
I am relatively sure, that there's just something wrong with the way I created the META-INF or persistence.xml (see below). Any suggestions?
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
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
http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">
<persistence-unit name="org.drools.persistence.jpa" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/testDatasource</jta-data-source>
<class>org.drools.persistence.info.SessionInfo</class>
<class>org.drools.persistence.processinstance.ProcessInstanceInfo</class>
<class>org.drools.persistence.processinstance.ProcessInstanceEventInfo</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.autocommit" value="true" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.BTMTransactionManagerLookup" />
</properties>
</persistence-unit>
</persistence>
I think the problem is related to the place where you put your persistence.xml file. Instead of src/META-INF you must place is either in src/main/resources/META-INF or src/test/resources/META-INF
Edited:
In your persistence.xml file you are stating that you want to use org.hibernate.ejb.HibernatePersistence as a provider. According to your comments, you are not including hibernate-entitymanager as a dependency [source]. Try to add that dependency.
Hope it helps,

Sample for integrating BatooJPA in Jetty with Gradle

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

what's wrong with my #PersistenceContext?

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/

Issue with GAE, JPA and Hibernate setup

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.

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>