1

我有三个实体,主要(用户)实体与其他两个实体相关,我如何使用休眠在一个查询中从数据库中检索三个实体的列表

package hib.test;

import java.util.HashSet;
import java.util.Set;

public class Country {
    private Integer id;
    private String country;
    private Set<User> userList = new HashSet<User>();

    public Country() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public Set<User> getUserList() {
        return userList;
    }

    public void setUserList(Set<User> userList) {
        this.userList = userList;
    }

}

用户.java

package hib.test;

public class User {
    private Integer id;
    private UserType userType;
    private Country country;

    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public UserType getUserType() {
        return userType;
    }

    public void setUserType(UserType userType) {
        this.userType = userType;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

}

用户类型.java

package hib.test;

import java.util.HashSet;
import java.util.Set;

public class UserType {
    private Integer id;
    private String userType;
    private Set<User> userList = new HashSet<User>();

    public UserType() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public Set<User> getUserList() {
        return userList;
    }

    public void setUserList(Set<User> userList) {
        this.userList = userList;
    }

}

国家.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 6, 2012 1:12:01 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="hib.test.Country" table="COUNTRY">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="country" type="java.lang.String">
            <column name="COUNTRY" />
        </property>
        <set name="userList" table="USER" inverse="false" lazy="true">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="hib.test.User" />
        </set>
    </class>
</hibernate-mapping>

用户.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 6, 2012 1:12:01 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="hib.test.User" table="USER">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <many-to-one name="userType" class="hib.test.UserType" fetch="join">
            <column name="USERTYPE" />
        </many-to-one>
        <many-to-one name="country" class="hib.test.Country" fetch="join">
            <column name="COUNTRY" />
        </many-to-one>
    </class>
</hibernate-mapping>

用户类型.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 6, 2012 1:12:01 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="hib.test.UserType" table="USERTYPE">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="userType" type="java.lang.String">
            <column name="USERTYPE" />
        </property>
        <set name="userList" table="USER" inverse="false" lazy="true">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="hib.test.User" />
        </set>
    </class>
</hibernate-mapping>

我怎样才能检索List<User>List<Country>List<UserType>通过一个查询

编辑

public static List<UserType> getUserTypeList() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    List<UserType> list = null;
    try {
        transaction = session.beginTransaction();
        list = session.createQuery("from UserType as u").list();
        if (list != null) {
            for (UserType uType : list)
                Hibernate.initialize(uType.getUserList());
        }
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
    return list;
}

实际上,我有 1 个 JTable 和两个用于 UserType 和 Country 的组合框因此,当我在组合框中选择任何数据时,JTable 数据应根据所选值进行过滤,并在内存中保存所选的 UserType 和 Country 对象。

4

3 回答 3

0

您需要三个查询来做到这一点:

select u from User u;
select ut from UserType ut;
select c from Country c;

编辑:

如果你真正想要的是所有用户类型的列表,每个用户类型的用户,每个用户类型的每个用户的国家,所有这些都加载在一个查询中,那么你需要获取连接,如中所述休眠文档:

select userType from UserType userType
left join fetch userType.users user
left join fetch user.country
于 2012-06-06T07:45:05.057 回答
0

如果您想要一次关系数据并且内存不是问题,请执行lazy="false"

于 2012-06-06T07:49:55.597 回答
0

尝试在您的用户映射上将获取类型定义为 EAGER,这样当您加载用户时,它将加载国家和用户类型。

于 2012-06-06T07:50:42.903 回答