在集合中添加具有自动化的ID的对象会导致仅通过使用Hibernate添加一个元素
我想创建更多对象并将它们添加到集合中。 HashCode和Equals方法取决于ID(我无法更改)。
首先,每个对象具有ID = 0,并且在尝试添加到集合时将仅添加第一个对象。
如果我在每次创建后都使用session.save(对象),则该集合将包含所有元素。
问题是未创建ID。
您对此有任何解决方案吗?每次创建对象都不容易保存,因为要这样做,我需要更改很多代码。
我从基类添加了等价和哈希码 谢谢你
public class Employee extend HibernateEntity{
public Employee(String name){
em_name = name;
}
}
//doesn't work because id=0 for both, it will add only the first one
Set set = new HashSet<Employee>();
set.add(Employee("lala"));
set.add(Employee("baba"));
works because save will provide a generated id and the object will not be the same
Set set = new HashSet<Employee>();
Employee em1 = Employee("lala");
session.save(em1);
set.add(em1);
Employee em2 = Employee("baba");
session.save(em2);
set.add(em2);
等于基类别的标准:
@MappedSuperclass
public abstract class HibernateEntity extends HibernateObject implements Cloneable {
/* Primary key */
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence-generator")
protected long id;
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public final boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof HibernateEntity)) {
return false;
}
obj = HibernateHelper.getRealObject(obj);
Object thisImpl = HibernateHelper.getRealObject(this);
if (thisImpl.getClass() != obj.getClass()) {
return false;
}
// It's now safe to cast object
final HibernateEntity other = (HibernateEntity) obj;
final HibernateEntity thisHibernteImpl = (HibernateEntity) thisImpl;
if (thisHibernteImpl.id != other.id) {
return false;
}
return true;
}
}
I want to create more objects and add them to a set. The hashcode and equals methods are dependent on id ( I can't change that ).
To start with, each object has id=0 and when trying to add to the set will result in adding only the first object.
If I am using session.save(object) after each creation, the set will contain all elements.
The problem is that the id isn't created.
Do you have any solution for this? Saving each time I create an object is not easy because to do this I need to change a lot of code.
I added equals and hashcode from base class
Thank you
public class Employee extend HibernateEntity{
public Employee(String name){
em_name = name;
}
}
//doesn't work because id=0 for both, it will add only the first one
Set set = new HashSet<Employee>();
set.add(Employee("lala"));
set.add(Employee("baba"));
works because save will provide a generated id and the object will not be the same
Set set = new HashSet<Employee>();
Employee em1 = Employee("lala");
session.save(em1);
set.add(em1);
Employee em2 = Employee("baba");
session.save(em2);
set.add(em2);
Equals and hashcode from base class:
@MappedSuperclass
public abstract class HibernateEntity extends HibernateObject implements Cloneable {
/* Primary key */
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence-generator")
protected long id;
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public final boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof HibernateEntity)) {
return false;
}
obj = HibernateHelper.getRealObject(obj);
Object thisImpl = HibernateHelper.getRealObject(this);
if (thisImpl.getClass() != obj.getClass()) {
return false;
}
// It's now safe to cast object
final HibernateEntity other = (HibernateEntity) obj;
final HibernateEntity thisHibernteImpl = (HibernateEntity) thisImpl;
if (thisHibernteImpl.id != other.id) {
return false;
}
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论