I've tried that, to no avail. Then I've re-mapped the database using
orm.xml, and it worked, and now it is working.
W.
2011/6/3 Tom Ware <tom.ware@xxxxxxxxxx <mailto:tom.ware@xxxxxxxxxx>>
It looks like, as James suggested, there is something going on with
the classloaders.
Is there a chance that the fact that that helper is created in a
static is affecting the classloader it is using? What happens if
you move the call to
Persistence.createEntityManagerFactory("AMB-JPA") into a non-static
part of yoru application.
-Tom
Wellington L.S. da Silva wrote:
Sure,
The EntityManagerFactoryHelper class is as follows:
public class EntityManagerFactoryHelper {
private static EntityManagerFactory factory;
static {
try {
factory =
Persistence.createEntityManagerFactory("AMB-JPA");
} catch(ExceptionInInitializerError e) {
throw e;
}
}
public static EntityManagerFactory getFactory() {
return factory;
}
}
it's a simple singleton based on code sugested at Slashdot on
the topic.
The code leading to the merge is this:
public String votarEnqueteAtiva() throws Exception {
System.out.println("inicio da votarEnqueteAtiva");
List<Enquete> le = CRUDAdm.listByProperty(
Enquete.class, "ativa", new Integer(1)); <<<<<<<<<<<<<< Query
executed here
if (le.size() == 1) {
Enquete e = le.get(0);
List<EnqueteResposta> resps = e.getEnqueteRespostas();
Iterator<EnqueteResposta> i = resps.iterator();
while (i.hasNext()) {
EnqueteResposta er = i.next();
if (er.getId() == voto) er.setTotal(er.getTotal()
+ 1); <<<<<<<< change in slave object
}
CRUDAdm.update(le); <<<<<<<<<<<<<<<<<<<<<<<<<
update (merge) called here
eab = new EnqueteAtivaBean();
eab.setPergunta(e.getQuestao());
List<String> re = new LinkedList<String>();
List<String> pe = new LinkedList<String>();
i = resps.iterator();
int totalGeral = 0;
while (i.hasNext()) {
EnqueteResposta er = i.next();
re.add(er.getResposta());
totalGeral += er.getTotal();
}
eab.setRespostas(re);
i = resps.iterator();
while (i.hasNext()) {
EnqueteResposta er = i.next();
pe.add(""+er.getTotal()*100/totalGeral);
}
eab.setPercents(pe);
HttpServletRequest request =
(HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
HttpSession session = request.getSession();
session.setAttribute("enqueteAtiva",eab);
}
System.out.println("fim da votarEnqueteAtiva");
return NONE;
}
Thanks in advance,
Wellington
2011/6/2 Tom Ware <tom.ware@xxxxxxxxxx
<mailto:tom.ware@xxxxxxxxxx> <mailto:tom.ware@xxxxxxxxxx
<mailto:tom.ware@xxxxxxxxxx>>>
Can you provide the code leading up to and including the
merge() and
the code that creates for
EntityManagerFactoryHelper.getFactory().
-Tom
Wellington L.S. da Silva wrote:
Hi Tom, here it is, generated by Dali with parameters
added by me:
<?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="AMB_JPA"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.amb.entidades.Enquete</class>
<class>com.amb.entidades.EnqueteResposta</class>
<properties>
<property name="eclipselink.target-database"
value="MySQL"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/amb"/>
<property name="javax.persistence.jdbc.user"
value="root"/>
<property name="javax.persistence.jdbc.password"
value="mobview"/>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
It is located at the jar under the META-INF folder. The jar
folder structure is:
root
META-INF (which contains the file persistence.xml)
com (root of the classpath to the classes)
I must remark that BEFORE I try to update the objects I
perform
a query via the listByProperty method (highlighted below)
which
works, so retrieving the very proper object. When I try to
update that retrieved object, it blunders.
The listByProperty is:
public static <T> List<T> listByProperty(Class<T>
entityClass, String propertyName, Object propertyValue)
throws
Exception { EntityManager em =
getEntityManager(); List<T> retorno = null;
try { String query = "select o
from " +
entityClass.getSimpleName() + " o where o." +
propertyName + "
= :propertyValue "; Query q =
em.createQuery(query).setParameter("propertyValue",
propertyValue); retorno =
q.getResultList(); } catch (Exception ex) {
ex.printStackTrace(); }
em.close();
return retorno; }
Cheers,
Wellington
2011/6/1 Tom Ware <tom.ware@xxxxxxxxxx
<mailto:tom.ware@xxxxxxxxxx>
<mailto:tom.ware@xxxxxxxxxx <mailto:tom.ware@xxxxxxxxxx>>
<mailto:tom.ware@xxxxxxxxxx <mailto:tom.ware@xxxxxxxxxx>
<mailto:tom.ware@xxxxxxxxxx <mailto:tom.ware@xxxxxxxxxx>>>>
What does your persistence.xml look like? Where is it
packaged
relative to your Enquete class? How is Enquete mapped?
Wellington L.S. da Silva wrote:
Hi all,
My name is Wellington, I'm brazilian and I'm
creating a poll
application in pure JPA with Eclipselink, that has
at the
moment
2 entities in a quite trivial One-to-many
relationship:
Enquete
holds the one question, EnqueteResposta holds one
of the many
possible answers. Here's the scenario:
1. I've setup a pure JPA project with Entities and
Helper
classes. The entities are created from pre-existant
tables in a
MySQL database by Eclipse Dali I wrote no code but a
Cascade.ALL
in the master class.
2. The main Helper class is a CRUDAdmin that takes
whatever
class through Java Generics and applies JPA
operations on
them
coded as follows:
package com.amb.ops;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
@SuppressWarnings("unchecked")
public class CRUDAdm {
private static EntityManager
getEntityManager() {
EntityManager em =
EntityManagerFactoryHelper.getFactory().createEntityManager();
return em;
} public static <T> T create(T
entity)
throws Exception { EntityManager em =
getEntityManager(); Exception e =
null; T retornoEntity = null;
EntityTransaction t =
em.getTransaction(); try {
t.begin(); retornoEntity =
em.merge(entity); t.commit();
} catch (Exception ex) { if
(t.isActive()) {
em.getTransaction().rollback(); }
e = ex; } em.close();
if (e != null) { throw
e; } return retornoEntity;
} public static <T> T
update(T entity) throws
Exception { EntityManager em =
getEntityManager(); Exception
e = null; T retornoEntity = null;
EntityTransaction t = em.getTransaction();
try { t.begin();
retornoEntity =
em.merge(entity); t.commit();
}
catch (Exception ex) { if
(t.isActive())
{ t.rollback();
} e
= ex; } em.close();
if (e != null) { throw
e; } return retornoEntity;
} public static <T> void
delete(T entity) throws
Exception { EntityManager em =
getEntityManager(); Exception e =
null; EntityTransaction t =
em.getTransaction(); try {
t.begin(); entity =
em.merge(entity);
em.remove(entity); t.commit();
} catch (Exception ex) {
if
(t.isActive()) { t.rollback();
} e = ex;
} em.close();
if (e != null) { throw
e; } } public
static <T> T
retrieve(Class<T>
entityClass, Object id) throws Exception {
EntityManager em = getEntityManager();
Exception e =
null; T retorno = null;
EntityTransaction t = em.getTransaction();
try { t.begin();
retorno =
em.find(entityClass, id);
t.commit(); } catch (Exception ex) {
if
(t.isActive()) { t.rollback();
} e = ex;
} em.close();
if (e != null) { throw
e; } return retorno;
} public
static <T> List<T> listAll(Class<T> entityClass,
String...
orderbys) throws Exception {
EntityManager em =
getEntityManager(); List<T> retorno =
null; try { String
query = "select o
from " +
entityClass.getSimpleName() + " o order by ";
for (String order : orderbys) {
query
+= order + " , "; }
query =
query.substring(0, query.length() - 2);
Query q
= em.createQuery(query); retorno =
q.getResultList(); } catch (Exception
ex) { ex.printStackTrace();
} em.close();
return retorno; }
public
static
<T> List<T> listAll(Class<T> entityClass) throws
Exception { EntityManager em =
getEntityManager(); List<T> retorno =
null; try {
String query =
"select o from " +
entityClass.getSimpleName() + " o ";
Query q =
em.createQuery(query);
retorno =
q.getResultList(); } catch (Exception
ex) { ex.printStackTrace();
} em.close();
return retorno; }
public
static
<T> List<T> listByProperty(Class<T> entityClass,
String
propertyName, Object propertyValue) throws
Exception { EntityManager em =
getEntityManager(); List<T>
retorno = null; try {
String
query =
"select o from " + entityClass.getSimpleName() + " o
where o."
+ propertyName + " = :propertyValue ";
Query q
= em.createQuery(query).setParameter("propertyValue",
propertyValue); retorno =
q.getResultList(); } catch (Exception
ex) { ex.printStackTrace();
} em.close();
return retorno; }
public static <T> List<T>
listByProperty(Class<T> entityClass,
String propertyName, Object propertyValue, String
operador,
String... orderbys) throws Exception {
EntityManager
em = getEntityManager(); List<T> retorno =
null; try { String query =
null; if
(!operador.equalsIgnoreCase("like")) {
query = "select o from " +
entityClass.getSimpleName() + " o
where o." + propertyName + " " + operador + "
:propertyValue
"; } else {
query =
"select
o from " + entityClass.getSimpleName() + " o where
upper(o." +
propertyName + ") " + operador + "
upper(:propertyValue)
"; } if
(orderbys !=
null &&
orderbys.length > 0) { query+= "
order by ";
for (String order : orderbys)
{ query += "o." + order +
" , "; }
query = query.substring(0,
query.length() -
2); } Query q =
em.createQuery(query).setParameter("propertyValue",
propertyValue); retorno =
q.getResultList(); } catch (Exception
ex) { ex.printStackTrace();
} em.close();
return retorno; }
public
static
<T> List<T> listByProperty(Class<T> entityClass,
String
propertyName, Object propertyValue, String
operador) throws
Exception { EntityManager em =
getEntityManager(); List<T> retorno =
null; try { String
query = "select o
from " +
entityClass.getSimpleName() + " o where o." +
propertyName + "
" + operador + " :propertyValue ";
Query q =
em.createQuery(query).setParameter("propertyValue",
propertyValue); retorno =
q.getResultList(); } catch (Exception
ex) { ex.printStackTrace();
} em.close();
return retorno; } }
3. The EntityManagerHelper class is a singleton
coded as
suggested in the EclipseLink docs somewhere, so,
it's a
singleton to the factory class.
4. Having that I packaged the classes in a jar
file and
used the
jar file (now containing the full MODEL of the
application with
all db operations fully encapsulated) in the
client Struts 2
app. The idea is to create the MODEL classes
incrementally and
adding actions as new MODEL jars arrive.
5. The struts app calls an action to update a value in
the slave
class EnqueteResposta, inside master class Enquete, as
follows:
public String votarEnqueteAtiva() throws
Exception {
System.out.println("inicio da
votarEnqueteAtiva");
List<Enquete> le =
CRUDAdm.listByProperty(Enquete.class,
"ativa", new Integer(1));
if (le.size() == 1) {
Enquete e = le.get(0);
List<EnqueteResposta> resps =
e.getEnqueteRespostas();
Iterator<EnqueteResposta> i =
resps.iterator();
while (i.hasNext()) {
EnqueteResposta er = i.next();
if (er.getId() == voto)
er.setTotal(er.getTotal()
+ 1);
}
CRUDAdm.update(le);
eab = new EnqueteAtivaBean();
eab.setPergunta(e.getQuestao());
List<String> re = new LinkedList<String>();
List<String> pe = new LinkedList<String>();
i = resps.iterator();
int totalGeral = 0;
while (i.hasNext()) {
EnqueteResposta er = i.next();
re.add(er.getResposta());
totalGeral += er.getTotal();
}
eab.setRespostas(re);
i = resps.iterator();
while (i.hasNext()) {
EnqueteResposta er = i.next();
pe.add(""+er.getTotal()*100/totalGeral);
}
eab.setPercents(pe);
HttpServletRequest request =
(HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
HttpSession session = request.getSession();
session.setAttribute("enqueteAtiva",eab);
}
System.out.println("fim da votarEnqueteAtiva");
return NONE;
}
Then I get the StackTrace below:
*java.lang.IllegalArgumentException: Object:
[com.amb.entidades.Enquete@af683a] is not a known
entity
type.*
org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3456)
org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.mergeCloneWithReferences(RepeatableWriteUnitOfWork.java:363)
org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3427)
org.eclipse.persistence.internal.jpa.EntityManagerImpl.mergeInternal(EntityManagerImpl.java:452)
org.eclipse.persistence.internal.jpa.EntityManagerImpl.merge(EntityManagerImpl.java:429)
com.amb.ops.CRUDAdm.update(CRUDAdm.java:46)
com.hoog.acoes.EnqueteAtiva.votarEnqueteAtiva(EnqueteAtiva.java:59)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
Now WHAT CAN THAT BE? Do you guys need any other
info to
analyze
this issue?
The CRUDAdmin method to list the Enquete is
working quite
nicely.
Any help will be appreciated.
Cheers all,
Wellington
------------------------------------------------------------------------
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>
<mailto:eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>>
<mailto:eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>
<mailto:eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>>>
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>
<mailto:eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>>
<mailto:eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>
<mailto:eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>>>
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
------------------------------------------------------------------------
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>
<mailto:eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>>
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>
<mailto:eclipselink-users@xxxxxxxxxxx
<mailto:eclipselink-users@xxxxxxxxxxx>>
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
------------------------------------------------------------------------
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx <mailto:eclipselink-users@xxxxxxxxxxx>
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx <mailto:eclipselink-users@xxxxxxxxxxx>
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
------------------------------------------------------------------------
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users