Persistence.xml file for Hibernate JPA and Mysql - java

I'm finding it difficult to find an up-to-date and clear explanation of how the persistence.xml should look and what it needs to contain. What would your typical persistence.xml file look like for the following:
Provider: Hibernate implementation of JPA
Mysql database
DB name: hibernatetest
username: root
password: 12345
databases url: localhost port 3306
and for the sake of simplicity say that my Entity class that needs to be persistent is located at com.example.MyClass
Thanks in advance!

here is an basic persistance.xml example :
<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="org.hibernate.tutorial.jpa">
<class>com.example.MyClass</class>
<class>...</class>
<class>...</class>
</persistence-unit>
</persistence>
You can find more details about it here :
http://docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/hibernate-gsg-tutorial-jpa.html

Related

access denied when accessing mysql database through glassfish server

I'm following a tutorial which uses GlassFish to access a mysql database in NetBeans. The problem is I'm getting this error
Internal Exception: java.sql.SQLException: Error in allocating a
connection. Cause: Connection could not be allocated because: Access
denied for user 'root'#'localhost' (using password: NO)
when I try to deploy my app. I understand I need to add the password to the configuration so that GlassFish can establish the connection. The problem is I'm not sure where to include it within the persistence file.
<?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="com.mycompany_MyApp_ejb_1.0-SNAPSHOTPU" transaction-type="JTA">
<jta-data-source>jdbc/creditpurchase</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
You can edit properties in glassfish admin console. Resources->JDBC->Connections Pools->your_connection_pool in Additional Properties tab. You can add property with name password. take a look here.
I was able to do it here:
C:\Code\Java\Glassfish\glassfish3\glassfish\domains\domain1\config\domain.xml
Change the properties within the jdbc-connection-pool tag

Hibernate tools: persistence unit not found

I have created a Hibernate / JPA project and I'd like to create entities from tables.
While the tool works like a charm selecting a pure Hibernate configuration, in the JPA mode it does not work, claiming the absence of the persistence unit
As shown in the following photo, the persistence unit is located in the folder /src/main/resources/META-INF
This is the code of persistence.xml file:
<?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="socialsports" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/LiveDataSource</jta-data-source>
</persistence-unit>
</persistence>
This is the result of my attempt to run Hibernate tools to generate entities:
Since it only doesn't work in JPA mode, the use of the deprecated org.hibernate.ejb.HibernatePersistenceProvider could be a problem. Try org.hibernate.jpa.HibernatePersistenceProvider instead.

JPA MySQL Connection in Java EE project

I am running a local MySQL Server, later I want to use one on a remote computer and I have a question how to setup JPA in my Java EE project correctly.
First of all, do I have to setup the database connection in my application server or only in my persistence.xml? I have just added it in a JBoss Server and I can use JNDI now. But JNDI is not compatible with JPA, is it correct?
I tried it now with Geronimo and JBoss, with both I am not sure what I have to do correctly. I wanted to use OpenJPA as an JPA implementation which is standard on Geronimo.
I found many tutorials on the web, but most isnt very complet.
<?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="schulungen" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>MySQLServer</jta-data-source>
<class>model.Schulung</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
</properties>
</persistence-unit>
</persistence>
What is JTA-Data-Source? If I understood it right, it's a name given by the application server to identify the which connection I want.
Do I have to add properties like jdbc connection URL etc.?
I hope you understand my problem.

JPA persistence.xml adding classes

I have a question regarding the class integration in the persistence xml.
Is something like this possible?
<?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="ranea">
<class>com.dal.pojo.*</class>
</persistence-unit>
</persistence>
I know that this does not work....but I have a rather big database structure and I would appreciate an answere how to include my pojo classes in the persistence unit easily.
If you're using Eclipse, you can right click on persistence and use "synchronize class list".

Entity manager error on the server?

Hi I have developed my java application on glassfish server with EJB 3.0 persistance. On my local machine the application was running fine and was persisting the data perfectly. However, When I deployed it on the linux server, whenever I persist any data it gives me this EJB exception error:
Caused by: Java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.
And here is my persistence.xml file:
<?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">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/security</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
</persistence>
what could possbily cause this?
PS: Im using Glassfish 3.1.43, EJB 3.0
What database are you using? I would verify that the user you have configured in the connection pool has the privilege to create tables.
I have managed to solve it finally!! The problem was in the database connection that was not set properly!! Now I have connected to the database and the data are persisted!!

Categories