Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

error while persisting many to many relationship object : Attempt to assign

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a scenario of persisting object with many to many relation. I have two classes Applicaiton and Person which has many to many relations with additional column, so mapped with third table ApplicationGroupMember.

The annotations are as below
Application:

@Entity
@Table(name="APPLICATION")
public class Application{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="APP_ID", insertable=false, updatable=false)
public long applicationId;

@OneToMany(mappedBy="application",
cascade={CascadeType.REFRESH, CascadeType.MERGE, CascadeType.PERSIST})
private List<ApplicationGroupMember> persons;
.....
}

@Entity
@Table(name="PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="PRSN_ID", insertable=false, updatable=false)
private long personId;

@OneToMany(mappedBy="person",
cascade={CascadeType.REFRESH, CascadeType.MERGE, CascadeType.PERSIST})
private List<ApplicationGroupMember> applications;
}

@Entity
@Table(name="APPLICATION_GROUP_MEMBER")
@IdClass(ApplicationPersonId.class)
public class ApplicationGroupMember {
@Id
@Column(name="APP_ID", insertable=false, updatable=false)
private long applicationId;

@Id
@Column(name="PRSN_ID", insertable=false, updatable=false)
private long personId;

@ManyToOne(
cascade={CascadeType.REFRESH, CascadeType.MERGE, CascadeType.PERSIST})
@PrimaryKeyJoinColumn(name="APP_ID", referencedColumnName="APP_ID")
private Application application;

@ManyToOne(
cascade={CascadeType.REFRESH, CascadeType.MERGE, CascadeType.PERSIST})
@PrimaryKeyJoinColumn(name="PRSN_ID", referencedColumnName="PRSN_ID")
private Person person;
}

@Embeddable
public class ApplicationPersonId {

private long applicationId;
private long personId;
}

I tried to load application object with two ApplicaitonGroupMember for two Person as below

Application app = new Application();
Person person1 = new Person();
Person person2 = new Person();
ApplicationGroupMember grp = new ApplicationGroupMember();
grp.setApplication(app);
grp.setPerson(person1);
app.getPersons().add(grp);
person1.getApplications().add(grp);

ApplicationGroupMember grp2 = new ApplicationGroupMember();
grp2.setApplication(app);
grp2.setPerson(person2);
app.getPersons().add(grp2);
person2.getApplications().add(grp2);

getEntityManager().persist(application);

I am getting following error
<openjpa-1.2.0-r422266:683325 fatal user error> org.apache.openjpa.persistence.ArgumentException: Attempt to assign id "com.ApplicationGroupMember-com.ApplicationPersonId@0" to new instance "com.ApplicationGroupMember@4aa44aa4" failed; there is already an object in the L1 cache with this id. You must delete this object (in a previous transaction or the current one) before reusing its id. This error can also occur when a horizontally or vertically mapped classes uses auto-increment application identity and does not use a hierarchy of application identity classes.

I traced the sql, all insert statements are fine but the error occured only when transaction is about to commit. The above code works fine with one person.

I really appreciate, if anyone helps to fix the issue

regards
Gopinath
 
reply
    Bookmark Topic Watch Topic
  • New Topic