Hibernate关联映射
集合映射 开发流程: 需求分析/数据库设计、项目设计/ 编码/测试/实施部署上线/验收需求: 用户购买, 填写地址! // javabean设计 public class User { private int userId; private String userName; // 一个用户,对应的多个地址 private Set String addre
集合映射
<code>开发流程: 需求分析/数据库设计、项目设计/ 编码/测试/实施部署上线/验收 需求: 用户购买, 填写地址! </code>
<code class=" hljs lasso"><span class="hljs-comment">// javabean设计</span> <span class="hljs-keyword">public</span> class User { <span class="hljs-keyword">private</span> int userId; <span class="hljs-keyword">private</span> <span class="hljs-built_in">String</span> userName; <span class="hljs-comment">// 一个用户,对应的多个地址</span> <span class="hljs-keyword">private</span> <span class="hljs-built_in">Set</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span> address; <span class="hljs-keyword">private</span> <span class="hljs-built_in">List</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span> addressList <span class="hljs-subst">=</span> <span class="hljs-literal">new</span> ArrayList<span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span>(); <span class="hljs-comment">//private String[] addressArray; // 映射方式和list一样 <array name=""></array></span> <span class="hljs-keyword">private</span> <span class="hljs-built_in">Map</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span>,<span class="hljs-built_in">String</span><span class="hljs-subst">></span> addressMap <span class="hljs-subst">=</span> <span class="hljs-literal">new</span> HashMap<span class="hljs-subst"><</span><span class="hljs-built_in">String</span>, <span class="hljs-built_in">String</span><span class="hljs-subst">></span>(); } </code>
<code class=" hljs xml"><span class="hljs-comment"><!-- 映射文件 --></span> <span class="hljs-pi"><?xml version="1.0"?></span> <span class="hljs-doctype"><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"></span> <span class="hljs-tag"><<span class="hljs-title">hibernate-mapping</span> <span class="hljs-attribute">package</span>=<span class="hljs-value">"cn.itcast.a_collection"</span>></span> <span class="hljs-tag"><<span class="hljs-title">class</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"User"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_user"</span>></span> <span class="hljs-tag"><<span class="hljs-title">id</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"userId"</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"id"</span>></span> <span class="hljs-tag"><<span class="hljs-title">generator</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"native"</span>></span><span class="hljs-tag"></<span class="hljs-title">generator</span>></span> <span class="hljs-tag"></<span class="hljs-title">id</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"userName"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-comment"><!-- set集合属性的映射 name 指定要映射的set集合的属性 table 集合属性要映射到的表 key 指定集合表(t_address)的外键字段 element 指定集合表的其他字段 type 元素类型,一定要指定 --></span> <span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"address"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_address"</span>></span> <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"uid"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span> <span class="hljs-tag"><<span class="hljs-title">element</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"address"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"string"</span>></span><span class="hljs-tag"></<span class="hljs-title">element</span>></span> <span class="hljs-tag"></<span class="hljs-title">set</span>></span> <span class="hljs-comment"><!-- list集合映射 list-index 指定的是排序列的名称 (因为要保证list集合的有序) --></span> <span class="hljs-tag"><<span class="hljs-title">list</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"addressList"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_addressList"</span>></span> <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"uid"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span> <span class="hljs-tag"><<span class="hljs-title">list-index</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"idx"</span>></span><span class="hljs-tag"></<span class="hljs-title">list-index</span>></span> <span class="hljs-tag"><<span class="hljs-title">element</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"address"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"string"</span>></span><span class="hljs-tag"></<span class="hljs-title">element</span>></span> <span class="hljs-tag"></<span class="hljs-title">list</span>></span> <span class="hljs-comment"><!-- map集合的映射 key 指定外键字段 map-key 指定map的key element 指定map的value --></span> <span class="hljs-tag"><<span class="hljs-title">map</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"addressMap"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_addressMap"</span>></span> <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"uid"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span> <span class="hljs-tag"><<span class="hljs-title">map-key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"shortName"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"string"</span> ></span><span class="hljs-tag"></<span class="hljs-title">map-key</span>></span> <span class="hljs-tag"><<span class="hljs-title">element</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"address"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"string"</span> ></span><span class="hljs-tag"></<span class="hljs-title">element</span>></span> <span class="hljs-tag"></<span class="hljs-title">map</span>></span> <span class="hljs-tag"></<span class="hljs-title">class</span>></span> <span class="hljs-tag"></<span class="hljs-title">hibernate-mapping</span>></span></code>
<code class="java hljs "> <span class="hljs-comment">// 测试程序</span> <span class="hljs-comment">// 保存set</span> <span class="hljs-annotation">@Test</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSaveSet</span>() <span class="hljs-keyword">throws</span> Exception { Session session = sf.openSession(); session.beginTransaction(); <span class="hljs-comment">//-- 保存</span> Set<String> addressSet = <span class="hljs-keyword">new</span> HashSet<String>(); addressSet.add(<span class="hljs-string">"广州"</span>); addressSet.add(<span class="hljs-string">"深圳"</span>); <span class="hljs-comment">// 用户对象</span> User user = <span class="hljs-keyword">new</span> User(); user.setUserName(<span class="hljs-string">"Jack"</span>); user.setAddress(addressSet); <span class="hljs-comment">// 保存</span> session.save(user); session.getTransaction().commit(); session.close(); } <span class="hljs-comment">// 保存list/map</span> <span class="hljs-annotation">@Test</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSaveList</span>() <span class="hljs-keyword">throws</span> Exception { Session session = sf.openSession(); session.beginTransaction(); User user = <span class="hljs-keyword">new</span> User(); user.setUserName(<span class="hljs-string">"Tom"</span>); <span class="hljs-comment">// // 用户对象 -- list</span> <span class="hljs-comment">// user.getAddressList().add("广州");</span> <span class="hljs-comment">// user.getAddressList().add("深圳");</span> <span class="hljs-comment">// // 保存</span> <span class="hljs-comment">// session.save(user);</span> <span class="hljs-comment">// 用户对象 -- Map</span> user.getAddressMap().put(<span class="hljs-string">"A0001"</span>, <span class="hljs-string">"广州"</span>); user.getAddressMap().put(<span class="hljs-string">"A0002"</span>, <span class="hljs-string">"深圳"</span>); <span class="hljs-comment">// 保存</span> session.save(user); session.getTransaction().commit(); session.close(); } </code>
<code>问题: 集合映射,映射的集合元素,都是普通的类型, 能否为对象类型? </code>
关联映射
<code>需求1: 部门与员工 一个部门有多个员工; 【一对多】 多个员工,属于一个部门 【多对一】 需求2: 项目与开发员工 一个项目,有多个开发人员! 一个开发人员,参与多个项目! 【多对多】 </code>
多对一映射与一对多
<code>1. 需求:员工与部门 2. 数据库: 3. 设计javabean封装: 4. 映射: </code>
JavaBean
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Dept { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> deptId; <span class="hljs-keyword">private</span> String deptName; <span class="hljs-comment">// 【一对多】 部门对应的多个员工</span> <span class="hljs-keyword">private</span> Set<Employee> emps = <span class="hljs-keyword">new</span> HashSet<Employee>(); } <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Employee { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> empId; <span class="hljs-keyword">private</span> String empName; <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> salary; <span class="hljs-comment">// 【多对一】员工与部门</span> <span class="hljs-keyword">private</span> Dept dept; }</code>
Dept.hbm.xml
<code class=" hljs xml"><span class="hljs-pi"><?xml version="1.0"?></span> <span class="hljs-doctype"><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"></span> <span class="hljs-tag"><<span class="hljs-title">hibernate-mapping</span> <span class="hljs-attribute">package</span>=<span class="hljs-value">"cn.itcast.b_one2Many"</span>></span> <span class="hljs-tag"><<span class="hljs-title">class</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"Dept"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_dept"</span>></span> <span class="hljs-tag"><<span class="hljs-title">id</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"deptId"</span>></span> <span class="hljs-tag"><<span class="hljs-title">generator</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"native"</span>></span><span class="hljs-tag"></<span class="hljs-title">generator</span>></span> <span class="hljs-tag"></<span class="hljs-title">id</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"deptName"</span> <span class="hljs-attribute">length</span>=<span class="hljs-value">"20"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-comment"><!-- 一对多关联映射配置 (通过部门管理到员工) Dept 映射关键点: 1. 指定 映射的集合属性: "emps" 2. 集合属性对应的集合表: "t_employee" 3. 集合表的外键字段 "t_employee. dept_id" 4. 集合元素的类型 --></span> <span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"emps"</span>></span> <span class="hljs-comment"><!-- table="t_employee" --></span> <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"dept_id"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span> <span class="hljs-tag"><<span class="hljs-title">one-to-many</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"Employee"</span>/></span> <span class="hljs-tag"></<span class="hljs-title">set</span>></span> <span class="hljs-tag"></<span class="hljs-title">class</span>></span> <span class="hljs-tag"></<span class="hljs-title">hibernate-mapping</span>></span></code>
Employee.hbm.xml
<code class=" hljs xml"><span class="hljs-pi"><?xml version="1.0"?></span> <span class="hljs-doctype"><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"></span> <span class="hljs-tag"><<span class="hljs-title">hibernate-mapping</span> <span class="hljs-attribute">package</span>=<span class="hljs-value">"cn.itcast.b_one2Many"</span>></span> <span class="hljs-tag"><<span class="hljs-title">class</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"Employee"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_employee"</span>></span> <span class="hljs-tag"><<span class="hljs-title">id</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"empId"</span>></span> <span class="hljs-tag"><<span class="hljs-title">generator</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"native"</span>></span><span class="hljs-tag"></<span class="hljs-title">generator</span>></span> <span class="hljs-tag"></<span class="hljs-title">id</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"empName"</span> <span class="hljs-attribute">length</span>=<span class="hljs-value">"20"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"salary"</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"double"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-comment"><!-- 多对一映射配置 Employee 映射关键点: 1. 映射的部门属性 : dept 2. 映射的部门属性,对应的外键字段: dept_id 3. 部门的类型 --></span> <span class="hljs-tag"><<span class="hljs-title">many-to-one</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"dept"</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"dept_id"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"Dept"</span>></span><span class="hljs-tag"></<span class="hljs-title">many-to-one</span>></span> <span class="hljs-tag"></<span class="hljs-title">class</span>></span> <span class="hljs-tag"></<span class="hljs-title">hibernate-mapping</span>></span></code>
测试
<code class=" hljs java"> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> {</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> SessionFactory sf; <span class="hljs-keyword">static</span> { sf = <span class="hljs-keyword">new</span> Configuration() .configure() .addClass(Dept.class) .addClass(Employee.class) <span class="hljs-comment">// 测试时候使用</span> .buildSessionFactory(); } <span class="hljs-comment">// 保存, 部门方 【一的一方法操作】</span> <span class="hljs-annotation">@Test</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">save</span>() { Session session = sf.openSession(); session.beginTransaction(); <span class="hljs-comment">// 部门对象</span> Dept dept = <span class="hljs-keyword">new</span> Dept(); dept.setDeptName(<span class="hljs-string">"应用开发部"</span>); <span class="hljs-comment">// 员工对象</span> Employee emp_zs = <span class="hljs-keyword">new</span> Employee(); emp_zs.setEmpName(<span class="hljs-string">"张三"</span>); Employee emp_ls = <span class="hljs-keyword">new</span> Employee(); emp_ls.setEmpName(<span class="hljs-string">"李四"</span>); <span class="hljs-comment">// 关系</span> dept.getEmps().add(emp_zs); dept.getEmps().add(emp_ls); <span class="hljs-comment">// 保存</span> session.save(emp_zs); session.save(emp_ls); session.save(dept); <span class="hljs-comment">// 保存部门,部门下所有的员工 </span> session.getTransaction().commit(); session.close(); <span class="hljs-comment">/* * 结果 * Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?) Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?) Hibernate: insert into t_dept (deptName) values (?) Hibernate: update t_employee set deptId=? where empId=? 维护员工引用的部门的id Hibernate: update t_employee set deptId=? where empId=? */</span> } <span class="hljs-comment">// 【推荐】 保存, 部员方 【多的一方法操作】</span> <span class="hljs-annotation">@Test</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">save2</span>() { Session session = sf.openSession(); session.beginTransaction(); <span class="hljs-comment">// 部门对象</span> Dept dept = <span class="hljs-keyword">new</span> Dept(); dept.setDeptName(<span class="hljs-string">"综合部"</span>); <span class="hljs-comment">// 员工对象</span> Employee emp_zs = <span class="hljs-keyword">new</span> Employee(); emp_zs.setEmpName(<span class="hljs-string">"张三"</span>); Employee emp_ls = <span class="hljs-keyword">new</span> Employee(); emp_ls.setEmpName(<span class="hljs-string">"李四"</span>); <span class="hljs-comment">// 关系</span> emp_zs.setDept(dept); emp_ls.setDept(dept); <span class="hljs-comment">// 保存</span> session.save(dept); <span class="hljs-comment">// 先保存一的方法</span> session.save(emp_zs); session.save(emp_ls);<span class="hljs-comment">// 再保存多的一方,关系回自动维护(映射配置完)</span> session.getTransaction().commit(); session.close(); <span class="hljs-comment">/* * 结果 * Hibernate: insert into t_dept (deptName) values (?) Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?) Hibernate: insert into t_employee (empName, salary, dept_id) values (?, ?, ?) 少生成2条update sql */</span> } }</code>
总结
<code> 在一对多与多对一的关联关系中,保存数据最好的通过多的一方来维护关系,这样可以减少update语句的生成,从而提高hibernate的执行效率! 配置一对多与多对一,这种叫“双向关联” 只配置一对多, 叫“单项一对多” 只配置多对一, 叫“单项多对一” 注意: 配置了哪一方,哪一方才有维护关联关系的权限! </code>
Inverse属性
<code>Inverse属性,是在维护关联关系的时候起作用的。 表示控制权是否转移。(在一的一方起作用) Inverse , 控制反转。 Inverse = false 不反转; 当前方有控制权 True 控制反转; 当前方没有控制权 维护关联关系中,是否设置inverse属性: 1. 保存数据 有影响。 如果设置控制反转,即inverse=true, 然后通过部门方维护关联关系。在保存部门的时候,同时保存员工, 数据会保存,但关联关系不会维护。即外键字段为NULL </code>
<code> 2. 获取数据 无。 3. 解除关联关系? 有影响。 inverse=false, 可以解除关联 inverse=true, 当前方(部门)没有控制权,不能解除关联关系 (不会生成update语句,也不会报错) 4. 删除数据对关联关系的影响? 有影响。 inverse=false, 有控制权, 可以删除。先清空外键引用,再删除数据。 inverse=true, 没有控制权: 如果删除的记录有被外键引用,会报错,违反主外键引用约束! 如果删除的记录没有被引用,可以直接删除。 </code>
cascade 属性
<code>cascade 表示级联操作 【可以设置到一的一方或多的一方】 none 不级联操作, 默认值 save-update 级联保存或更新 delete 级联删除 save-update,delete 级联保存、更新、删除 all 同上。级联保存、更新、删除 </code>
多对多映射
<code>需求:项目与开发人员 Project Developer 电商系统 曹吉 王春 OA系统 王春 老张 </code>
JavaBean
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Developer { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> d_id; <span class="hljs-keyword">private</span> String d_name; <span class="hljs-comment">// 开发人员,参数的多个项目</span> <span class="hljs-keyword">private</span> Set<Project> projects = <span class="hljs-keyword">new</span> HashSet<Project>(); } <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Project { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> prj_id; <span class="hljs-keyword">private</span> String prj_name; <span class="hljs-comment">// 项目下的多个员工</span> <span class="hljs-keyword">private</span> Set<Developer> developers = <span class="hljs-keyword">new</span> HashSet<Developer>(); }</code>
Project.hbm.xml
<code class=" hljs xml"><span class="hljs-pi"><?xml version="1.0"?></span> <span class="hljs-doctype"><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"></span> <span class="hljs-tag"><<span class="hljs-title">hibernate-mapping</span> <span class="hljs-attribute">package</span>=<span class="hljs-value">"cn.itcast.c_many2many"</span>></span> <span class="hljs-tag"><<span class="hljs-title">class</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"Project"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_project"</span>></span> <span class="hljs-tag"><<span class="hljs-title">id</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"prj_id"</span>></span> <span class="hljs-tag"><<span class="hljs-title">generator</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"native"</span>></span><span class="hljs-tag"></<span class="hljs-title">generator</span>></span> <span class="hljs-tag"></<span class="hljs-title">id</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"prj_name"</span> <span class="hljs-attribute">length</span>=<span class="hljs-value">"20"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-comment"><!-- 多对多映射: 1. 映射的集合属性: “developers” 2. 集合属性,对应的中间表: “t_relation” 3. 外键字段: prjId 4. 外键字段,对应的中间表字段: did 5. 集合属性元素的类型 --></span> <span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"developers"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_relation"</span> <span class="hljs-attribute">cascade</span>=<span class="hljs-value">"save-update"</span>></span> <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"prjId"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span> <span class="hljs-tag"><<span class="hljs-title">many-to-many</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"did"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"Developer"</span>></span><span class="hljs-tag"></<span class="hljs-title">many-to-many</span>></span> <span class="hljs-tag"></<span class="hljs-title">set</span>></span> <span class="hljs-tag"></<span class="hljs-title">class</span>></span> <span class="hljs-tag"></<span class="hljs-title">hibernate-mapping</span>></span></code>
Developer.hbm.xml
<code class=" hljs xml"><span class="hljs-pi"><?xml version="1.0"?></span> <span class="hljs-doctype"><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"></span> <span class="hljs-tag"><<span class="hljs-title">hibernate-mapping</span> <span class="hljs-attribute">package</span>=<span class="hljs-value">"cn.itcast.c_many2many"</span>></span> <span class="hljs-tag"><<span class="hljs-title">class</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"Developer"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_developer"</span>></span> <span class="hljs-tag"><<span class="hljs-title">id</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"d_id"</span>></span> <span class="hljs-tag"><<span class="hljs-title">generator</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"native"</span>></span><span class="hljs-tag"></<span class="hljs-title">generator</span>></span> <span class="hljs-tag"></<span class="hljs-title">id</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"d_name"</span> <span class="hljs-attribute">length</span>=<span class="hljs-value">"20"</span>></span><span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-comment"><!-- 多对多映射配置: 员工方 name 指定映射的集合属性 table 集合属性对应的中间表 key 指定中间表的外键字段(引用当前表t_developer主键的外键字段) many-to-many column 指定外键字段对应的项目字段 class 集合元素的类型 --></span> <span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"projects"</span> <span class="hljs-attribute">table</span>=<span class="hljs-value">"t_relation"</span>></span> <span class="hljs-tag"><<span class="hljs-title">key</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"did"</span>></span><span class="hljs-tag"></<span class="hljs-title">key</span>></span> <span class="hljs-tag"><<span class="hljs-title">many-to-many</span> <span class="hljs-attribute">column</span>=<span class="hljs-value">"prjId"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"Project"</span>></span><span class="hljs-tag"></<span class="hljs-title">many-to-many</span>></span> <span class="hljs-tag"></<span class="hljs-title">set</span>></span> <span class="hljs-tag"></<span class="hljs-title">class</span>></span> <span class="hljs-tag"></<span class="hljs-title">hibernate-mapping</span>></span></code>
维护关联关系
<code>设置inverse属性,在多对多种维护关联关系的影响? 1) 保存数据 有影响。 inverse=false ,有控制权,可以维护关联关系; 保存数据的时候会把对象关系插入中间表; inverse=true, 没有控制权, 不会往中间表插入数据。 2) 获取数据 无。 3) 解除关系 有影响。 inverse=false ,有控制权, 解除关系就是删除中间表的数据。 inverse=true, 没有控制权,不能解除关系。 4) 删除数据 有影响。 inverse=false, 有控制权。 先删除中间表数据,再删除自身。 inverse=true, 没有控制权。 如果删除的数据有被引用,会报错! 否则,才可以删除 </code>

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

Video Face Swap
Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

Dieses KI-gestützte Programmiertool hat in dieser Phase der schnellen KI-Entwicklung eine große Anzahl nützlicher KI-gestützter Programmiertools zu Tage gefördert. KI-gestützte Programmiertools können die Entwicklungseffizienz verbessern, die Codequalität verbessern und Fehlerraten reduzieren. Sie sind wichtige Helfer im modernen Softwareentwicklungsprozess. Heute wird Dayao Ihnen 4 KI-gestützte Programmiertools vorstellen (und alle unterstützen die C#-Sprache). https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot ist ein KI-Codierungsassistent, der Ihnen hilft, Code schneller und mit weniger Aufwand zu schreiben, sodass Sie sich mehr auf Problemlösung und Zusammenarbeit konzentrieren können. Git

Mit der Popularität des mobilen Internets hat sich Toutiao zu einer der beliebtesten Nachrichteninformationsplattformen in meinem Land entwickelt. Viele Benutzer hoffen, mehrere Konten auf der Toutiao-Plattform zu haben, um unterschiedlichen Anforderungen gerecht zu werden. Wie eröffnet man also mehrere Toutiao-Konten? In diesem Artikel werden die Methode und der Antragsprozess zur Eröffnung mehrerer Toutiao-Konten ausführlich vorgestellt. 1. Wie eröffne ich mehrere Toutiao-Konten? Die Methode zur Eröffnung mehrerer Toutiao-Konten ist wie folgt: Auf der Toutiao-Plattform können Benutzer Konten über verschiedene Mobiltelefonnummern registrieren. Jede Mobiltelefonnummer kann nur ein Toutiao-Konto registrieren, was bedeutet, dass Benutzer mehrere Mobiltelefonnummern verwenden können, um mehrere Konten zu registrieren. 2. E-Mail-Registrierung: Verwenden Sie verschiedene E-Mail-Adressen, um ein Toutiao-Konto zu registrieren. Ähnlich wie bei der Registrierung einer Mobiltelefonnummer kann auch jede E-Mail-Adresse ein Toutiao-Konto registrieren. 3. Melden Sie sich mit einem Drittanbieterkonto an

Es ist schwierig, sammlungsähnliche Funktionen in der Go-Sprache zu implementieren, was viele Entwickler beschäftigt. Im Vergleich zu anderen Programmiersprachen wie Python oder Java verfügt die Go-Sprache nicht über integrierte Sammlungstypen wie Set, Map usw., was Entwickler bei der Implementierung von Sammlungsfunktionen vor einige Herausforderungen stellt. Schauen wir uns zunächst an, warum es schwierig ist, sammlungsähnliche Funktionen direkt in der Go-Sprache zu implementieren. In der Go-Sprache sind die am häufigsten verwendeten Datenstrukturen Slice und Map. Sie können jedoch sammlungsähnliche Funktionen ausführen

Am 3. März 2022, weniger als einen Monat nach der Geburt von Devin, dem weltweit ersten KI-Programmierer, entwickelte das NLP-Team der Princeton University einen Open-Source-KI-Programmierer-SWE-Agenten. Es nutzt das GPT-4-Modell, um Probleme in GitHub-Repositorys automatisch zu lösen. Die Leistung des SWE-Agenten auf dem SWE-Bench-Testsatz ist ähnlich wie die von Devin, er benötigt durchschnittlich 93 Sekunden und löst 12,29 % der Probleme. Durch die Interaktion mit einem dedizierten Terminal kann der SWE-Agent Dateiinhalte öffnen und durchsuchen, die automatische Syntaxprüfung verwenden, bestimmte Zeilen bearbeiten sowie Tests schreiben und ausführen. (Hinweis: Der obige Inhalt stellt eine geringfügige Anpassung des Originalinhalts dar, die Schlüsselinformationen im Originaltext bleiben jedoch erhalten und überschreiten nicht die angegebene Wortbeschränkung.) SWE-A

Tutorial zur Entwicklung mobiler Anwendungen in der Go-Sprache Da der Markt für mobile Anwendungen weiterhin boomt, beginnen immer mehr Entwickler damit, sich mit der Verwendung der Go-Sprache für die Entwicklung mobiler Anwendungen zu befassen. Als einfache und effiziente Programmiersprache hat die Go-Sprache auch großes Potenzial für die Entwicklung mobiler Anwendungen gezeigt. In diesem Artikel wird detailliert beschrieben, wie die Go-Sprache zum Entwickeln mobiler Anwendungen verwendet wird, und es werden spezifische Codebeispiele angehängt, um den Lesern den schnellen Einstieg und die Entwicklung eigener mobiler Anwendungen zu erleichtern. 1. Vorbereitung Bevor wir beginnen, müssen wir die Entwicklungsumgebung und die Tools vorbereiten. Kopf

In der heutigen schnelllebigen Gesellschaft plagen immer mehr Menschen Probleme mit der Schlafqualität. Um die Schlafqualität der Nutzer zu verbessern, erschien auf der Douyin-Plattform eine Gruppe spezieller Schlafanker. Sie interagieren mit den Nutzern über Live-Übertragungen, geben Tipps zum Einschlafen und sorgen mit entspannender Musik und Geräuschen dafür, dass die Zuschauer ruhig einschlafen können. Sind diese Schlafanker also profitabel? Dieser Artikel konzentriert sich auf dieses Problem. 1. Lohnt es sich, ein Douyin-Schlafanker zu sein? Mit Douyin-Schlafankern lassen sich tatsächlich gewisse Gewinne erzielen. Erstens können sie über die Trinkgeldfunktion im Live-Übertragungsraum Geschenke und Transfers erhalten. Diese Vorteile hängen von der Anzahl ihrer Fans und der Zufriedenheit des Publikums ab. Zweitens gewährt die Douyin-Plattform dem Moderator einen bestimmten Anteil basierend auf der Anzahl der Aufrufe, Likes, Shares und anderen Daten der Live-Übertragung. Einige Schlafanker werden es auch tun

Die Android-Entwicklung ist eine arbeitsreiche und spannende Aufgabe, und die Auswahl einer geeigneten Linux-Distribution für die Entwicklung ist besonders wichtig. Welche der vielen Linux-Distributionen eignet sich am besten für die Android-Entwicklung? In diesem Artikel wird dieses Problem unter verschiedenen Aspekten untersucht und spezifische Codebeispiele aufgeführt. Werfen wir zunächst einen Blick auf einige derzeit beliebte Linux-Distributionen: Ubuntu, Fedora, Debian, CentOS usw. Sie alle haben ihre eigenen Vorteile und Eigenschaften.

Als schnelle und effiziente Programmiersprache erfreut sich Go im Bereich der Backend-Entwicklung großer Beliebtheit. Allerdings assoziieren nur wenige Menschen die Go-Sprache mit der Front-End-Entwicklung. Tatsächlich kann die Verwendung der Go-Sprache für die Front-End-Entwicklung nicht nur die Effizienz verbessern, sondern Entwicklern auch neue Horizonte eröffnen. In diesem Artikel wird die Möglichkeit der Verwendung der Go-Sprache für die Front-End-Entwicklung untersucht und spezifische Codebeispiele bereitgestellt, um den Lesern ein besseres Verständnis dieses Bereichs zu erleichtern. In der traditionellen Frontend-Entwicklung werden häufig JavaScript, HTML und CSS zum Erstellen von Benutzeroberflächen verwendet
