休眠多对多数据检索 [英] Hibernate many-to-many data retrieval

查看:81
本文介绍了休眠多对多数据检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个对象User和Contact,有多对多的关系,并且我为这个关系使用了一个中间表USER_CONTACT

I have two objects User and Contact, with many to many relation, and I am using an intermediate table for this relation USER_CONTACT

保存这个关联中的数据是很好,但检索是一个问题。

Saving the data in this association is fine, but the retrieval is an issue.

我需要基于用户检索数据,但我得到的是所有用户的所有联系人。

I need to retrieve the data based on the User, but what I am getting is all the Contacts, for all the Users.

如果你能让我知道我在做什么错误,那将是一件好事。

It will be good if you can let me know what wrong I am doing.

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:

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

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>

这就是我试图检索数据的方式,我认为这是不正确的。 p>

and this is how I am trying to retrieve the data, which I think is not correct.

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

如果我可以知道如何获取基于用户的联系人,这将是一件好事。 / p>

It will be good if I can know how to go about getting the Contacts based on the User.

推荐答案

// 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;

这篇关于休眠多对多数据检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆