-->
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: Mapping multiple columns to a list
PostPosted: Fri Mar 11, 2011 8:15 am 
Newbie

Joined: Fri Mar 11, 2011 8:08 am
Posts: 1
Hi guys,
I have an old database which has the following table called PriceLists:

Code:
ID <pk>
Description
Price1
Price2
Price3
[...]
Price60


I would like to map this table to a class like this:

Code:
public class PriceList
{
    public virtual string Id { get; set; }
    public virtual IList<float> Prices { get; set; }
}


Is it possible? I have already tried with bags and components with no luck.

Thank you.


Top
 Profile  
 
 Post subject: Re: Mapping multiple columns to a list
PostPosted: Thu Aug 25, 2011 5:06 am 
Newbie

Joined: Thu Aug 25, 2011 4:52 am
Posts: 5
Hi

I have the same problem reading data from a legacy database, which I cannot influence. I don't need to update the fields!

So I implemented a property method in the bean class which adds the column value to the list:
Code:
   
public void setAdressLine(String text)
{
    if (text != null)
    {
        addressLines.add(text);
    }
}

But I cannot define the property mapping more than once in the hibernate mapping:
Code:
<property name="addressLine" column="ADR_ZEILE_1" type="string" insert="false" update="false"/>
<property name="addressLine" column="ADR_ZEILE_2" type="string" insert="false" update="false"/>


This leads to the following exception:

org.hibernate.MappingException: Duplicate property mapping of addressLine found in ...

Any ideas how to solve this?


Top
 Profile  
 
 Post subject: Re: Mapping multiple columns to a list
PostPosted: Wed Jan 11, 2012 12:07 pm 
Newbie

Joined: Fri Sep 02, 2011 12:55 pm
Posts: 4
I'm not sure if this is relevant, but in Java Hibernate you can do this with a subselect in your list mapping. The explain plan on Oracle doesn't look horrible for this type of query, but you'd definitely want to do some performance testing to make sure on whatever database you're using.

Mapping:
Code:
        <list name="prices" >
           <subselect>
            SELECT ID,
                   Price1 as price,
                   0 AS idx
              FROM PRICELISTS
            UNION
            SELECT ID,
                   Price2 as price,
                   1 AS idx
              FROM PRICELISTS
*** more ***
            UNION
            SELECT ID,
                   Price60 as price,
                   59 AS idx
              FROM PRICELISTS
           </subselect>
      <key>
         <column name="ID"></column>
      </key>
           <list-index column="idx"></list-index>
           <element column="price" type="float"></element>
        </list>


Query will look something like:
Code:
select
        prices0_.ID as ID22_0_,
        prices0_.price as price22_0_,
        prices0_.idx as idx3_0_
    from
        (            SELECT ID,
                   Price1 as price,
                   0 AS idx
              FROM PRICELISTS
            UNION
            SELECT ID,
                   Price2 as price,
                   1 AS idx
              FROM PRICELISTS
*** more ***
            UNION
            SELECT ID,
                   Price60 as price,
                   59 AS idx
              FROM PRICELISTS
    ) prices0_
where
    prices0_.ID=?


I think because it's using the primary key, the filter gets into the subselect which is why the explain plan isn't horrible. Anyway, hope this is helpful.


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.