openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sergioflores <checofor...@hotmail.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 17:30:28 GMT
Hello, thanks a lot...
I just change the Execution.java to this...

============================
public class Execution {

  public static void main(String[] args) {

    EntityManagerFactory emf =
Persistence.createEntityManagerFactory("correo");
    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 object project 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);
        
        em.persist(project);
        
        em.getTransaction().commit();

      } catch (Exception e) {
        e.printStackTrace();
        em.getTransaction().rollback();
      }

    }

  }

}
==============================
And it doesn't work.
I just enabled
openjpa.Log=SQL=TRACE...
this is the trace....

========================
72  correo  WARN   [main] openjpa.Runtime - The configuration property named
"openjpa.jdbc.UpdateManager " was not recognized and will be ignored,
although the name closely matches a valid property called
"openjpa.jdbc.UpdateManager".
510  correo  INFO   [main] openjpa.Runtime - OpenJPA dynamically loaded the
class enhancer. Any classes that were not enhanced at build time will be
enhanced when they are loaded by the JVM.
570  correo  INFO   [main] openjpa.Runtime - Starting OpenJPA 2.0.1
8913  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn 132384740>
executing prepstmnt 1359433991 SELECT SCHEMA.PROJECT_GENERATOR.NEXTVAL FROM
DUAL
8914  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn 132384740>
[1 ms] spent
9123  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn 661071719>
executing prepstmnt 1780443679 INSERT INTO SCHEMA.PROJECT (ID_PROJECT,
NOMBRE) VALUES (?, ?) [params=?, ?]
9132  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn 661071719>
[9 ms] spent
9134  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn 661071719>
executing prepstmnt 65078241 INSERT INTO SCHEMA.ASIGNATION (ID_PROJECT,
ID_PROFESSOR, IND_ESTATUS) VALUES (?, ?, ?) [params=?, ?, ?]
9138  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn 661071719>
[4 ms] spent
<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@44c044c
	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:132)
23772  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn
1695442190> executing prepstmnt 2039118218 SELECT
SCHEMA.PROJECT_GENERATOR.NEXTVAL FROM DUAL
23774  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn
1695442190> [2 ms] spent
23967  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn
125765503> executing prepstmnt 1306086873 INSERT INTO SCHEMA.PROJECT
(ID_PROJECT, NOMBRE) VALUES (?, ?) [params=?, ?]
23977  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn
125765503> [10 ms] spent
23983  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn
125765503> executing prepstmnt 1741187016 INSERT INTO SCHEMA.ASIGNATION
(ID_PROJECT, ID_PROFESSOR, IND_ESTATUS) VALUES (?, ?, ?) [params=?, ?, ?]
23984  correo  TRACE  [main] openjpa.jdbc.SQL - <t 992754476, conn
125765503> [1 ms] spent
<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@44c044c
	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:132)
============================


allee8285 wrote:
> 
> 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.
> 
> 
Quoted from: 
http://openjpa.208410.n2.nabble.com/an-object-of-type-with-oid-already-exists-in-this-context-another-cannot-be-persisted-with-EmbeddedId-tp7004684p7004853.html


--
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-tp7004684p7004990.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.

Mime
View raw message