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[]

  • 相关阅读:
    Java当中的IO
    Java当中的类集框架
    Java当中的JVM
    Java当中的常量池
    详细讲解String和StringBuffer和StringBuilder的使用
    Java中流的操作以及编码解码
    VMware-workstation12.5.6 新建虚拟机 安装 centos6.5
    linux 常用命令
    macos 下安装brew
    mysql 的一些事
  • 原文地址:https://www.cnblogs.com/lt123/p/7244916.html
Copyright © 2011-2022 走看看