Issue with persistence.xml file when trying to do maven clean install - java

I am getting an error when I try to do a clean/compile/install for one of my modules.
[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:run
(generate-drop-ddl) on project baseImpl: There was an error creating the AntRun task.
An Ant BuildException has occured: Problems in creating a configuration for JPA.
Have you remembered to add hibernate EntityManager jars to the classpath ?:
InvocationTargetException: Invalid persistence.xml.
[ERROR] Error parsing XML (line-1 : column -1): cvc-complex-type.2.4.d:
Invalid content was found starting with element 'class'.
No child element is expected at this point.
I have another module that compiles/installs fine, and the only difference is that it references different classes, so I can't figure out what's wrong with my persistence.xml, or if that is even the problem.
<?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">
<persistence-unit name="appPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>base.bo.Address</class>
<class>base.bo.Club</class>
<class>base.bo.Team</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>

Apparently it wasn't the persistence.xml. I started my pom file from scratch, redid the clean/compile/install and it works. Go figure.

Related

Does persistence.xml support *.xml?

If mine persistence.xml file like 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="jpa">
<mapping-file>jpaOrm/*.xml</mapping-file>
</persistence-unit>
</persistence>
Can spring-data-jpa load all the XML-terminated files in the jpaOrm folder?
I don't think so, but I guess you tried and already found out that it doesn't work? The reason is that the mapping files are looked up by using ClassLoader#getResource() where the mapping file path is passed in. It does that because the class path is not scannable by default i.e. you can ask for elements on the class path that match a pattern. It can be done of course, but that would hurt performance as class path scanning is one of the most expensive operations.

Referencing entities from other jars in EAR

I'm trying to create a layered web proyect with an EAR. I divided the Entities from the DataAccess in order to only allow DataAccess to be called by BusinessLogic.
I'm actually getting this exception:
Object: co.edu.icesi.i2trading.entities.Statetype#21397ba0 is not a known entity type.
And I found here that I need to create a persistence.xml in the EAR, but I can't really understand how.
Sharing a persistence unit across components in a .ear file
I tried placing the XML file in the Configuration Files:
And referencing the jar with <jar-files> in the 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="I2TradingDataAccessPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/I2DataSource</jta-data-source>
<jar-file>../I2TradingEntities.jar</jar-file>
<jar-file>../../I2TradingEntities.jar</jar-file>
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="eclipselink.target-server" value="None"/>
<!-- <property name="eclipselink.logging.level" value="ALL"/> -->
<property name="eclipselink.logging.level" value="WARNING"/>
</properties>
</persistence-unit>
</persistence>
With no success. Any idea why? I'm very new to Web Developing and this is getting to my nerves.
I'm also getting this WARNING in the persistence.xml's DESIGN tab:
I believe that the instruction jar-file interprets any path from the file that holds the persistence.xml... So, your configuration specifies that the Entities JAR are siblings of your EAR file.
The best (and easy) solution for your problem is to put the given persistence.xmlat the META-INF folder of your I2TradingEntities.jar and remove the <jar-file> instructions from it ... This will instruct the Application Server to create a PersistenceContext called "I2TradingDataAccessPU" and to detect any #Entity Class contained in that JAR.

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.

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.

Entities not mapped after moving them into external jar package

In my Java/Seam/JbossAS app, I decided to externalize my Model classes (hibernate entities) and moved them into another project. The project produces model.jar, which is then used by the main app. The model.jar dependency is resolved by Ivy.
Building the main app with Ant works without problems. Then I copy manually the model.jar into 'mainapp.ear/lib' directory. Afterwards I deploy the app and there are no problems (although I notice that there are is no log info about found mappings). But when I want to login, I get the exception:
javax.el.ELException: javax.ejb.EJBTransactionRolledbackException:
org.hibernate.hql.ast.QuerySyntaxException: AppUser is not
mapped [select u from AppUser u where u.userName = :usernamePar]
There were no code changes in the meantime, just externalizing some of the classes into a jar. Does this mean, that I need the source code of the Model classes when compiling the main app?
The EntityManagerFactory is built for scanning entities only from the jar that has a /META-INF/persistence.xml file into.
In order to scan other jars you have to use <jar-file>:
<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="manager1" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/DefaultDS</jta-data-source>
<mapping-file>ormap.xml</mapping-file>
<jar-file>MyApp.jar</jar-file>
<class>org.acme.Employee</class>
<class>org.acme.Person</class>
<class>org.acme.Address</class>
<shared-cache-mode>ENABLE_SELECTOVE</shared-cache-mode>
<validation-mode>CALLBACK</validation-mode>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
See 2.2.1 Packaging in Hibernate doc.
Also check if your hibernate mappings are correctly placed wrt hibernate config file. Note that hibernate mapping resources or classes are relative to the location of hibernate.cfg.xml file.

Categories