Re: [appengine-java] Re: Persistence with JPA + GAE
Hi Johan, try this code try{ cn2.getTransaction().begin(); cn2.persist(e); cn2.getTransaction().commit(); response.getWriter().write(ok); } catch(Exception e){ cn2.getTransaction().rollback(); response.getWriter().write(e.getLocalizedMessage()); }finally{ cn2.close(); } I think this place an exception in persist method. []'s Diego 2010/2/9 Johan Vallejo jvall...@vnperu.com Hi Diego Yes, i try using Long and Key but with both i have the error Log : javax.persistence.PersistenceException: Transaction is still active. You should always close your transactions correctly using commit() or rollback(). El 09/02/2010 01:06 p.m., Diego Osse Fernandes escribió: Hi Johan, you can use the Long as PK @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; []'s Diego 2010/2/9 Johan Vallejo jvall...@vnperu.com Hi Jake, Now i have this error Log : javax.persistence.PersistenceException: Transaction is still active. You should always close your transactions correctly using commit() or rollback(). this is my code : EntityManager cn2 = emf.get().createEntityManager(); distritos dist2 = cn2.find(distritos.class, id); evento e = new evento(); e.setDistrito(dist2); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.getTransaction().begin(); cn2.persist(e); cn2.getTransaction().commit(); response.getWriter().write(ok); }finally{ cn2.close(); } Distrito : @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Extension (vendorName=datanucleus, key=gae.encoded-pk, value=true) Key id; --getter and setter-- El 08/02/2010 03:37 p.m., Jake escribió: Hello, The Google Datastore has four ways that you can store a Primary Key ID: String, Long, and two variations on Google's own Key object. If you want to create a parent/child relationship between two persisted objects (in your case, evento is the parent and distritos is the child), then you need to use Google's Key object as the primary ID. See: http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#Keys If the class is used as a child class in a relationship, the key field must be of a type capable of representing an entity group parent: either a Key instance, or a Key value encoded as a string. So, you have two options: 1. Change your Long id values to Key id values. 2. Instead of storing private distritos distrito directly in your evento object, store a reference to the id: private Long distrito_id Jake On Feb 6, 4:06 pm, chevelle jvall...@vnperu.com jvall...@vnperu.com wrote: Hi I am new in the list, in these days i am trying save data between two entity beans using JPA but without success, i am cheking the log nd says: Uncaught exception from servlet javax.persistence.PersistenceException: Error in meta-data forbeans.distritos.id: Cannot have a java.lang.Long primary key and be a child object (owning field is beans.evento.distrito). this is my servlet: Long id = Long.parseLong(request.getParameter(idDistrito)); response.setContentType(text/html); EntityManager cn = emf.get().createEntityManager(); Query q = cn.createQuery(SELECT d FROM distritos d WHEREd.id = :codigo); q.setParameter(codigo,id); distritos dist = (distritos) q.getSingleResult(); EntityManager cn2 = emf.get().createEntityManager(); cn2.getTransaction().begin(); evento e = new evento(); e.setDistrito(dist); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.persist(e); response.getWriter().write(ok); }finally{ cn2.getTransaction().commit(); cn2.close(); } this is my entity bean Evento: @Entity public class evento implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; @Temporal(javax.persistence.TemporalType.DATE) private Date fechaCreacion; @JoinColumn @OneToMany(cascade = CascadeType.ALL) private distritos distrito; --getter and setter-- Distrito: @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; private String distrito; --getter and setter-- i hope anyone help me, Thank! Johan -- You received this message because you are subscribed to the Google Groups Google App Engine for Java
Re: [appengine-java] Re: Persistence with JPA + GAE
Hi Jake, Now i have this error Log : javax.persistence.PersistenceException: Transaction is still active. You should always close your transactions correctly using commit() or rollback(). this is my code : EntityManager cn2 = emf.get().createEntityManager(); distritos dist2 = cn2.find(distritos.class, id); evento e = new evento(); e.setDistrito(dist2); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.getTransaction().begin(); cn2.persist(e); cn2.getTransaction().commit(); response.getWriter().write("ok"); }finally{ cn2.close(); } Distrito : @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Extension (vendorName="datanucleus", key="gae.encoded-pk", value="true") Key id; --getter and setter-- El 08/02/2010 03:37 p.m., Jake escribi: Hello, The Google Datastore has four ways that you can store a Primary Key ID: String, Long, and two variations on Google's own Key object. If you want to create a parent/child relationship between two persisted objects (in your case, evento is the parent and distritos is the child), then you need to use Google's Key object as the primary ID. See: http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#Keys "If the class is used as a "child" class in a relationship, the key field must be of a type capable of representing an entity group parent: either a Key instance, or a Key value encoded as a string." So, you have two options: 1. Change your Long id values to Key id values. 2. Instead of storing "private distritos distrito" directly in your evento object, store a reference to the id: "private Long distrito_id" Jake On Feb 6, 4:06pm, chevelle jvall...@vnperu.com wrote: Hi I am new in the list, in these days i am trying save data between two entity beans using JPA but without success, i am cheking the log nd says: Uncaught exception from servlet javax.persistence.PersistenceException: Error in meta-data for beans.distritos.id: Cannot have a java.lang.Long primary key and be a child object (owning field is beans.evento.distrito). this is my servlet: Long id = Long.parseLong(request.getParameter("idDistrito")); response.setContentType("text/html"); EntityManager cn = emf.get().createEntityManager(); Query q = cn.createQuery("SELECT d FROM distritos d WHERE d.id = :codigo"); q.setParameter("codigo",id); distritos dist = (distritos) q.getSingleResult(); EntityManager cn2 = emf.get().createEntityManager(); cn2.getTransaction().begin(); evento e = new evento(); e.setDistrito(dist); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.persist(e); response.getWriter().write("ok"); }finally{ cn2.getTransaction().commit(); cn2.close(); } this is my entity bean Evento: @Entity public class evento implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; @Temporal(javax.persistence.TemporalType.DATE) private Date fechaCreacion; @JoinColumn @OneToMany(cascade = CascadeType.ALL) private distritos distrito; --getter and setter-- Distrito: @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; private String distrito; --getter and setter-- i hope anyone help me, Thank! Johan -- You received this message because you are subscribed to the Google Groups "Google App Engine for Java" group. To post to this group, send email to google-appengine-j...@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
[appengine-java] Re: Persistence with JPA + GAE
Hey, I don't play with JPA too much, but I saw this: http://www.datanucleus.org/products/datanucleus/jpa/transactions.html Jake On Feb 9, 10:55 am, Johan Vallejo jvall...@vnperu.com wrote: Hi Jake, Now i have this error Log :javax.persistence.PersistenceException: Transaction is still active. You should always close your transactions correctly using commit() or rollback(). this is my code : EntityManager cn2 = emf.get().createEntityManager(); distritos dist2 = cn2.find(distritos.class, id); evento e = new evento(); e.setDistrito(dist2); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.getTransaction().begin(); cn2.persist(e); cn2.getTransaction().commit(); response.getWriter().write(ok); }finally{ cn2.close(); } Distrito : @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Extension (vendorName=datanucleus, key=gae.encoded-pk, value=true) Key id; --getter and setter-- El 08/02/2010 03:37 p.m., Jake escribió:Hello, The Google Datastore has four ways that you can store a Primary Key ID: String, Long, and two variations on Google's own Key object. If you want to create a parent/child relationship between two persisted objects (in your case, evento is the parent and distritos is the child), then you need to use Google's Key object as the primary ID. See:http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#KeysIf the class is used as a child class in a relationship, the key field must be of a type capable of representing an entity group parent: either a Key instance, or a Key value encoded as a string. So, you have two options: 1. Change your Long id values to Key id values. 2. Instead of storing private distritos distrito directly in your evento object, store a reference to the id: private Long distrito_id Jake On Feb 6, 4:06 pm, chevellejvall...@vnperu.comwrote:Hi I am new in the list, in these days i am trying save data between two entity beans using JPA but without success, i am cheking the log nd says: Uncaught exception from servlet javax.persistence.PersistenceException: Error in meta-data for beans.distritos.id: Cannot have a java.lang.Long primary key and be a child object (owning field is beans.evento.distrito). this is my servlet: Long id = Long.parseLong(request.getParameter(idDistrito)); response.setContentType(text/html); EntityManager cn = emf.get().createEntityManager(); Query q = cn.createQuery(SELECT d FROM distritos d WHERE d.id = :codigo); q.setParameter(codigo,id); distritos dist = (distritos) q.getSingleResult(); EntityManager cn2 = emf.get().createEntityManager(); cn2.getTransaction().begin(); evento e = new evento(); e.setDistrito(dist); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.persist(e); response.getWriter().write(ok); }finally{ cn2.getTransaction().commit(); cn2.close(); } this is my entity bean Evento: @Entity public class evento implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; @Temporal(javax.persistence.TemporalType.DATE) private Date fechaCreacion; @JoinColumn @OneToMany(cascade = CascadeType.ALL) private distritos distrito; --getter and setter-- Distrito: @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; private String distrito; --getter and setter-- i hope anyone help me, Thank! Johan -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To post to this group, send email to google-appengine-j...@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
Re: [appengine-java] Re: Persistence with JPA + GAE
Hi Johan, you can use the Long as PK @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; []'s Diego 2010/2/9 Johan Vallejo jvall...@vnperu.com Hi Jake, Now i have this error Log : javax.persistence.PersistenceException: Transaction is still active. You should always close your transactions correctly using commit() or rollback(). this is my code : EntityManager cn2 = emf.get().createEntityManager(); distritos dist2 = cn2.find(distritos.class, id); evento e = new evento(); e.setDistrito(dist2); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.getTransaction().begin(); cn2.persist(e); cn2.getTransaction().commit(); response.getWriter().write(ok); }finally{ cn2.close(); } Distrito : @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Extension (vendorName=datanucleus, key=gae.encoded-pk, value=true) Key id; --getter and setter-- El 08/02/2010 03:37 p.m., Jake escribió: Hello, The Google Datastore has four ways that you can store a Primary Key ID: String, Long, and two variations on Google's own Key object. If you want to create a parent/child relationship between two persisted objects (in your case, evento is the parent and distritos is the child), then you need to use Google's Key object as the primary ID. See: http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#Keys If the class is used as a child class in a relationship, the key field must be of a type capable of representing an entity group parent: either a Key instance, or a Key value encoded as a string. So, you have two options: 1. Change your Long id values to Key id values. 2. Instead of storing private distritos distrito directly in your evento object, store a reference to the id: private Long distrito_id Jake On Feb 6, 4:06 pm, chevelle jvall...@vnperu.com jvall...@vnperu.com wrote: Hi I am new in the list, in these days i am trying save data between two entity beans using JPA but without success, i am cheking the log nd says: Uncaught exception from servlet javax.persistence.PersistenceException: Error in meta-data forbeans.distritos.id: Cannot have a java.lang.Long primary key and be a child object (owning field is beans.evento.distrito). this is my servlet: Long id = Long.parseLong(request.getParameter(idDistrito)); response.setContentType(text/html); EntityManager cn = emf.get().createEntityManager(); Query q = cn.createQuery(SELECT d FROM distritos d WHEREd.id = :codigo); q.setParameter(codigo,id); distritos dist = (distritos) q.getSingleResult(); EntityManager cn2 = emf.get().createEntityManager(); cn2.getTransaction().begin(); evento e = new evento(); e.setDistrito(dist); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.persist(e); response.getWriter().write(ok); }finally{ cn2.getTransaction().commit(); cn2.close(); } this is my entity bean Evento: @Entity public class evento implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; @Temporal(javax.persistence.TemporalType.DATE) private Date fechaCreacion; @JoinColumn @OneToMany(cascade = CascadeType.ALL) private distritos distrito; --getter and setter-- Distrito: @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; private String distrito; --getter and setter-- i hope anyone help me, Thank! Johan -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To post to this group, send email to google-appengine-j...@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.comgoogle-appengine-java%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en. -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To post to this group, send email to google-appengine-j...@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
Re: [appengine-java] Re: Persistence with JPA + GAE
Hi Diego Yes, i try using Long and Key but with both i have the error Log : javax.persistence.PersistenceException: Transaction is still active. You should always close your transactions correctly using commit() or rollback(). El 09/02/2010 01:06 p.m., Diego Osse Fernandes escribi: Hi Johan, you can use the Long as PK @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; []'s Diego 2010/2/9 Johan Vallejo jvall...@vnperu.com Hi Jake, Now i have this error Log : javax.persistence.PersistenceException: Transaction is still active. You should always close your transactions correctly using commit() or rollback(). this is my code : EntityManager cn2 = emf.get().createEntityManager(); distritos dist2 = cn2.find(distritos.class, id); evento e = new evento(); e.setDistrito(dist2); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.getTransaction().begin(); cn2.persist(e); cn2.getTransaction().commit(); response.getWriter().write("ok"); }finally{ cn2.close(); } Distrito : @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Extension (vendorName="datanucleus", key="gae.encoded-pk", value="true") Key id; --getter and setter-- El 08/02/2010 03:37 p.m., Jake escribi: Hello, The Google Datastore has four ways that you can store a Primary Key ID: String, Long, and two variations on Google's own Key object. If you want to create a parent/child relationship between two persisted objects (in your case, evento is the parent and distritos is the child), then you need to use Google's Key object as the primary ID. See: http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#Keys "If the class is used as a "child" class in a relationship, the key field must be of a type capable of representing an entity group parent: either a Key instance, or a Key value encoded as a string." So, you have two options: 1. Change your Long id values to Key id values. 2. Instead of storing "private distritos distrito" directly in your evento object, store a reference to the id: "private Long distrito_id" Jake On Feb 6, 4:06pm, chevelle jvall...@vnperu.com wrote: Hi I am new in the list, in these days i am trying save data between two entity beans using JPA but without success, i am cheking the log nd says: Uncaught exception from servlet javax.persistence.PersistenceException: Error in meta-data for beans.distritos.id: Cannot have a java.lang.Long primary key and be a child object (owning field is beans.evento.distrito). this is my servlet: Long id = Long.parseLong(request.getParameter("idDistrito")); response.setContentType("text/html"); EntityManager cn = emf.get().createEntityManager(); Query q = cn.createQuery("SELECT d FROM distritos d WHERE d.id = :codigo"); q.setParameter("codigo",id); distritos dist = (distritos) q.getSingleResult(); EntityManager cn2 = emf.get().createEntityManager(); cn2.getTransaction().begin(); evento e = new evento(); e.setDistrito(dist); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.persist(e); response.getWriter().write("ok"); }finally{ cn2.getTransaction().commit(); cn2.close(); } this is my entity bean Evento: @Entity public class evento implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; @Temporal(javax.persistence.TemporalType.DATE) private Date fechaCreacion; @JoinColumn @OneToMany(cascade = CascadeType.ALL) private distritos distrito; --getter and setter-- Distrito: @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; private String distrito; --getter and setter-- i hope anyone help me, Thank! Johan -- You received this message because you are subscribed to the Google Groups "Google App Engine for Java" group. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en. -- You received this message because you are subscribed to the Google Groups "Google App Engine for Java" group. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en. -- You received this message because you are subscribed to the Google Groups "Google App Engine for Java" group. To post to this group, send
[appengine-java] Re: Persistence with JPA + GAE
Hello, The Google Datastore has four ways that you can store a Primary Key ID: String, Long, and two variations on Google's own Key object. If you want to create a parent/child relationship between two persisted objects (in your case, evento is the parent and distritos is the child), then you need to use Google's Key object as the primary ID. See: http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#Keys If the class is used as a child class in a relationship, the key field must be of a type capable of representing an entity group parent: either a Key instance, or a Key value encoded as a string. So, you have two options: 1. Change your Long id values to Key id values. 2. Instead of storing private distritos distrito directly in your evento object, store a reference to the id: private Long distrito_id Jake On Feb 6, 4:06 pm, chevelle jvall...@vnperu.com wrote: Hi I am new in the list, in these days i am trying save data between two entity beans using JPA but without success, i am cheking the log nd says: Uncaught exception from servlet javax.persistence.PersistenceException: Error in meta-data for beans.distritos.id: Cannot have a java.lang.Long primary key and be a child object (owning field is beans.evento.distrito). this is my servlet: Long id = Long.parseLong(request.getParameter(idDistrito)); response.setContentType(text/html); EntityManager cn = emf.get().createEntityManager(); Query q = cn.createQuery(SELECT d FROM distritos d WHERE d.id = :codigo); q.setParameter(codigo,id); distritos dist = (distritos) q.getSingleResult(); EntityManager cn2 = emf.get().createEntityManager(); cn2.getTransaction().begin(); evento e = new evento(); e.setDistrito(dist); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.persist(e); response.getWriter().write(ok); }finally{ cn2.getTransaction().commit(); cn2.close(); } this is my entity bean Evento: @Entity public class evento implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; @Temporal(javax.persistence.TemporalType.DATE) private Date fechaCreacion; @JoinColumn @OneToMany(cascade = CascadeType.ALL) private distritos distrito; --getter and setter-- Distrito: @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; private String distrito; --getter and setter-- i hope anyone help me, Thank! Johan -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To post to this group, send email to google-appengine-j...@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
Re: [appengine-java] Re: Persistence with JPA + GAE
hi jake, thank for you answer i will try you suggestion and i will tell later El 08/02/2010 03:37 p.m., Jake escribió: Hello, The Google Datastore has four ways that you can store a Primary Key ID: String, Long, and two variations on Google's own Key object. If you want to create a parent/child relationship between two persisted objects (in your case, evento is the parent and distritos is the child), then you need to use Google's Key object as the primary ID. See: http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html#Keys If the class is used as a child class in a relationship, the key field must be of a type capable of representing an entity group parent: either a Key instance, or a Key value encoded as a string. So, you have two options: 1. Change your Long id values to Key id values. 2. Instead of storing private distritos distrito directly in your evento object, store a reference to the id: private Long distrito_id Jake On Feb 6, 4:06 pm, chevellejvall...@vnperu.com wrote: Hi I am new in the list, in these days i am trying save data between two entity beans using JPA but without success, i am cheking the log nd says: Uncaught exception from servlet javax.persistence.PersistenceException: Error in meta-data for beans.distritos.id: Cannot have a java.lang.Long primary key and be a child object (owning field is beans.evento.distrito). this is my servlet: Long id = Long.parseLong(request.getParameter(idDistrito)); response.setContentType(text/html); EntityManager cn = emf.get().createEntityManager(); Query q = cn.createQuery(SELECT d FROM distritos d WHERE d.id = :codigo); q.setParameter(codigo,id); distritos dist = (distritos) q.getSingleResult(); EntityManager cn2 = emf.get().createEntityManager(); cn2.getTransaction().begin(); evento e = new evento(); e.setDistrito(dist); Date fecha = new Date(); e.setFechaCreacion(fecha); try{ cn2.persist(e); response.getWriter().write(ok); }finally{ cn2.getTransaction().commit(); cn2.close(); } this is my entity bean Evento: @Entity public class evento implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; @Temporal(javax.persistence.TemporalType.DATE) private Date fechaCreacion; @JoinColumn @OneToMany(cascade = CascadeType.ALL) private distritos distrito; --getter and setter-- Distrito: @Entity public class distritos implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; private String distrito; --getter and setter-- i hope anyone help me, Thank! Johan -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To post to this group, send email to google-appengine-j...@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.