-->
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.  [ 3 posts ] 
Author Message
 Post subject: Do I need DTOs for my DAOs?
PostPosted: Sat Feb 21, 2004 4:54 pm 
Newbie

Joined: Sat Feb 21, 2004 4:27 pm
Posts: 2
Location: NJ, USA
I'm building a data services layer for a multitier application. A DAO model will be used to map between domain objects and the database and hibernate is the engine.

I can't figure out how to get hibernate to retrieve the keys instead of the actual referenced objects. Perhaps if I want to return only references to the caller in the other tier I need to copy the objects into DTOs to map the references not actual referencees?

For example, here's a simple slice of the data model:

Code:
  public class Customer {
    public CustomerKey key;
    public CustomerFamilyKey cfKey;
    public String name;
  }

  public class CustomerKey {
    public long id;
    public int version;
  }


and customers have a many-to-one relationship with a CustomerFamily:

Code:
  public class CustomerFamily {
    public CustomerFamilyKey key;
    public String name;
  }

  public class CustomerFamilyKey {
    public long id;
    public int version;
  }


Note that both objects are versioned and have composite keys. Not sure if that matters.

In the object that I return to the calling tier (caller -> StatelessSessionBean -> DAO -> RDBMS) I am intending to use a style of interface that provides on the the primary key of the parent/family not a reference to the actual object:

Code:
  public class Customer {
    public CustomerFamilyKey key;
       .
       .
 


Hibernate seems to want to make this more like:

Code:
public class Customer {
    public CustomerFamily family;
      .
      .


is there even a way to declare that what I want is the list of associated ids instead of the associated objects themselves?

Or, do I need to have the Hibernate managed objects give me back the connected objects and then I need to copy things over into more referenced-by-id DTOs?

Thanks in advance.

Here are the snippets of .hbm code I'm using if that helps:

Code:
  <hibernate-mapping package="foo">
    <class name="Customer" table="CUSTOMER">

      <composite-id name="primaryKey" class="Customer" unsaved-value="any">
        <key-property name="id" column="id"/>
        <key-property name="version" column="version"/>
      </composite-id>

      <property name="name"/>

       <many-to-one name="family" class="CustomerFamilyVO" cascade="all">
           <column name="family_id"/>
           <column name="family_version"/>
       </many-to-one>

    </class>
  </hibernate-mapping>


and

Code:
  <hibernate-mapping pacakge="foo">
    <class name="CustomerFamily" table="CUSTOMER_FAMILY">

      <composite-id name="primaryKey" unsaved-value="any">
        <key-property name="id" column="id"/>
        <key-property name="version" column="version"/>
      </composite-id>

      <property name="name"/>

    </class>
  </hibernate-mapping>

_________________
--lee


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2004 7:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You should not use "id-references" at all. Instead, you should rather use proxys and lazy loading collections to partition your object graph. Hibernate is really all about not having such crappy DTO patterns, which IMHO are a real ugly thing produced by the shortcomings of EJBs, and now people think having DTOs is somehow a "good thing".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 2:14 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Plus the O in ORM means Object. Hibernate is done not to bother with ids-references. Use it that way and you'll find it fantastic in your domain model.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.