Hibernate 一对多出现低级问题因为po实现了hashcode和equals方法这里设置的Id做比较...

本文介绍如何使用Hibernate实现树状结构的一对多双向关联,并通过示例代码解释如何处理树节点间的父子关系及在插入数据库时遇到的问题。

1.Hibernate中一对多的双向关联
     <?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class table="T_Orgnization" name="com.love.oa.model.Orgnization">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<property name="sn" />
<many-to-one name="parent"  column="pid"/>//多对一  *------1
<set name="chilren" inverse="true" cascade="all">//一对多 1----*
<key column="pid"/>
<one-to-many class="com.love.oa.model.Orgnization"/>
</set>

</class>
</hibernate-mapping>

 

 

package com.love.oa.model;

 

import java.io.Serializable;

import java.util.Set;

 

/**

 * @hibernate.class table="T_Orgnization"

 */

public class Orgnization implements Serializable {

/**

* @hibernate.id

* generator-class="native"

*/

private int id;

/**

* @hibernate.property

*/

private String name;

/**

* @hibernate.property

*/

private String sn;

/**

* @hibernate.many-to-one

* column="pid"

*/

private Orgnization parent;

/**

* @hibernate.set

* @hibernate.key column="pid"

* @hibernate.one-to-many class="com.love.oa.model.Orgnization"

*/

private Set chilren;

 

public int getId() {

return id;

}

 

public void setId(int id) {

this.id = id;

}

 

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

public String getSn() {

return sn;

}

 

public void setSn(String sn) {

this.sn = sn;

}

 

public Orgnization getParent() {

return parent;

}

 

public void setParent(Orgnization parent) {

this.parent = parent;

}

 

public Set getChilren() {

return chilren;

}

 

public void setChilren(Set chilren) {

this.chilren = chilren;

}

 

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + id;

return result;

}

 

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

final Orgnization other = (Orgnization) obj;

if (id != other.id)

return false;

return true;

}

}

 

我在测试插入数据库的时候

 

@SuppressWarnings("unchecked")

public void testAddOrgination() throws Exception {

 

Session session = baseDao.getSessionFactory().openSession();

Transaction transaction = session.beginTransaction();

Set hashSet = new HashSet();

Orgnization org1 = new Orgnization();

org1.setName("开发部");

org1.setSn("111");

org1.setParent(null);

Orgnization org2 = new Orgnization();

org2.setName("开发部1");

org2.setSn("222");

org2.setParent(org1);

hashSet.add(org2);

//se.save(org2);

 

Orgnization org3 = new Orgnization();

org3.setName("开发部2"); 

org3.setSn("333");

org3.setParent(org1);

//se.save(org3);

hashSet.add(org3);

System.out.println(hashSet.size());

//这里的结果始终是1,因为po实现了hashcode和equals方法这里设置的Id做比较,这里就会出现一个插入数据库的时候少一条数据,因为Set集合不能插入重复的数据,这里我要实现树状结构,所以ID是相同的,造成了插入的数据不对。悲剧了啊!真的悲剧。。。

org1.setChilren(hashSet);

session.save(org1);

transaction.commit();

session.close();

}


 

在以下场景中,通常必须重写`hashCode``equals`方法: ### 使用自定义对象作为哈希表的键 当使用自定义对象作为`HashMap`、`HashSet`等哈希表的键时,必须重写`hashCode``equals`方法。`HashMap`在存储元素时,先根据键的`hashCode`值确定元素在数组中的位置,当发生哈希冲突时,再使用`equals`方法来判断两个键是否真正相等。如果不重写这两个方法,即使两个对象的内容相同,也可能因为它们的`hashCode`不同或者`equals`方法判断不相等,而被存储在不同的位置,或者被视为不同的键,导致哈希表无法正常工作。例如: ```java import java.util.HashMap; class CustomKey { private int id; private String name; public CustomKey(int id, String name) { this.id = id; this.name = name; } // 重写equals方法 @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CustomKey customKey = (CustomKey) o; return id == customKey.id && name.equals(customKey.name); } // 重写hashCode方法 @Override public int hashCode() { int result = id; result = 31 * result + name.hashCode(); return result; } } public class Main { public static void main(String[] args) { HashMap<CustomKey, String> map = new HashMap<>(); CustomKey key1 = new CustomKey(1, "example"); CustomKey key2 = new CustomKey(1, "example"); map.put(key1, "value"); System.out.println(map.get(key2)); // 输出 "value",因为重写了hashCodeequals方法 } } ``` ### 业务系统中需要业务上的对象相等判断 在业务系统中,有时候需要的不是一种严格意义上的相等(即`object1 == object2`),而是一种业务上的对象相等。在这种情况下,原生的`equals`方法就不能满足需求,所以需要重写`equals`方法。同时,为了保证在使用哈希表等数据结构时的一致性,也需要重写`hashCode`方法。例如,在一个学生管理系统中,判断两个学生对象是否相等可能只需要比较他们的学号,而不需要比较其他属性: ```java class Student { private int studentId; private String name; public Student(int studentId, String name) { this.studentId = studentId; this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return studentId == student.studentId; } @Override public int hashCode() { return studentId; } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值