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"/> <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.
java java-ee orm hibernate hibernate-mapping
Harbir Apr 7 '13 at 22:59 2013-04-07 22:59
source share