TomEE pointing to wrong DataSource - java

I'm using a single TomEE instance to deploy 2 Webapplications. Both applications use a different database and different entities.
Application 2 is integrated into Application 1, so I need both the schema anytime while running.
I have both DataSources configured in the tomee.xml like this:
<tomee>
<Resource id="testDBPool" type="DataSource">
jdbcDriver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/testDB"
username = "admin"
password = "admin"
</Resource>
</tomee>
<tomee>
<Resource id="testDBPool2" type="DataSource">
jdbcDriver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/testDB2"
username = "admin"
password = "admin"
</Resource>
</tomee>
In Application 1 I use this 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="testDBPool" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/testDBPool</jta-data-source>
</persistence-unit>
</persistence>
In Application 2 I use this 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="testDBPool2" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/testDBPool2</jta-data-source>
</persistence-unit>
</persistence>
When I tried to run application then it is throwing testDB.table1 is not exist, whereas actually the table1 exist in testDB2 schema not in testDB, I do not understand why it is pointing to wrong schema?

It seems to me that your persistence.xml of Application2 is never loaded. That is not so surprising as there already is a presistence.xml running for the context.
Try adding the entries from Application2 into Application 1 and see if that works out.
Alternatively have a look at this question: How can I make a JPA application access different databases? and see if it helps.
If both didn't work untie Application2 from Application1 and run them in different contexts so each of them has its own persistence.xml running then debug it again.

Related

Can not find the declaration of element 'persistence' , element persistence missing

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.

Cannot resolve column name

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 ?

Get all available persistence-unit names defined in persistence.xml

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.

Ljava.lang.String class cannot be resolved in persistence.xml

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.

EclipseLink compilation error

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>

Categories

Resources