Hibernate tools: persistence unit not found - java

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.

Related

DRY in persistence.xml

I'm trying to use DRY in my persistence.xml file in different persistence units.
I have the following persistence.xml (it's just one file):
<persistence-unit name="siteAPU" transaction-type="JTA">
<jta-data-source>java:/siteADS</jta-data-source>
<class>package.name.Subs</class>
<class>package.name.SubsRef</class>
<class>package.name.SubsLoc</class>
...
<persistence-unit name="siteBPU" transaction-type="JTA">
<jta-data-source>java:/siteBDS</jta-data-source>
<class>package.name.Subs</class>
<class>package.name.SubsRef</class>
<class>package.name.SubsLoc</class>
...
You can see I have two different persistence units and I have the same classes being persisted.
Is there a way I can programmatically generate the persistence.xml file and don't repeat the content inside each persistence unit?
create a orm file:
<persistence-unit name="YOU_PU" ...>
<provider>YOU_PROVIDER</provider>
<mapping-file>orm.xml</mapping-file>
Inside the ORM file you will write the entities.
There is a sample here: https://github.com/uaihebert/uaicriteria/blob/master/src/test/resources/orm.xml

Persistence.xml file for Hibernate JPA and Mysql

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

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".

ERROR: Class "test2" is mapped, but is not included in any

I'm getting the error "Class "test2" is mapped, but is not included in any" in eclipse, however the class is included in 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="test1">
<class>com.test.domains.test1</class>
<properties>
<!-- properties for schema1 here -->
</properties>
</persistence-unit>
<persistence-unit name="test2">
<class>com.test.test2</class>
<properties>
<!-- properties for schema 2 here -->
</properties>
</persistence-unit>
</persistence>
Why does this error appear. Do I have to configure it different for 2 db schemas?
Eclipse (or more specifically Dali) do have limitation, it does not support two persistence units. That is also told in their pages:
Currently Dali only supports one Persistence Unit and one Persistence
XML file per project. Other configurations can exist in a JPA project,
but the validation and defaults processing may not be correct when
multiple persistence units are used.
What it comes to JPA itself, persistence.xml seems to becorrect. There is no need to add same class to two persistence units.

Two Persistence Unit in one Persistence.xml

We created some libraries that all our projects will use, this libraries will provide the basic functionality of all our systems (login, some manage, etc). But the application itself could use another database.
What we did was to create the Persistence.xml with two persist units. And package all the core library entities in a jar called "LN-model.jar" and all of the entities of out test app in "App-model.jar". But for some reason we still obtain the following message.
Could not resolve a persistence unit corresponding to the persistence-context-ref-name [x.x.x.x.listener.InicializadorListener/em] in the scope of the module called [gfdeploy#/Users/zkropotkine/WORK/SeguridadCore/dist/gfdeploy/SeguridadCore-war_war]. Please verify your application.
Here's our Persistence.xml
<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="x" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/x</jta-data-source>
<jar-file>App-model.jar</jar-file>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
</properties>
</persistence-unit>
<persistence-unit name="y" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/y</jta-data-source>
<jar-file>LN-model.jar</jar-file>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
By the way we put the Persistence.xml in a jar, that we add to our Enterprise Project (EAR).
The problem is that the JPA does not know which is the persistence unit to use. when you have only one persistence unit this problem does not occur. To fix do the following:
You need to specify a persistence unit : #PersistenceContext(unitName="...") in the Ejb that do not have
You can add the annotations:
#PersistenceUnit(name = "x")
EntityManagerFactory entityManagerFactory;
#PersistenceContext(unitName = "y")
EntityManager entityManager;
Or you can create it manually:
EntityManagerFactory emfA = Persistence.createEntityManagerFactory("x", properties);
EntityManagerFactory emfB = Persistence.createEntityManagerFactory("y", properties);
For more details, please see the following link: https://docs.oracle.com/html/E25034_01/usingmultipledbs.htm
is very useful, to me helped me!

Categories