`

HQL 关联查询简单实现_hibernate_left_right HQL

阅读更多


public class Emp {
    private int id;
    private String name;
    private Dept dept;

}
public class Dept {

    private int id;
    private String name;
    private Set<Emp> emps;

}


//##################################
    <class name="com.tom.Emp" >
       <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" />
        <many-to-one name="dept" column="dept_id" class="com.tom.Dept" />
    </class>
//##################################
    <class name="com.tom.Dept" >
       <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" />
       
        <set name="emps">
           <key column="dept_id"/>
           <one-to-many class="com.tom.Emp"/>
        </set>
    </class>

//##################################
INSERT INTO `dept` (`id`,`name`) VALUES
 (1,'技术部1'),
 (2,'技术部2'),
 (3,'财务部门');

 INSERT INTO `emp` (`id`,`name`,`dept_id`) VALUES
 (1,'tom',1),
 (2,'jerry',1),
 (3,'tom2',2),
 (4,'jerry2',2),
 (5,'kaka',NULL);



//String hql = "select d.name , d.emps.name from com.tom.Dept d";
//String hql = "select e.dept.name ,e.name from com.tom.Emp e right join e.dept";
//String hql = "select e.dept.name ,e.name from com.tom.Emp e left join e.dept";
//for(Object[] o : oo){
// System.out.println("部门名称"+o[0]+"雇员名称"+o[1]);
//}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics