Reset data for many users

I have two User and Contact objects, with many for many relationships, and I use a staging table for this relationship USER_CONTACT

Saving data in this association is great, but searching is a problem.

I need to get data based on User, but I get all contacts for all users.

Well, if you can tell me what I'm doing wrong.

public class User { private Integer userID; private String userLoginEmail; private String password; private Set<Contact> contactSet = new HashSet<Contact>(); . . } public class Contact implements Serializable { private Integer contactID; private String givenName; private String familyName; private Set<User> userSet = new HashSet<User>(); . . } 

User.hbm.xml:

 <class name="User" table="USERACCOUNT"> <id column="USER_ID" length="500" name="userID"> <generator class="increment" /> </id> <property column="USER_LOGIN_EMAIL" generated="never" lazy="false" length="100" name="userLoginEmail" /> <property column="USER_FIRSTNAME" generated="never" lazy="false" length="100" name="userFirstName" /> <property column="USER_LASTNAME" generated="never" lazy="false" length="100" name="userLastName" /> <set name="contactSet" table="USER_CONTACT" inverse="false" lazy="false" fetch="select" cascade="all"> <key column="USER_ID"/> <many-to-many column="CONTACT_ID" class="com.smallworks.model.Contact"/> </set> </class> 

Contact.hbm.xml

  <class name="Contact" table="CONTACT"> <id column="CONTACT_ID" length="500" name="contactID"> <generator class="increment"/> </id> <property column="GIVEN_NAME" generated="never" lazy="false" length="100" name="givenName"/> <property column="FAMILY_NAME" generated="never" lazy="false" length="100" name="familyName"/> <!-- many to many mapping with the User via User_Contact table --> <set inverse="true" lazy="false" name="userSet" sort="unsorted" table="USER_CONTACT"> <key column="USER_ID"/> <many-to-many class="com.smallworks.model.Contact" column="CONTACT_ID" unique="false"/> </set> </class> 

and that’s how I try to get data that I think is incorrect.

 List contactList = session.createQuery("from Contact").list(); 

Well, if I can learn how to get Contacts based on the user.

0
java java-ee orm hibernate hibernate-mapping
Apr 7 '13 at 22:59
source share
1 answer
 // First, retrieve the user you want. User user = (User) session.get(User.class, user_id_you_want); // Second, get the contacts of that given user and add them to a list (optional) List contacts = new ArrayList(); contacts.addAll(user.getContactSet()); return contacts; 
+3
Apr 08 '13 at 2:45
source share
β€” -



All Articles