知识点
相关文章
更多最近更新
更多hibernate中one2Many双向
2019-03-28 21:43|来源: 领悟书生
在hibernate中ManyToOne单向中,我们说过班级和学生的关系,如果要使得他们是one2many双向关系,应该如何实现呢?
首先,修改Classroom.java和Classroom.hbm.xml映射文件
package org.zttc.itat.model; import java.util.Set; public class Classroom { private int id; private String name; private int grade; private Set<Student> stus; //set get 方法省略 }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="org.zttc.itat.model"> <class name="Classroom" table="t_cla"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <property name="grade"/> <!-- inverse=true表示不再自己这一端维护关系 --> <set name="stus" lazy="extra" inverse="true"> <key column="cid"/> <one-to-many class="Student"/> </set> </class> </hibernate-mapping>
看看测试类:
@Test public void testAdd01() { Session session = null; try { session = HibernateUtil.openSession(); session.beginTransaction(); Student stu1 = new Student(); stu1.setName("猪八戒"); stu1.setNo("1111"); session.save(stu1); Student stu2 = new Student(); stu2.setName("朱纠结"); stu2.setNo("2222"); session.save(stu2); Classroom cla = new Classroom(); cla.setGrade(2012); cla.setName("计算机应用技术"); Set<Student> stus = new HashSet<Student>(); stus.add(stu1); stus.add(stu2); cla.setStus(stus); session.save(cla); /* * 此时也会发出5条sql(三条添加,两条更新), * 所以最佳实践就是不要使用1的一方来维护关系 * 在配置文件的set标签中可以通过inverse=true * 来明确不使用一的这一端维护关系(此时只有三天添加SQL) */ session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); if(session!=null) session.getTransaction().rollback(); } finally { HibernateUtil.close(session); } } @Test public void testAdd02() { Session session = null; try { session = HibernateUtil.openSession(); session.beginTransaction(); //先添加一,再添加多 Classroom cla = new Classroom(); cla.setGrade(2012); cla.setName("计算机网络技术"); session.save(cla); Student stu1 = new Student(); stu1.setName("猪八戒"); stu1.setNo("1111"); stu1.setClassroom(cla); session.save(stu1); Student stu2 = new Student(); stu2.setName("朱纠结"); stu2.setNo("2222"); stu2.setClassroom(cla); session.save(stu2); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); if(session!=null) session.getTransaction().rollback(); } finally { HibernateUtil.close(session); } } @Test public void testLoad02() { Session session = null; try { session = HibernateUtil.openSession(); session.beginTransaction(); // Classroom cla = (Classroom)session.load(Classroom.class, 6); // 会执行一条select count(id)..的语句 // System.out.println(cla.getStus().size()); Student stu = (Student)session.load(Student.class, 15); System.out.println(stu.getName()+","+stu.getClassroom().getGrade()); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); if(session!=null) session.getTransaction().rollback(); } finally { HibernateUtil.close(session); } }
本文链接:hibernate中one2Many双向,由领悟书生原创(笔记),转载请注明出处http://www.656463.com/article/399
相关问答
更多-
您需要从函数return [res.id] ID列表,如return [res.id] 您必须从搜索方法中删除self 。 这就是你错误的原因。 ids = obj.search(cr, uid, []) 根据您的代码,这是我的建议。 You need to return list of IDs from your function like this return [res.id] You have to remove self from search method. That is the reaso ...
-
如何在OpenERP7的one2many字段中设置特定的行顺序?(How to set a specific lines order in a one2many field in OpenERP7?)[2023-09-23]
最后,我找到了一种管理方法,下载此模块: https://www.odoo.com/apps/modules/7.0/one2many_sorted/ 然后,我覆盖了我要改变其顺序的one2many,在我的情况下,字段是child_ids,我希望它通过电子邮件订购: 'child_ids':one2many_sorted.one2many_sorted('res.partner','parent_id','Contacts',order ='email',) 请注意,此字段与one2many之间唯一不同的是 ... -
暂时我正在做一个session.update(父)。 这使CHILD表中存在的外键和孤立记录无效。 在我手动完成了孤儿的session.delete(...)之后。 这更像是一种解决方法,而不是解决方案。 For the time being I am doing an session.update(parent). Which nullify the foreign keys and orphan records exist in the CHILD table. And there after I do ...
-
在xml中,如果你想为需要python表达式的字段定义一个值,你需要使用eval : 假设你在xmlid =“record_id”的数据库中有一条记录,所以在xml文件中定义一个many2one字段的值给这条记录:
or 所以在你的情况下试试这个,因为我从来没有尝试过它: 通常,您不应序列化您的实体。 循环依赖和代理使得这很难。 相反,您应手动将需要发送的数据传输到DTO(新的仅数据类),并将其序列化。 它不会有懒惰的集合,代理和诸如此类的东西。 In general, you should not serialize your entities. Circular dependencies and proxies make that hard. Instead, you should manually transfer the data you need to send t ...尝试修改集而不是替换它。 即: quotation.getQuotationItem().clear(); quotation.getQuotationItem().addAll(newSet); 这是由于一些冬眠的怪异。 除此之外,还有一点关于命名。 您的Quotation.quotationItem应该命名为quotationItems因为它包含多个QuotationItem 。 Try to modify the set rather than replace it. i.e.: ...您可以在视图中设置以禁用所有记录的删除。 否则就没有办法对此施加条件。 您尝试覆盖unlink()的方式只是这样做的方法。 您可以在方法中检查布尔字段值并引发错误相应。 @api.multi def unlink(self): for rec in self: if rec.your_boolean_field : raise UserError(_('In order to delete a record, you mus ... 首先创建扩展ListView模板的xml文件,以在基本列表视图模板中添加colspan功能。 colspan.xml你好我正在回答这个问题,你必须在suitcrm做同样的事情。 我检查了Campaign Module中只有一对一的关系。 因此,尝试使用一对多关系创建自定义子面板 1.创建一个新的链接类 这应该进入自定义/模块// YourNewLink.php,这个类将充当自定义功能,将在两个记录之间建立链接。数据库设计,one2many,表之间的双向依赖(Database design, one2many, bidirectional dependency between tables)[2023-04-09]
贷款模式: public class Loan { [Key] public int LoanId { get; set; } [Required] public DateTime WhenBorrowed { get; set; } public DateTime? WhenReturned { get; set; } // foreign key to Tool public virtual ICollection...