TABLE_PER_CLASS 双向@OneToMany 问题
Hibernate 可以使用 TABLE_PER_CLASS
(
) 实现双向多态性,如此 此处:
此策略支持一对多关联,前提是它们是双向的。
我正在尝试让一个简单的例子发挥作用。 4 个基本类:抽象 A <-- B <-- C 和 A< /strong> <-- D,其中 D 保存着 B 的 myArray
。
我没有收到任何错误; myArray
根本就是空的。
详细信息
A
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Class_A {
@Id
public int myId = 0;
public int a = 0;
}
B
@Entity
public class Class_B extends Class_A {
public int b = 0;
@ManyToOne
@JoinColumn(name="join_column_b")
private Class_D class_d;
}
C
@Entity
public class Class_C extends Class_B {
public int c = 0;
}
D
@Entity
public class Class_D extends Class_A {
@OneToMany(cascade=CascadeType.ALL, mappedBy="class_d", fetch=FetchType.EAGER)
public List<Class_B> myArray;
public Class_D() {
myArray = new ArrayList<Class_B>();
}
}
代码
// Definition.
Class_B b = new Class_B(); b.myId = 9;
Class_C c = new Class_C(); c.myId = 18;
Class_D d = new Class_D(); d.myId = 92;
d.myArray.add(b);
d.myArray.add(c);
// Saving.
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(d);
session.getTransaction().commit();
session.close();
// Loading.
Session session2 = sessionFactory.openSession();
session2.beginTransaction();
Class_D d2 = (Class_D)session2.createQuery("from " + Class_D.class.getName()).uniqueResult();
session2.getTransaction().commit();
session2.close();
在调试器中,我可以将实例 d
与d2
(它们不是同一个实例,但具有相同的myId
)。 d2
有一个空的 myArray
。
没有例外 - 没有 Oracle 错误 - 没有任何错误
为什么?
备注:
1.Hibernate 3.6.0 Final + Oracle 11g + 纯Java
2.使用注解
3.我已经审查了这个和这个但仍然无法让这个简单的示例工作(ffs!)
4. 下周日我会更新 - 如果到那时我没有回复请原谅我
Hibernate can do bidirectional polymorphism with TABLE_PER_CLASS
(<union-subclass>
), says so here:
This strategy supports one-to-many associations provided that they are bidirectional.
I'm trying to get a simple example to work. 4 basic classes: abstract A <-- B <-- C and A <-- D, where D is holding myArray
of B's.
I get no error; myArray
is simply empty.
Details
A
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Class_A {
@Id
public int myId = 0;
public int a = 0;
}
B
@Entity
public class Class_B extends Class_A {
public int b = 0;
@ManyToOne
@JoinColumn(name="join_column_b")
private Class_D class_d;
}
C
@Entity
public class Class_C extends Class_B {
public int c = 0;
}
D
@Entity
public class Class_D extends Class_A {
@OneToMany(cascade=CascadeType.ALL, mappedBy="class_d", fetch=FetchType.EAGER)
public List<Class_B> myArray;
public Class_D() {
myArray = new ArrayList<Class_B>();
}
}
Code
// Definition.
Class_B b = new Class_B(); b.myId = 9;
Class_C c = new Class_C(); c.myId = 18;
Class_D d = new Class_D(); d.myId = 92;
d.myArray.add(b);
d.myArray.add(c);
// Saving.
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(d);
session.getTransaction().commit();
session.close();
// Loading.
Session session2 = sessionFactory.openSession();
session2.beginTransaction();
Class_D d2 = (Class_D)session2.createQuery("from " + Class_D.class.getName()).uniqueResult();
session2.getTransaction().commit();
session2.close();
In the debugger I can compare instance d
with d2
(they are not the same instance, but have the same myId
). d2
has an empty myArray
.
No exception - No Oracle error - No nothing
Why?
Notes:
1. Hibernate 3.6.0 Final + Oracle 11g + pure Java
2. Using annotations
3. I've reviewed this and this but still can't get this simple example to work (ffs!)
4. I'll be able to update next on Sunday - please forgive me if I don't reply until then
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当将关系保存到数据库时,Hibernate 会查看拥有方。对于双向一对多关系,拥有方是“多”,即您的情况下的
Class_B
。因此数据库反映了
Class_B.class_d
的状态,并且您应该在将Class_B
关联到Class_D
时设置它的值(最好创建一个方法一次设置两侧):When saving relationship to the database Hibernate looks at the owning side. For bidirectional one-to-many relationships the owning side is "many", i.e.
Class_B
in your case.Therefore database reflects state of
Class_B.class_d
, and you should set its value when associatingClass_B
toClass_D
(it would be better to create a method to set both sides at once):