JPA 2.1 no work in eclipse kepler - persistence

this tag doesn't work in eclipse kepler and wildfly 8.0, want to work with jpa 2.1 the tag is auto generate by eclipse.
<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">
if I try with this tag
<?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">
everything works fine.
please helpme with this issues.

According to this reference: How to specify JPA 2.1 in persistence.xml? your tag seems fine.
Can you please post the error eclipse is giving for JPA 2.1 tag?

Related

How the persistence.xml file is different from hibernate.conf.xml file, in which scenario we should which file

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.

Setting Glassfish 4 with jdbc automatically rather to work around manually

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

Eclipse does not detect contents of public schema in jpa mode

After successfully reverse engineering my db using hibernate tools i tried to do the same using jpa facet mode. The connection is successfully made to the db ( same connetion used earlier for hibernate rev-eng) and it detects the contents of all other schemas (pg_catalog, pg_information, pg_toast_temp) except the important one : "public" . Any ideas on how to get it to detect the public schema and generate entities from it? I would like to mention that my persistence.xml is pretty much empty:
<?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="testjpa">
</persistence-unit>
</persistence>

persistence.xml with Glassfish 3.1.1

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.

JPA on JBoss As 6.X or 7

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.