[jboss-user] [EJB 3.0] - EJB3 (Hibernate3) Many-to-Many annotation question?

ericmacau do-not-reply at jboss.com
Tue Aug 12 23:41:17 EDT 2008


Hello,

How can I define the annotation in the following situation?


  | class User {
  | @ManyToMany(fetch = FetchType.EAGER, 
  | 			targetEntity=Role.class)
  | 	@JoinTable(
  |         name="USER_ROLE_LINK",
  |         joinColumns={@JoinColumn(name="username")},
  |         inverseJoinColumns={@JoinColumn(name="rolename")}
  |     )
  | 	public Collection<Role> getRoles() {
  | 		return roles;
  | 	}
  | 
  | }
  | 


  | class Role {
  | @ManyToMany(
  | 		targetEntity=GenericUser.class,
  | 		mappedBy="roles",
  | 		fetch = FetchType.EAGER)
  | 	@JoinTable(
  |         name="USER_ROLE_LINK",
  |         joinColumns={@JoinColumn(name="rolename")},
  |         inverseJoinColumns={@JoinColumn(name="username")}
  |     )
  |     @XmlElement
  | 	public Collection<User> getUsers() {
  | 		return users;
  | 	}
  | 
  | }
  | 


The above two classes will create three tables (USER, ROLE, USER_ROLE_LINK) in database.


when I tried to delete the USER, it will delete one record from table USER_ROLE_LINK and USER. It is correct. But when I tried to delete one record from ROLE, it raised some exception: [B]something like cannot delete or update parent entity ...[/B].

How can I define the annotations so that when I want to remove an entity from USER and ROLE, it also can remove all the related records in USER_ROLE_LINK;

for example,

USER
=====
user1
user2


ROLE
====
role1
role2


USER_ROLE_LINK
============
user1      role1
user1      role2
user2      role1



In the above table records, if I want to delete 'user1', it should remove 'user1' from USER and two records from USER_ROLE_LINK that are 'user1'

And if I want to remove 'role1', it will remove 'role1' from ROLE and two records from USER_ROLE_LINK that are 'role1'.






View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4170210#4170210

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4170210



More information about the jboss-user mailing list