-->
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: Mapping mutiple columns of the same type into a Collection.
PostPosted: Tue Oct 07, 2008 8:32 am 
Newbie

Joined: Tue Oct 07, 2008 3:47 am
Posts: 1
Location: Brussels
Hibernate version:3.1


Hello all,

I would like to map a series of column of a legacy database to a java collection.

For example, let us say I have a table order with the columns Mass_1, Mass_2, Mass_3, .... Mass_n, all containing integers.

I would like to map this table to an object Order with a field masses of type List<Integer>, where order.getMasses.get(2) would output the value of Mass_3, and so forth.

Is it possible using just an hibernate mapping ? (I see how I could do this by tweaking the code for the setters and getters for the Mass_i's, but I am looking for something cleaner as I will encounter this problem many time in this legacy database).

Martin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2008 10:08 am 
Newbie

Joined: Fri Oct 03, 2008 2:30 am
Posts: 16
Location: Neuchâtel, Switzerland
I read "Java Persistence with Hibernate" and read nothing about mapping multiple columns of a table into a Collection in the same table.

A more elegant/generic solution than the getter tweaking is maybe using an entity listener, that does the work of collecting all integers. With the use of an own annotation and reflection this may be "cleaner" solution.

Code:
@Entity
@EntityListeners(CollectableListener.class)
public class LegacyTable {
  @Collectable(id=0)
  private Integer m1;
  @Collectable(id=1)
  private Integer m2;
  @Transient
  private List<Integer> mAll;
}

public class CollectableListener {
  @PostLoad
  public void collectIntegers(Object entity) {
    // ... get field by annotation, setting the list on the entity
  }
}


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.