-->
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: Legacy mapping question: numbered columns to a Java List
PostPosted: Thu Oct 19, 2006 7:00 pm 
Newbie

Joined: Thu Oct 19, 2006 6:33 pm
Posts: 2
Location: Toronto, Canada
Hi,

I have a legacy table that looks like this:

Code:
CREATE TABLE legacy (
  id INTEGER NOT NULL,
  foo_01 VARCHAR(30) NULL,
  foo_02 VARCHAR(30) NULL,
  foo_03 VARCHAR(30) NULL,
  foo_04 VARCHAR(30) NULL,
  foo_05 VARCHAR(30) NULL,
  bar_01 VARCHAR(30) NULL,
  bar_02 VARCHAR(30) NULL,
  bar_03 VARCHAR(30) NULL,
  bar_04 VARCHAR(30) NULL,
  bar_05 VARCHAR(30) NULL
);


and I would like to map it to a Java class that looks like this:

Code:
public class FooBar {
  private Long id;
  private List<String> foos;
  private List<String> bars;

  ...
}


The lists foo and bar might be of length 0..5, and if they are shorter than 5, then the unneeded columns in the table are set to SQL NULL. For instance, if foo.size() == 2 then foo_03, foo_04, and foo_05 will be set to null for that row in the legacy table.

I expect this is a common pattern that we have to deal with when mapping an object oriented model to a legacy database, but I haven't been able to find the answer in Hibernate In Action, or on the Hibernate web site, or using Google. If the answer is already somewhere on the web and I'm just not finding it, I apologise for wasting everyone's time with this question, but I'd still really appreciate a pointer to the information!

Thanks!

-Jonathan


Top
 Profile  
 
 Post subject: UserType
PostPosted: Thu Oct 19, 2006 10:15 pm 
Newbie

Joined: Fri Jan 06, 2006 12:10 am
Posts: 7
You could do something with a parameterized UserType. Something like:

Code:
<class name="FooBar" table="legacy">
    <id name="id"
        column="ID"
        type="java.lang.Long"
        access="field">
        <generator class="native"/>
    </id>

<property name="foos">
    <column name="foo_01"/>
    <column name="foo_02">
    <column name="foo_03"/>
    <column name="foo_04"/>
    <column name="foo_05"/>
    <type name="ListToMultipleColumnType">
        <param name="columnCount">5</param>
    </type>
</property>

<property name="bars">
    <column name="bar_01"/>
    <column name="bar_02"/>
    <column name="bar_03"/>
    <column name="bar_04"/>
    <column name="bar_05"/>
    <type name="ListToMultipleColumnType">
        <param name="columnCount">5</param>
    </type>
</property>
</class>


Top
 Profile  
 
 Post subject: UserType
PostPosted: Thu Oct 19, 2006 11:00 pm 
Newbie

Joined: Fri Jan 06, 2006 12:10 am
Posts: 7
4You could do something with a parameterized UserType. Something like:

Code:
<property name="foos">
    <column name="foo_01"/>
    <column name="foo_02">
    <column name="foo_03"/>
    <column name="foo_04"/>
    <column name="foo_05"/>
    <type name="ListToMultipleColumnType">
        <param name="columnCount">5</param>
    </type>
</property>


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.