Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA ManytoMany Bidirectional relation throwing exception...

 
Muhammad Ahsan Jamshaid
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
i am facing difficulty in managing the many to many relation with JPA in my application. The sceen is like this.... Role & Dist_list have many to many relation among each other. beside the Role and Dist_List tables the relation is placed in another table role_dist_test table{role_id,dist_list_id}


IN DistList.java relation is defined as

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "role_dist_test", joinColumns = { @JoinColumn(name = "dist_id") },
inverseJoinColumns = { @JoinColumn(name = "role_id") })
private List<Role> roleList;

In Role.java the relation is defined as

@ManyToMany(cascade = CascadeType.ALL,mappedBy="roleList")
private List<DistributionList> distLists;

The scnerio is when i creat the new instances of each other it works fine. But i want to create a dist_list and associate it with an existing role. following is my code...

Role role = rmsService.findRoleById(1);
List<Role> rList = new ArrayList<Role>();

//role.setRoleId(null);
DistributionList dist = new DistributionList();
dist.setArchived(false);
dist.setDescription("The temp");
dist.setName("Dist List 123");
dist.setStatus(true);

rList.add(role);
dist.setRoleList(rList);

rmsService.createDistributionList(dist);

it throws me ....Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.tabaq.rms.model.Role....... Can anyone help me what wrong with it...
Thanks
--Ahsan Jamshaid...
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!