Getting hibernate persistence-unit classes from both ORM and OGM - mongodb

I'm trying to use both of hibernate's orm and ogm in the same application.
In my persistence.xml I have two persistence-unit:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="orm-mysql">
...
<class>...</class>
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
...
</persistence-unit>
<persistence-unit name="ogm-mongodb" transaction-type="JTA">
...
<class>...</class>
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
...
</persistence-unit>
...
</persistence>
I want to use the hibernate orm native api with both orm and ogm, and so I need to add the annotated classes to two different MetadataSources.
Since I have the persistence units I thought that I can do this:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("orm-mysql");
Set<ManagedType<?>> managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
ormSources.addAnnotatedClass(type.getJavaType());
}
entityManagerFactory = Persistence.createEntityManagerFactory("ogm-mongo");
managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
ogmSources.addAnnotatedClass(type.getJavaType());
}
The problem is that in the first iteration, of the orm classes, I get both the orm and ogm classes that are included in orm-mysql and ogm-mongo.
The second time around it's fine though, I get only the ogm-mongo classes.
Can't figure out what's wrong, any ideas?

In case anyone makes this stupid mistake as well, I had:
<persistence-unit name="orm-mysql">
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
...
</properties>
</persistence-unit>
Removing the hibernate.archive.autodetection property solved the problem.

Related

database access from a jar inside ear

In an ear-file I have a persistence-unit for managing entities inside that ear which works fine. Additionally, there is a commons jar-file located in APP-INF/lib which is a dependency in other ears as well (other ear-modules on the same level). In this commons-jar I would like to have a single entity and create the possibility for CRUD on that entity. Now it is a jar only with an additional persistence unit (but same db as for the ear) and I can't manage to set it all up. Would that persistence unit be managed as well by the container? Could I use EJBs inside that jar file as well? Is it possible at all to do what I want?
I created the entity as follows
#Entity
public class ConfigEntity {
#Id
private Long id;
#Version
private Long version;
[further fields, getters and setters omitted]
}
With the following "Dao" I try to call isEntityManagerNull from a bean inside the ear.
public class ConfigBEDao {
#PersistenceContext(unitName = "configurationPU")
EntityManager entityManager;
public boolean isEntityManagerNull() {
return entityManager == null;
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="configurationPU" transaction-type="RESOURCE_LOCAL">
<description/>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/db</jta-data-source>
<properties>
<property name="eclipselink.target-database" value="${database.dialect}"/>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.cache.shared.default" value="false"/>
</properties>
</persistence-unit>
</persistence>
From a bean inside the ear file I call
ConfigBEDao dao = new ConfigBEDao();
dao.isEntityManagerNull();
which gives me the following exception already when trying to deploy on the server (payara):
java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [ConfigBEDao/entityManager] in the scope of the module called [main_ear-1.0-SNAPSHOT#main_ejb-1.0-SNAPSHOT.jar]. Please verify your application.
at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUViaEMRef(BundleDescriptor.java:733)
at org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl.findReferencedPUs(EjbBundleDescriptorImpl.java:889)
at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:186)
at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
Then I changed the persistence.xml as follows but that didn't work either although the application can be deployed:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="configurationPU" transaction-type="RESOURCE_LOCAL">
<description/>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/db"/>
<property name="javax.persistence.jdbc.user" value="db"/>
<property name="javax.persistence.jdbc.password" value="secret"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
</properties>
</persistence-unit>
</persistence>
which gave me
javax.persistence.PersistenceException: No Persistence provider for EntityManager named configurationPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)

How to use 2 PersistenceUnits in one Persistence.xml in a JavaEE application?

I am trying to access 2 different tables in my database.
Initially my persistence.xml was as follows:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="Persistence">
<jta-data-source>jdbc/classicmodels</jta-data-source>
<class>com.tugay.maythirty.model.Customers</class>
<class>com.tugay.maythirty.model.Products</class>
<class>com.tugay.maythirty.model.Employee</class>
<class>com.tugay.maythirty.model.Office</class>
</persistence-unit>
</persistence>
So I defined a DataSource in Glassfish following this great artical and my application was working fine. I was using #PersistenceContext annotation in my DAO classes with name="Persistence"
This basically is connected to a table called "classicmodels".
Then I needed to fetch some data from a table called "sakila". I added these lines to my persistence.xml:
<persistence-unit name="sakila">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.tugay.maythirty.model.Actor</class>
<class>com.tugay.maythirty.model.Film</class>
<class>com.tugay.maythirty.model.FilmActor</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sakila"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="password"/>
</properties>
</persistence-unit>
And I have used this code in my DAO:
public List<Actor> getAllActors(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sakila");
EntityManager em = emf.createEntityManager();
TypedQuery<Actor> actorTypedQuery = em.createQuery("Select a from Actor a",Actor.class);
return actorTypedQuery.getResultList();
}
When I deploy my application to GlassFish however I started getting this Exception:
java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [Persistence] in the scope of the module called [may-thrity]. Please verify your application.
So it seems that now my Persistence Unit with name "Persistence" is gone.
What is it that I am doing wrong?
Have you tried explicitly declaring your persistence unit with
#PersistenceContext(unitName="Persistence")
instead of
#PersistenceContext(name="Persistence")
PersistenceContext doc

EclipseLink does not work on Netbeans, is this normal?

It'll be weeks that I'm stuck with EclipseLink. I can not persist an object in my database. I use netbeans 7.3. I encountered this problem when I started designing a web application. What follows is the approach I have adopted. It may be that I do without me realize a mistake.
After that netbeans has finished generating the project files I configured the jndi. Then I converted automatically with netbeans, the database tables in entity object.
here is the link
then, from these classes, I created their JPAController . (Always automatically with netbeans)
and finally, as a test, I just instantiate the description of "Outils" and leave the fields empty id. Since the latter automatically increment in the database, if the persistence is done well, I should have an id when I appear with out the console.
<body>
<h1>Hello World!</h1>
<%
Outils o = new Outils();
o.setDesignation("hammers");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Test_EclipseLinkPU");
UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
OutilsJpaController o_ctrl = new OutilsJpaController(utx, emf);
o_ctrl.create(o);
out.println("this is the id of hammer " + o.toString());
%>
</body>
and I get as result: Outils[ id=null ].
I have no error or on glassfish even less about the debugger.
Ps : Here are the 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="Test_EclipseLinkPU" transaction-type="JTA">
<jta-data-source>test_data_source</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Thank you for your future is in your answers and listening for any additional information.
(Not really an answer but I couldn't show this in a comment)
To increase logging your persistence.xml should look like this:
<?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="Test_EclipseLinkPU" transaction-type="JTA">
<jta-data-source>test_data_source</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
You need to commit or flush the transaction before anything will be written to the database. If you are using IDENTITY sequencing (I strongly recommend not using IDENTITY, use TABLE or SEQUENCE if db supports it), then the Id cannot be allocated until the insert occurs, so a persist() will not assign the Id (as it does for TABLE and SEQUENCE).
What does your create() method do? How is your Id mapped?

Basic Standalone JPA example with Postgres using Eclipse

I have been trying to get a very basic standalone JPA example to work with Postgres using the Eclipse IDE.
I have a persistance.xml defined which looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<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="sample" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>package.class</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="INFO" />
<property name="eclipselink.jdbc.driver" value="org.postgresql.Driver" />
<property name="eclipselink.jdbc.url"
value="jdbc:postgresql://localhost:5432/sample" />
<property name="eclipselink.jdbc.user" value="scott" />
<property name="eclipselink.jdbc.password" value="tiger" />
</properties>
</persistence-unit>
</persistence>
This file lives in the src/main/resources/META-INF folder. I have included the src/main/resources folder to my source directory in eclipse. I have one simple Entity defined named User. When I try and create that entity
EntityManagerFactory entityManagerFactory
= Persistence.createEntityManagerFactory("sample");
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
User user = new User();
user.setEnabled(false);
user.setEmailId("test#test.com");
tx.begin();
em.persist(user);
tx.commit();
} catch (Exception e) {
em.getTransaction().rollback();
} finally {
em.close();
}
I get the following exception - Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named sample
It seems like my persistance.xml file is not being picked up. Where does the JPA framework look to load the persistance.xml file?
This was a dumb mistake. The file needs to called persistence.xml and not persistance.xml.

Exception javax.persistence.PersistenceException: No Persistence provider for EntityManager

We've been working on this for days and we are stumped. This is supposed to be an easy tutortial using TopLink. We are trying to get this to work before we do our real web app. This is the following exception we get:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named pu1:
The following providers:
oracle.toplink.essentials.PersistenceProvider
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
Returned null to createEntityManagerFactory.
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at client.Client.main(Client.java:45)
Java Result: 1
this happens after executing this line from our emf driver class:
emf = Persistence.createEntityManagerFactory("pu1");
I'm assuming the problem is in our persistence.xml file (which is in the correct folder (WEB-INF/classes/META-INF). Also netbeans generated the xml file for us which is:
<?xml version="1.0" encoding="UTF-8"?>
<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="JPAExamplePU" transaction-type="JTA">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<jta-data-source>SomeDB</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
We were also thinking it may be an adding a library issue or something along that line. Any help is much appreciated. Thanks
You have the wrong persistence unit name. Use the one from the xml (i.e. the one defined with <persistence-unit name="..."):
emf = Persistence.createEntityManagerFactory("JPAExamplePU");