Cascade query for Hibernate single entity

Source: Internet
Author: User
In fact, one-to-many and many-to-one and 2-entity usage is the same, now take the user as an example:

Java code:

Package Com.test.bean;

Import Java.util.Set;

public class User
{
Private Integer ID;
Private String FirstName;
Private String LastName;
Private Integer ParentID;
private int age;
Private Set Childrens;
private user user;

Public Set Getchildrens () {
return childrens;
}

public void Setchildrens (Set childrens) {
This.childrens = childrens;
}

Public User GetUser () {
return user;
}

public void SetUser (user user) {
This.user = user;
}

Public Integer getId ()
{
return ID;
}

public void SetId (Integer ID)
{
This.id = ID;
}

Public String Getfirstname ()
{
return FirstName;
}

public void Setfirstname (String firstname)
{
This.firstname = FirstName;
}

Public String Getlastname ()
{
return lastname;
}

public void Setlastname (String lastname)
{
This.lastname = LastName;
}

public int Getage ()
{
return age;
}

public void Setage (int.)
{
This.age = age;
}

Public Integer Getparentid () {
return parentid;
}

public void Setparentid (Integer parentid) {
This.parentid = ParentID;
}

}


The corresponding HBM files:

<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<class name= "Com.test.bean.User" table= "Users" >
<id name= "id" type= "java.lang.Integer" column= "id" >
<generator class= "Increment" ></generator>
</id>

<property name= "FirstName" type= "string" column= "FirstName"
Length= ">"
</property>
<property name= "LastName" Type= "string" column= "LastName"
Length= ">"
</property>
<property name= "Age" type= "Java.lang.Integer" column= "Age" ></property>

<set name= "childrens" inverse= "true"
Lazy= "false" where= "age=4" >
<key>
<column name= "ParentID"/>
</key>
<one-to-many class= "Com.test.bean.User"/>
</set>
<many-to-one name= "user" column= "parentid" class= "Com.test.bean.User" ></many-to-one>

</class>

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.