在一个类中休眠许多一对多关系

时间:2012-06-06 07:39:40

标签: java hibernate

我有三个实体和Main(User)enitiy与其他两个实体有关,如何在一个查询中使用hibernate从数据库中检索三个实体的列表

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

}

User.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;
    }

}

UserType.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;
    }

}

country.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>

user.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>

usertype.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;
}

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

3 个答案:

答案 0 :(得分:0)

您需要三个查询才能执行此操作:

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

编辑:

如果您真正想要的是所有用户类型的列表,每个用户类型的用户以及每个用户类型的每个用户的国家/地区,所有这些都在一个查询中加载,那么您需要获取联接,正如Hibernate documentation

中所述
select userType from UserType userType
left join fetch userType.users user
left join fetch user.country

答案 1 :(得分:0)

如果您想立即获得关系数据并且内存不是问题,请执行lazy="false"

答案 2 :(得分:0)

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