Reputation: 6352
I am doing the query below to test if JPA is retrieving the expected results from the database.
I can't understand why row instanceof Ativo
is returning false. row.getClass()
returns br.meuspila.entity.Ativo
, and the printed object is br.meuspila.entity.Ativo[ id=1 ]
. The imports of the class are ok, and there is not other class named Ativo inside the same package.
// OUTPUT:
INFO: br.meuspila.entity.Ativo[ id=1 ] // row
INFO: false // row instanceof Ativo?
INFO: class br.meuspila.entity.Ativo // row.getClass()
MyMB class:
package br.meuspila.mb;
import br.meuspila.database.JpaUtil;
import br.meuspila.entity.Ativo;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
@ManagedBean
@SessionScoped
public class MyMB {
public String teste = "testando, 123";
/**
* Creates a new instance of ManagedBean
*/
public MyMB() {
}
public String getTeste() {
return teste;
}
public void setTeste(String teste) {
this.teste = teste;
}
public void actionTeste() {
EntityManager em = JpaUtil.getInstance().createEntityManager();
try {
EntityTransaction t = em.getTransaction();
t.begin();
Query query = em.createQuery("select x from Ativo x");
List result = query.getResultList();
for (Object row : result) {
System.out.println(row);
System.out.println(row instanceof Ativo);
System.out.println(row.getClass());
}
t.commit();
} finally {
em.close();
}
}
}
JpaUtil class:
package br.meuspila.database;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public final class JpaUtil {
private JpaUtil() {
}
public static JpaUtil getInstance() {
return JpaUtilHolder.INSTANCE;
}
private static class JpaUtilHolder {
private static final JpaUtil INSTANCE = new JpaUtil();
private static final EntityManagerFactory EMF = Persistence.createEntityManagerFactory("MeusPila3_WebPU");
}
public EntityManager createEntityManager() {
return JpaUtilHolder.EMF.createEntityManager();
}
}
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="MeusPila3_WebPU" transaction-type="JTA">
<jta-data-source>jdbc/meuspiladb</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
</properties>
</persistence-unit>
</persistence>
My project configuration (Web Project):
JPA 2.1 - EclipseLink-2.5.0.v20130507-rNA
GlassFish Server 4
NetBeans IDE 7.3.1 (Build 201306052037)
Java: 1.7.0_25; Java HotSpot(TM) 64-Bit Server VM 23.25-b01
Runtime: Java(TM) SE Runtime Environment 1.7.0_25-b17
System: Windows 7 version 6.1 running on amd64; Cp1252; pt_BR (nb)
Note: JpaUtil
class was inside br.meuspila.database
package.
Upvotes: 3
Views: 3047
Reputation: 359
In my case, the Entity´s composite PK was different from the PK of the table. Fixing the mapping, resolve the issue.
Upvotes: 0
Reputation: 6352
As suggested by @Rafaelle, I think the problem was my singleton class: JpaUtil. Fixed the code by deleting that class.
Maybe if I changed that class the problem would be solved. I was observing it and noticed that I did something (possibly) wrong: in the singleton I tried to create 2 instances inside the internal class (JpaUtilHolder)... Maybe I should remove EMF from that internal class and put it as a not static attribute of the JpaUtil class.
Replaced in MyMB:
EntityManager em = JpaUtil.getInstance().createEntityManager();
by
EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
EntityManager em = emf.createEntityManager();
Upvotes: 0
Reputation: 20885
Random loaders showing up! I can't help without seeing the whole application - but here you a quick fix: remove the Ativo
class definition (better the entire beans package) from your WEB-INF/lib
, and put all of your beans inside an archive in the container lib path (refer to your container doc). This way, thanks to delegation, the bean classes will always be loaded from the same loader.
Again, be sure that no bean class definition is in WEB-INF
(neither in a JAR nor in a plain .class file). If you use a build tool supporting the following, try to put the beans in a separate project (subproject/module/submodule), make other projects depend on it, and set the dependency scope to provided (or the equivalent, ie the classes are needed for compilation and testing but must not be included in the output artifact, war, jar or ear)
Upvotes: 2