I'm using JPA with hibernate under my small resteasy project deployed on wildfly.
I've got my persistence.xml file under directory:
resources/META-INF/
<?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="PostgresDS" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/PostgresDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
So I'm dropping and creating database each time on startup.
When typing annotation #Column(name="SomeName") I've got error that
"Cannot resolve column "Name" "
and as fix I'm supposed to attach data-source.
Should I prepare some hibernate config file(attaching data-source didn't help), maybe disabling some validation ?
Related
Have put the persistence.xml in the classpath of the project in eclipse because before the error was that the file was not found. Now gives this error:
Caused by: javax.persistence.PersistenceException: Invalid persistence.xml. Error parsing XML [line : -1, column : -1] : cvc-elt.1: Can not find the declaration of element 'persistence'
here is my file:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.1"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="DataSource" transaction-type="JTA">
<description>JTA persistence unit related to the datasource DataSource</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.ejb.cfgfile" value="hibernate_DataSource.cfg.xml"/>
</properties>
</persistence-unit>
<persistence-unit name="securityStore" transaction-type="JTA">
<description>JTA persistence unit related to the datasource securityStore</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.ejb.cfgfile" value="hibernate_securityStore.cfg.xml"/>
</properties>
</persistence-unit>
</persistence>
According to the JPA spec (see section 8.2.1 persistence.xml file):
A persistence.xml file defines a persistence unit. The persistence.xml file is located in the META-INF directory of the root of the persistence unit.
So, try to put your persistence.xml in the META-INF directory.
P.S. Maybe you application actually use not the persistence.xml what you expect, but placed somewhere in the classpath in META-INF directory.
In my application I have two distinct sets of entities, and for each of them I want to use a separate persistence unit.
How can I make it so each persistence unit only handles part of the entities I have annotated with #Entity?
I have them distributed in two distinct packages, so I was planning to do something like this in the two orm.xml i have:
<entity-mappings>
<persistence-unit-metadata>
<persistence-unit-defaults>
<schema>first_schema</schema>
</persistence-unit-defaults>
</persistence-unit-metadata>
<package>example.package.first</package>
</entity-mappings>
and
<entity-mappings>
<persistence-unit-metadata>
<persistence-unit-defaults>
<schema>second_schema</schema>
</persistence-unit-defaults>
</persistence-unit-metadata>
<package>example.package.second</package>
</entity-mappings>
But it does not work
we can configure one or more persistent units in same persistent.xml
as below and create entity managers for each with #PersistenceContext(unitName="...") to access from dao.
xml configuration:
-------------------
<?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="persistenceUnit1">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>Datasource</jta-data-source>
<class>Entity1</class>
<class>Entity2</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
....etc
</properties>
</persistence-unit>
<persistence-unit name="persistenceUnit2">
</persistence-unit>
//and so on
</persistence>
Dao service
-------------
#PersistenceContext(unitName="persistenceUnit1")
EntityManager entityManager;
I have persistence.xml as below (just an example) and I want to retrieve set of all persistence-unit names. In this example a,b and c
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="a">
</persistence-unit>
<persistence-unit name="b">
</persistence-unit>
<persistence-unit name="c">
</persistence-unit>
</persistence>
You can get all persistence unit names using this code.
List<ParsedPersistenceXmlDescriptor> persistenceUnits = PersistenceXmlParser.locatePersistenceUnits(new Properties());
for (ParsedPersistenceXmlDescriptor descriptor : persistenceUnits) {
AVAILABLE_MODULES_LIST.add(descriptor.getName());
}
For me it works fine.
I am using JBoss Developer Studio. I have a project with a persistence.xml file. The file is perfect to me, but I keep getting this error on the tab that lists all the Problems of my project.
Class "[Ljava.lang.String;#22ec7158" cannot be resolved
I include the picture for a better context.
When I click on the error, so that it takes me to the place where the error is happening, it takes me to the end of the file.
Persistence.xml
<?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="Persistence">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/MemberOfferDS</jta-data-source>
<class>com.bbb.memberoffer.model.SycsCoordinator</class>
<class>com.bbb.aicweb.memberoffer.model.SycsCoordinatorPhoneNumber</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorClub</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorSecurityGroup</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorClubPk</class>
<class>com.bbb.memberoffer.model.PhoneNumberType</class>
<class>com.bbb.memberoffer.model.Club</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- Not sure if this is the right one to use or not? -->
<property name='hibernate.show_sql' value='true' />
<property name='hibernate.format_sql' value='true' />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
</properties>
</persistence-unit>
</persistence>
You may have solved this already, however, in my case the problem was caused by referencing a class in a element that no longer existed.
EDIT: The exact reason for this type of error message is that the error generator is trying to run a toString() on a String array when it generates the error message.
I have a maven project on NetBeans that have a Resteasy service running, this with no problem, and when I tried to add database access to use with the service using Hibernate(EclipseLink) I am getting a error:
Problem with Filer: Attempt to recreate a file for type my.package.MyClass_
I have no idea why this is happening, any tip?
Thanks in advice.
EDIT
Here is mu persstence.xml
<?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="br.com.meraki.curtamix_CurtamixREST_war_1PU" transaction-type="JTA">
<jta-data-source>curtamix</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties/>
</persistence-unit>
</persistence>