-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Saving two different subclasses with one unique superclass
PostPosted: Thu Apr 21, 2005 9:54 am 
Newbie

Joined: Thu Apr 21, 2005 9:17 am
Posts: 5
Location: The Netherlands
In the application I'm developing the user can add relations to the database. There are different kinds of relations, such as:

    Customer
    Distributor


Besides the differences, there are a couple of things that are similair. That's why I created a structure like this:

Code:
        +----------+
     -->+ Relation + <--
     |  +----------+   |
     |                 |
+----------+    +-------------+
+ Customer +    + Distributor +
+----------+    +-------------+


In the mapping-file I used the joined-subclass for this:

Code:
<hibernate-mapping package="com.example">
   <class name="com.example.Relation" table="relation">
      <id
         column="RelationID"
         name="RelationID"
         type="integer"
      >
         <generator class="vm" />
         ...
      </id>

      <joined-subclass name="com.example.Customer"
          table="customer">
          <key column="RelationID" />
         ...
      </joined-subclass>

      <joined-subclass name="com.example.Distributor"
          table="distributor">
          <key column="RelationID" />
         ...
      </joined-subclass>
   </class>
</hibernate-mapping>


But sometimes a customer is also a distributor. In a situation like that, I need to create a Customer and a Distributor object and the ids need to be the same. Hibernates needs to see that the superclass of the two objects (the Relation data) are in fact the same object. In the database the records should be:

Table Relation
relationId: 1
name: Some Company

Table Customer
relationId: 1
invoiceAddress: Some Street

Table Distributor
relationId: 1
contractPeriod: 1 year

The problem is that when I add the Customer object first, I can't save the Distributor object with the same id. When I save it, Hibernate generates an unique id. When I save the Relation-object first and then try to save the customer or distributor, Hibernate first checks if there is an object in cache that corresponds to the RelationId. This is the case because I saved the Relation object, so it returns this Relation object.

So, how can I save these two objects of different types and that Hibernate sees that the superclass of the two objects are the same and doesn't need to be resaved? It is necessairy that the two ids stay the same.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 9:58 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 5:37 am
Posts: 45
Location: Switzerland
Almost the same problem as http://forum.hibernate.org/viewtopic.php?t=940671


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 10:02 am 
Newbie

Joined: Thu Apr 21, 2005 9:17 am
Posts: 5
Location: The Netherlands
I didn't see that one. Stood a bit low in the search results. I'll wait until there is an acceptable answer there.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 10:34 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 5:37 am
Posts: 45
Location: Switzerland
I don't think that your solution is possible. What should Hibernate return when you make a query against the super class? Instances of Customer or instances of Distributor? If you would make a query against Customer and one against Distributor you would suddenly hafe two different objects that mean the same.

I think you should really do it that way: Make Customer and Distributor contain an Relation. If thats not enoug, make an Interface with the Methods of Relation und make Customer and Distributor implement it and return the values of the method of the Relation that they contain.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 4:58 am 
Newbie

Joined: Thu Apr 21, 2005 9:17 am
Posts: 5
Location: The Netherlands
keule wrote:
I don't think that your solution is possible. What should Hibernate return when you make a query against the super class? Instances of Customer or instances of Distributor? If you would make a query against Customer and one against Distributor you would suddenly hafe two different objects that mean the same.

I use query by example, so if I would request a Distributor, I'll get that one. That confusement wouldn't be an option in our application.

keule wrote:
I think you should really do it that way: Make Customer and Distributor contain an Relation. If thats not enoug, make an Interface with the Methods of Relation und make Customer and Distributor implement it and return the values of the method of the Relation that they contain.

The reason why we choose Hibernate was that we wanted to program our business logic in the way we felt best and we didn't want our business logic to depend on how we would setup our database.

But I did find a solution. First I add the Relation-object
Code:
session.save(relation);


After that I evict the relation out of the session:
Code:
session.evict(relation);


I manually added a record to the database for the Distributor and the Customer:
Code:
insert into customer values(relationId, ...);
insert into distributor values(relationId, ...);


Then I clear the session, set the relationId, find the customer, set the values and update the object:
Code:
session.clear();
customer.setRelationId(relation.getRelationId);
customerDAO.findByExample(customer); // data access object, a find will probably have the same result
customer.setXXX(xxx);
session.update(customer);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.