This content has been marked as final.
Show 5 replies
-
1. Re: EJB-QL for @ManyToMany?
epbernard Feb 21, 2006 7:53 AM (in response to wesslan)select genre from Genre genre left outer join genre.movies
Set genres = new HashSet( query.getResultList ); -
2. Re: EJB-QL for @ManyToMany?
gus888 Feb 22, 2006 12:25 AM (in response to wesslan)Hi Emmanuel,
I also have a problem with manytomany query. Following are my codes:public class Account { ... @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY, mappedBy="accounts") public List<Group> getGroups() { return groups; } public void setGroups(List<Group> groups) { this.groups = groups; } ... } pulic class Group { ... @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER) @JoinTable(table = @Table(name = "account_groups"), joinColumns = {@JoinColumn(name = "ACCOUNT_ID")}, inverseJoinColumns = {@JoinColumn(name = "GROUP_ID")}) public List<Account> getAccounts() { return accounts; } public void setAccounts(List<Account> accounts) { this.accounts = accounts; } ... } EntityManager em; public Account getAccount(String userName) { return (Account) em.createQuery("select a from Account a left outer join a.groups where a.userName = :userName") .setParameter ("userName", userName) .getSingleResult(); }
when I run it, I always got the exceptions:java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=Laptop/58, BranchQual=, localId=58] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.HibernateException: Found shared references to a collection: com.gdocs.model.Account.groups)
I could not figure out what is wrong. Thank you very much for any advice in advance. Any helps are appreciated.
GUS -
3. Re: EJB-QL for @ManyToMany?
epbernard Feb 22, 2006 8:49 AM (in response to wesslan)The showing code cannot create such an exception AFAIK, you probably have some other code somewhere.
-
4. Re: EJB-QL for @ManyToMany?
gus888 Feb 22, 2006 10:26 PM (in response to wesslan)Hi Emmanuel, you are great. Yes, the other codes in the Group class made the exceptions. Now I fixed, but I have a new question. That is how to insert new data into the joined table, e.g. add a new group into table groups(group_id,...) and table group_members (group_id, account_id). Thank you very much.
GUS -
5. Re: EJB-QL for @ManyToMany?
gus888 Feb 22, 2006 11:13 PM (in response to wesslan)Hi Emmanul,
I got the insert method. Thank you so much.
GUS