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> 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
--