TABLE_PER_CLASS 双向@OneToMany 问题

发布于 2024-10-21 01:19:02 字数 2570 浏览 0 评论 0原文

Hibernate 可以使用 TABLE_PER_CLASS () 实现双向多态性,如此 此处

此策略支持一对多关联,前提是它们是双向的。

我正在尝试让一个简单的例子发挥作用。 4 个基本类:抽象 A <-- B <-- CA< /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();

在调试器中,我可以将实例 dd2(它们不是同一个实例,但具有相同的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2024-10-28 01:19:03

当将关系保存到数据库时,Hibernate 会查看拥有方。对于双向一对多关系,拥有方是“多”,即您的情况下的 Class_B

因此数据库反映了Class_B.class_d的状态,并且您应该在将Class_B关联到Class_D时设置它的值(最好创建一个方法一次设置两侧):

public class Class_D {
    ...
    public void addToMyArray(Class_B class_b) {
        myArray.add(class_b);
        class_b.setClass_D(this);
    }
}

d.addToMyArray(b);
d.addToMyArray(c);

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 associating Class_B to Class_D (it would be better to create a method to set both sides at once):

public class Class_D {
    ...
    public void addToMyArray(Class_B class_b) {
        myArray.add(class_b);
        class_b.setClass_D(this);
    }
}

d.addToMyArray(b);
d.addToMyArray(c);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文