zoukankan      html  css  js  c++  java
  • hibernate中映射关系总结

    1.一对多

    父表 name,column,class顺序

    <set name="employees">
    <key column="departmentId"></key>
    <one-to-many class="Employee"/>
    </set>

    子表 (外键列)

    <many-to-one name="department" class="Department" column="departmentId"></many-to-one>

    2.一对一(基于外键的方式)

    (外键表 子表 )

    <many-to-one name="employee" class="Employee" column="employeeId" unique="true"></many-to-one>

    <one-to-one name="userAccount" class="UserAccount" property-ref="employee"></one-to-one>

    3.多对多

    Privilege.hbm.xml文件夹下

    <set name="userAccounts" table="privilege_account" >
    <key column="privilegeId"></key>
    <many-to-many class="UserAccount" column="userAccountId"></many-to-many>
    </set>

    UserAccount.hbm.xml文件夹下

    <set name="privileges" table="privilege_account" >
    <key column="userAccountId"></key>
    <many-to-many class="Privilege" column="privilegeId"></many-to-many>
    </set>

    需注意的地方

    1.<property></property>标签中name属性必填 column,type可填,可不填

    2.javabean中只能含有类,集合,不能含有外键

    private Long id;
    private String name;
    private String description;
    private Department parent;
    private Set<Department> children=new HashSet<Department>();

    3.

    几条查询语句

    String hql="from Department d where d.parent is null order by id desc";

     hql="select e.department.id,count(*) as num from Employee e group by e.department.id";

    String dml="update Employee e set e.department=? where id=?";//注意更新 不能修改任何一个表的自动生成主键

    String hql="select u.employee.id from UserAccount u where u.employee.id is not null";查询语句 ,注意

    from Employee  返回的是javabean 

    select id from employee where xx=xx 有可能是obj 也有可能是ob[]

  • 相关阅读:
    [][]
    Spark笔记04
    Spark笔记03
    Spark笔记02
    Spark笔记01
    【熟能生巧】使用Screw快速生成数据库文档
    记一次关于jdbcTemplate.queryForList快速Debug及感悟
    【从零单排】Exception实战总结1
    【从零单排】Java性能排查实战模拟
    【从零单排】关于泛型Generic的一些思考
  • 原文地址:https://www.cnblogs.com/lt123/p/7244916.html
Copyright © 2011-2022 走看看