Hibernate多态:实例化正确的类 [英] Hibernate polymorphism: instantiating the right class

查看:137
本文介绍了Hibernate多态:实例化正确的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如你很快就会看到的,我刚刚接触hibernate。我很抱歉,如果这个问题有一个简单的答案,但我只是不熟悉所有的术语,很容易找到它。



假设我有一个基类我使用Hibernate映射的A和一个子类B,可能使用每个子类的表策略。基类不是抽象的。所有B都是As,但不是所有B都是。这反映在数据库中,表B引用表A。



好吧,现在假设我有一个显示A对象列表的程序。用户可以选择任何A对象并将其带到屏幕上以修改它。但是,如果A对象也是B,屏幕将允许用户修改B而不是A。



在这个世界上我该如何处理这个问题?



注意:我不是在询问如何确定什么类对象是。我在问什么是如何让hibernate返回一个合适类的对象列表。

我再次为这个问题道歉。我对hibernate的工作原理感到非常惊讶,这真的很酷。我不认为它会自动完成这一切,我甚至不确定我想问什么。当我回应评论时,我开始在脑海中细化这个问题,然后能够找到我正在寻找的答案。感谢所有帮助过我的人。



答案是:hibernate会自动执行此操作。

假设您有具有主键id的数据库表A和具有引用表A的主键a_id的表B.所以,您创建以下类(缩写):

  public class A {
private String aProperty;
// ... getter和setter等
{

public class B extends A {
private String bProperty;
// ... getter和setter等
}

然后映射他们像这样:

 < hibernate-mapping> 
< class name =Atable =acatalog =testokdelete>
< id name =idtype =java.lang.Integer>
< column name =id/>
< generator class =identity/>
< / id>
< property name =aPropertycolumn =a_property/>
< joined-subclass name =Btable =b>
< key column =a_id/>
< property name =bPropertycolumn =b_property/>
< / joined-subclass>
< / class>
< / hibernate-mapping>

您可以使用简单的from A查询返回A和B对象,如下所示代码:

 查询查询= session.createQuery(from A); 
列表< Object> objects = query.list();
for(Object object:objects){
System.out.print(A property:);
System.out.print(((A)object).getAProperty());
System.out.print(,B property:);
System.out.println((object.getClass()== B.class)?((B)object).getBProperty():not a B);





$ b它们所做的就是使用查询from A返回一个对象列表,然后遍历它们,从A类打印出一个属性,如果类是B类,则从B类中获得bProperty。



这个hibernate查询大小写是自动多态的,在适当的时候会给你一个B对象。


I'm new to hibernate, as you'll soon see. I apologize if this question has an easy answer, but I'm just not familiar enough with all of the terminology to find it easily.

Let's say I have a base class "A" and one subclass "B" that I'm mapping with Hibernate, perhaps using the table per subclass strategy. The base class is not abstract. All Bs are As, but not all As are Bs. This is reflected in the database, where table B references table A.

Ok, now suppose I have a program of some sort that displays a list of A objects. The user can select any A object and be taken to a screen to modify it...BUT, if the A object is also a B, the screen will allow the user to modify B instead of just A.

How in the world do I approach this?

Note: I'm not asking about how to determine what class an object is. What I'm asking is how do I get hibernate to return a list of objects that are of the proper class.

解决方案

I apologize again for this question. I'm very surprised at how hibernate works, it's really cool. I didn't think it would do all of this automagically, and I really wasn't even sure what I was trying to ask. As I responded to comments I started to refine the question in my head and was able to then find the answer I was looking for. Thanks to everyone who helped.

The answer is: hibernate does this automatically.

Suppose you have in your database table A with a primary key "id", and a table B that has a primary key called "a_id" that references table A.

So you create the following classes (abbreviated):

public class A {
  private String aProperty;
  // ... getter and setter, etc
{

public class B extends A {
  private String bProperty;
  // ... getter and setter, etc
}

Then map them like so:

<hibernate-mapping>
    <class name="A" table="a" catalog="testokdelete">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="aProperty" column="a_property"/>
        <joined-subclass name="B" table="b">
            <key column="a_id"/>
            <property name="bProperty" column="b_property"/>
        </joined-subclass>
    </class>
</hibernate-mapping>

And you can return A and B objects using a simple "from A" query, as in the following code:

Query query = session.createQuery("from A");
List<Object> objects = query.list();
for (Object object: objects) {
    System.out.print("A property: ");
    System.out.print(((A)object).getAProperty());
    System.out.print(", B property: ");
    System.out.println( (object.getClass() == B.class) ? ((B)object).getBProperty() : "not a B");
}

All it does is return a list of objects using the query "from A", and then walks through them printing out aProperty from our A class and, if the class is of type B, the bProperty from our B class.

The hibernate query in this case is automatically polymorphic and will give you a B object when appropriate.

这篇关于Hibernate多态:实例化正确的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆