简体   繁体   中英

Hibernate many One To Many relation in one class

i have three entity and Main(User) enitiy is in relation with other two entity,how can i retrive list of three entities from database in one query using 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>

How can i retrive List<User> , List<Country> and List<UserType> with one query

EDIT

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

Actually I have 1 JTable and Two combo box each for UserType and Country So when i select any data in combo box JTable data should be filter according to selected value and in memory it shoud be save selected UserType and Country object.

You need three queries to do that:

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

EDIT:

If what you actually want is a list of all the user types, with the users of each user type, and the country of each user of each user type, all this loaded in a single query, then you need fetch joins, as explained in the Hibernate documentation :

select userType from UserType userType
left join fetch userType.users user
left join fetch user.country

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM