Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate polymorphism: instantiating the right class

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.

like image 341
Boden Avatar asked Apr 16 '09 19:04

Boden


People also ask

What is polymorphism in hibernate?

Implicit polymorphism means if a class or interface is used in HQL, criteria or named queries, hibernate fetches the records from the table mapped to the used class along with all the tables mapped to its subclasses, at any hierarchy level.

How polymorphism works?

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks.

Which of the following are an example of polymorphism?

A real-life example of polymorphism is a person who at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee. So the same person exhibits different behavior in different situations. This is called polymorphism.

What is polymorphism OOP?

Polymorphism is one of the core concepts of object-oriented programming (OOP) and describes situations in which something occurs in several different forms. In computer science, it describes the concept that you can access objects of different types through the same interface.


1 Answers

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.

like image 95
Boden Avatar answered Oct 05 '22 00:10

Boden