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

  • 相关阅读:
    【Anagrams】 cpp
    【Count and Say】cpp
    【Roman To Integer】cpp
    【Integer To Roman】cpp
    【Valid Number】cpp
    重构之 实体与引用 逻辑实体 逻辑存在的形式 可引用逻辑实体 不可引用逻辑实体 散弹式修改
    Maven项目聚合 jar包锁定 依赖传递 私服
    Oracle学习2 视图 索引 sql编程 游标 存储过程 存储函数 触发器
    mysql案例~tcpdump的使用
    tidb架构~本地化安装
  • 原文地址:https://www.cnblogs.com/lt123/p/7244916.html
Copyright © 2011-2022 走看看