openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Albert Lee <allee8...@gmail.com>
Subject Re: an object of type with oid already exists in this context another cannot be persisted with @EmbeddedId
Date Thu, 17 Nov 2011 16:57:14 GMT
1) In the Execution class, each iteration has a tx begin but no commit or
rollback, is this intended?
2) Since it is complaining about id conflict and most of the entities uses
sequence id generation, you may want to enable openjpa.Log=SQL=TRACE to see
when and how many "SELECT seq NEXTVAL" (or something of this nature)
happened

Albert
On Thu, Nov 17, 2011 at 10:20 AM, sergioflores <checoforo_2@hotmail.com>wrote:

> Hi,
> I'm have a little problem with JPA.
> I can't persist an entity with an @EmbeddedId on cascade.
>
> This are my entities....
>
> ====================================
> *Project.java*
>
> @Entity
> public class Project {
>
>  @Id
>  @SequenceGenerator(name="PROJECT_GENERATOR" )
>  @GeneratedValue(strategy=GenerationType.SEQUENCE,
> generator="PROJECT_GENERATOR")
>  @Column(name="ID_PROJECT")
>  private long idProyecto;
>
>  private String nombre;
>
>  public void setIdProyecto(long id) {
>
>    this.idProyecto = id;
>  }
>
>  public long getIdProyecto() {
>
>    return this.idProyecto;
>  }
>
>  public void setNombre(String nombre) {
>
>    this.nombre = nombre;
>  }
>
>  public String getNombre() {
>
>    return this.nombre;
>  }
>
>  @OneToMany(mappedBy = "project", cascade = CascadeType.ALL)
>  private List<Asignation> listaAsignacion;
>
>  public void setListaAsignacion(List<Asignation> listaAsignacion) {
>
>    this.listaAsignacion = listaAsignacion;
>  }
>
>  public List<Asignation> getListaAsignacion() {
>
>    return this.listaAsignacion;
>  }
>
> }
>
> *Asignation.java*
>
> @Entity
> public class Asignation {
>
>  @EmbeddedId
>  private AsignationPk pk;
>
>  private String indicadorEstatus;
>
>  @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
>  @JoinColumn(name="ID_PROJECT", nullable=false)
>  private Project project;
>
>  @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
>  @JoinColumn(name="ID_PROFESSOR", nullable=false)
>  private Professor professor;
>
>  public AsignationPk getAsignacionPk(){
>    return pk;
>  }
>
>  public void setAsignationPk(AsignationPk pk){
>    this.pk = pk;
>  }
>
>  public String getIndicadorEstatus() {
>    return indicadorEstatus;
>  }
>
>  public void setIndicadorEstatus(String inIndicadorEstatus) {
>    indicadorEstatus = inIndicadorEstatus;
>  }
>
>  public void setProject(Project proyecto){
>    this.project = proyecto;
>  }
>
>  public Project getProject(){
>    return this.project;
>  }
>
>  public void setProfesor(Professor professor){
>    this.professor = professor;
>  }
>
>  public Professor getProfessor(){
>    return this.professor;
>  }
>
> }
>
> *AsignationPk.java*
>
> @Embeddable
> public class AsignationPk {
>
>  @Column(name="ID_PROJECT")
>  private Long idProyecto;
>
>  @Column(name="ID_PROFESSOR")
>  private Long idProfesor;
>
>  public void setIdProyecto(long id) {
>
>    this.idProyecto = id;
>  }
>
>  public long getIdProyecto() {
>
>    return this.idProyecto;
>  }
>
>  public void setIdProfesor(long id) {
>
>    this.idProfesor = id;
>  }
>
>  public long getIdProfesor() {
>
>    return this.idProfesor;
>  }
>
> }
>
> *Professor.java*
>
> @Entity
> public class Professor {
>
>  @Id
>  @SequenceGenerator(name="PROFESSOR_GENERATOR" )
>  @GeneratedValue(strategy=GenerationType.SEQUENCE,
> generator="PROFESSOR_GENERATOR")
>  @Column(name="ID_PROFESSOR")
>  private long idProfesor;
>
>  public void setIdProfesor(long id) {
>
>    this.idProfesor = id;
>  }
>
>  public long getIdProfesor() {
>
>    return this.idProfesor;
>  }
>
>  @OneToMany(mappedBy = "professor", cascade = CascadeType.ALL)
>  private List<Asignation> listaAsignacion;
>
>  public List<Asignation> getListaAsignacion() {
>
>    return listaAsignacion;
>  }
>
>  public void setListaAsignacion(List<Asignation> listaAsignacion) {
>
>    this.listaAsignacion = listaAsignacion;
>  }
>
> }
>
>
> *And this is the main....*
>
> public class Execution {
>
>  public static void main(String[] args) {
>
>    EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("PUProject");
>    EntityManager em = emf.createEntityManager();
>
>    for (int x = 0; x < 4; x++) {
>
>      try {
>        em.getTransaction().begin();
>
>        Project project = new Project();
>        project.setNombre("Project Name");
>
>        List<Asignation> listaAsignacion = new ArrayList<Asignation>();
>
>        /* I set the idProfesor and the reference to project object which
> will be persisted,
>         * because I don't know the idProject */
>        AsignationPk pk = new AsignationPk();
>        pk.setIdProfesor(1L);
>        Asignation asignation = new Asignation();
>        asignation.setAsignationPk(pk);
>        asignation.setProject(project);
>        // I = Inactive
>        // A = Active
>        asignation.setIndicadorEstatus("A");
>
>        listaAsignacion.add(asignation);
>
>        project.setListaAsignacion(listaAsignacion);
>
>      } catch (Exception e) {
>        e.printStackTrace();
>      }
>
>    }
>
>  }
>
> }
>
>
> This is my persistence.xml
> <?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="PUProject"
> transaction-type="RESOURCE_LOCAL">
>
>                <properties>
>                        <property name="javax.persistence.jdbc.url"
> value="jdbc:oracle:thin:@x.x.x.x:1521:orcl"/>
>                        <property name="javax.persistence.jdbc.user"
> value="user"/>
>                        <property name="javax.persistence.jdbc.password"
> value="1234"/>
>                        <property name="javax.persistence.jdbc.driver"
> value="oracle.jdbc.OracleDriver"/>
>
>
>                        <property name="openjpa.jdbc.SynchronizeMappings"
> value="buildSchema(ForeignKeys=true)" />
>                        <property name="openjpa.ConnectionFactoryMode"
> value="managed" />
>                        <property name="openjpa.jdbc.DBDictionary"
> value="oracle(DriverVendor=oracle)" />
>                        <property name="openjpa.LockManager"
> value="pessimistic" />
>                        <property name="openjpa.jdbc.Schema" value="schema"
> />
>                        <property name="openjpa.RuntimeUnenhancedClasses"
> value="supported"/>
>                        <property name="openjpa.jdbc.SchemaFactory"
> value="native"/>
>                </properties>
>
>
>        </persistence-unit>
> </persistence>
>
>
> *Problem...*
>
> I'm triying to persist a Project entity on cascade, as you can see, 4
> times.
> The first time this entity is persisted correctly, but the second time it
> doesn't work, and on third work and quarter doesn't work.
> This is, one time yes and one time no.
>
> I get this error...
>
>
> <openjpa-2.0.1-r422266:989424 nonfatal store error>
> org.apache.openjpa.persistence.EntityExistsException: An object of type
> "package.Asignation" with oid "package.Asignation-package.AsignationPk@0"
> already exists in this context; another cannot be persisted.
> FailedObject: package.Asignation@14a014a
>        at
>
> org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(BrokerImpl.java:4958)
>        at
> org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2584)
>        at
> org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2504)
>        at
> org.apache.openjpa.kernel.BrokerImpl.persistAll(BrokerImpl.java:2451)
>        at
>
> org.apache.openjpa.kernel.SingleFieldManager.persist(SingleFieldManager.java:268)
>        at
>
> org.apache.openjpa.kernel.StateManagerImpl.cascadePersist(StateManagerImpl.java:3020)
>        at
> org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2600)
>        at
> org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2423)
>        at
>
> org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1069)
>        at
>
> org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:706)
>        at Execution.main(Execution.java:136)
>
> Why is this happen? Is this a bug?
>
> I'm using openJPA 2.0.1
> Oracle 11
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/an-object-of-type-with-oid-already-exists-in-this-context-another-cannot-be-persisted-with-EmbeddedId-tp7004684p7004684.html
> Sent from the OpenJPA Developers mailing list archive at Nabble.com.
>



-- 
Albert Lee.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message