-->
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.  [ 2 posts ] 
Author Message
 Post subject: 1-to-1 associations for 2 classes into the same table
PostPosted: Wed Jun 23, 2004 6:16 am 
Newbie

Joined: Sat Jan 10, 2004 5:45 pm
Posts: 12
Location: Hong Kong
Hi.

I am using Hibernate Version 2.1.4, and I'd have the following class structure:

Code:
class Page {
  public Integer getID();
  public void setID(Integer id);
  public PageInfo getPageInfo();
  public void setPageInfo(PageInfo pageInfo);
  public Page getNextPage();
  public void setNextPage(Page nextPage);

/*
  // Intentionally commented: See description below
  public PageInfo getNextPageInfo();
  public void setNextPageInfo(Page nextPageInfo);
*/

  public byte[] getData();
  public void setData(byte[] data);
}

class PageInfo {
  public Integer getID();
  public void setID(Integer id);
  public String getTitle();
  public void setTtile(String title);
}


What happen if PageInfo is mapped as a component of the Page, and both of them are mapped to the *same* table? i.e.

Code:
<hiberate-mapping>
  <class name="Page" table="Page">
    <id name="ID" column="ID" type="int">
      <generator class="identity"/>
    </id>
    <one-to-one name="PageInfo" class="PageInfo" cascade="all"/>
    <property name="data"/>
  </class>
  <class name="PageInfo" table="Page">
    <id name="ID" column="ID" type="int">
      <generator class="identity"/>
    </id>
    <property name="Title" />
  </class>
</hibernate-mapping>


This mapping may not make sense, but the intention is that the Page class is actually generated for mapping to a XSD and will emit a XML with this format:

Code:
<Page id="1">
  <!-- Some Details Omitted -->
  <NextPage>
    <Page id="2">
      <!-- Some Details Omitted -->
      </NextPage>
    </Page>
  </NextPage>
</Page>


The XML is generated by reading the class recursively. This arises a problem that the bulky data[] will also be loaded.

This problem can be solved if we only refers to the next page as a PageInfo.

But normally doing this requires 2 tables for each class.

The question is, what will be the beahviour of Hibernate if the mapping is like this.

Thank you very much.


Top
 Profile  
 
 Post subject: Solution
PostPosted: Wed Jun 23, 2004 6:28 am 
Newbie

Joined: Sat Jan 10, 2004 5:45 pm
Posts: 12
Location: Hong Kong
Found a soluton *by design*:

Code:
class PageInfo {
  // ID code omitted
  public PageInfo getNextPageInfo();
  public void setNextPageInfo(PageInfo nextPageInfo);
}

class Page {
  // ID code omitted
  public PageInfo getPageInfo();
  public void setPageInfo(PageInfo info);
}


With this the problem at the domain level is solved.

p.s. For technical curiousity, I'd like to know the behaviour of above mapping. Thank you.


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