Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate many One To Many relation in one class
    primarykey
    data
    text
    <p>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</p> <pre><code>package hib.test; import java.util.HashSet; import java.util.Set; public class Country { private Integer id; private String country; private Set&lt;User&gt; userList = new HashSet&lt;User&gt;(); 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&lt;User&gt; getUserList() { return userList; } public void setUserList(Set&lt;User&gt; userList) { this.userList = userList; } } </code></pre> <p>User.java</p> <pre><code>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; } } </code></pre> <p>UserType.java</p> <pre><code>package hib.test; import java.util.HashSet; import java.util.Set; public class UserType { private Integer id; private String userType; private Set&lt;User&gt; userList = new HashSet&lt;User&gt;(); 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&lt;User&gt; getUserList() { return userList; } public void setUserList(Set&lt;User&gt; userList) { this.userList = userList; } } </code></pre> <p>country.hbm.xml</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;!-- Generated Jun 6, 2012 1:12:01 PM by Hibernate Tools 3.4.0.CR1 --&gt; &lt;hibernate-mapping&gt; &lt;class name="hib.test.Country" table="COUNTRY"&gt; &lt;id name="id" type="java.lang.Integer"&gt; &lt;column name="ID" /&gt; &lt;generator class="assigned" /&gt; &lt;/id&gt; &lt;property name="country" type="java.lang.String"&gt; &lt;column name="COUNTRY" /&gt; &lt;/property&gt; &lt;set name="userList" table="USER" inverse="false" lazy="true"&gt; &lt;key&gt; &lt;column name="ID" /&gt; &lt;/key&gt; &lt;one-to-many class="hib.test.User" /&gt; &lt;/set&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>user.hbm.xml</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;!-- Generated Jun 6, 2012 1:12:01 PM by Hibernate Tools 3.4.0.CR1 --&gt; &lt;hibernate-mapping&gt; &lt;class name="hib.test.User" table="USER"&gt; &lt;id name="id" type="java.lang.Integer"&gt; &lt;column name="ID" /&gt; &lt;generator class="assigned" /&gt; &lt;/id&gt; &lt;many-to-one name="userType" class="hib.test.UserType" fetch="join"&gt; &lt;column name="USERTYPE" /&gt; &lt;/many-to-one&gt; &lt;many-to-one name="country" class="hib.test.Country" fetch="join"&gt; &lt;column name="COUNTRY" /&gt; &lt;/many-to-one&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>usertype.hbm.xml</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;!-- Generated Jun 6, 2012 1:12:01 PM by Hibernate Tools 3.4.0.CR1 --&gt; &lt;hibernate-mapping&gt; &lt;class name="hib.test.UserType" table="USERTYPE"&gt; &lt;id name="id" type="java.lang.Integer"&gt; &lt;column name="ID" /&gt; &lt;generator class="assigned" /&gt; &lt;/id&gt; &lt;property name="userType" type="java.lang.String"&gt; &lt;column name="USERTYPE" /&gt; &lt;/property&gt; &lt;set name="userList" table="USER" inverse="false" lazy="true"&gt; &lt;key&gt; &lt;column name="ID" /&gt; &lt;/key&gt; &lt;one-to-many class="hib.test.User" /&gt; &lt;/set&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>How can i retrive <code>List&lt;User&gt;</code>, <code>List&lt;Country&gt;</code> and <code>List&lt;UserType&gt;</code> with one query</p> <p>EDIT</p> <pre><code>public static List&lt;UserType&gt; getUserTypeList() { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = null; List&lt;UserType&gt; 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; } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload