-->
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 flat table to object model...
PostPosted: Thu Jul 06, 2006 12:31 pm 
Newbie

Joined: Wed Jun 08, 2005 11:42 pm
Posts: 4
I have a single flat table that needs need to get mapped into a small object model, ~6 levels. The mapping only needs to be read only and I have no choice in the underlying data structure, I can't change it. I would like to use hibernate but I am not sure if/how to do it. Basically it's a heirarchical datamodel that has been flattened down to a single table. Obviously this will requried many distinct queries to get it to work. I have A mapped with a composite ID and I can get the it queried properly. The problem crops up with B, which should be related to A. With the mapping of B below I can load them properly by themselves, the problem comes up when I try to map A's list of Bs, and the parent reference back to A in B. I can't get this to work, see exception below. Any suggestions as to how to make this work? Since my data is flat I also need the collection to be distinct and I can't seem to find anyway to do that? I would really prefer to use Hibernate however I am wondering if due to the data model if I would be better off just doing this in JDBC?

Thanks!

-Nate

Hibernate version:3.1.3

Mapping documents:

Table structure (1 flat table - file_level_metadata):
Code:
date | time | validTime | ........(~3 more levels)

Code:
<hibernate-mapping>

    <class name="test.A" table="file_level_metadata" >
       <composite-id>
          <key-property name="date" />
          <key-property name="time" />
       </composite-id>
       
       <bag  name="bList" cascade="none"  lazy="false" >
           <key>
              <column name="date" />
              <column name="time" />
           </key>
         <one-to-many class="test.B" />
      </bag>
    </class>
   
    <class name="test.B" table="file_level_metadata" >
       <composite-id>
          <key-property name="date" />
          <key-property name="time" />
          <key-property name="validTime" />
       </composite-id>
        <!-- Would like to have a reference back to the parent A... -->
    </class>

</hibernate-mapping>

Code:
public class A implements Serializable{
   
   private static final long serialVersionUID = 1L;
   
   private Date date;
   private Date time;
   
   private List<B> bList;

        // Mutators removed for brevity...

   @Override
   public int hashCode() {
      final int PRIME = 31;
      int result = 1;
      result = PRIME * result + ((date == null) ? 0 : date.hashCode());
      result = PRIME * result + ((time == null) ? 0 : time.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      final A other = (A) obj;
      if (date == null) {
         if (other.date != null)
            return false;
      } else if (!date.equals(other.date))
         return false;
      if (time == null) {
         if (other.time != null)
            return false;
      } else if (!time.equals(other.time))
         return false;
      return true;
   }
   
   
}

Code:
public class B implements Serializable {
   
   private static final long serialVersionUID = 1L;

   private A parentA;
   
   private Date date;
   private Date time;
   private short validTime;
   
        // Mutators removed for brevity...

   @Override
   public int hashCode() {
      final int PRIME = 31;
      int result = 1;
      result = PRIME * result + ((date == null) ? 0 : date.hashCode());
      result = PRIME * result + ((time == null) ? 0 : time.hashCode());
      result = PRIME * result + validTime;
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      final B other = (B) obj;
      if (date == null) {
         if (other.date != null)
            return false;
      } else if (!date.equals(other.date))
         return false;
      if (time == null) {
         if (other.time != null)
            return false;
      } else if (!time.equals(other.time))
         return false;
      if (validTime != other.validTime)
         return false;
      return true;
   }
   
}



Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
Foreign key (FK974916ADCC1CB060:file_level_metadata [date,time])) must have same number of columns as the referenced primary key (file_level_metadata [date,time,validTime])

]
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 10, 2006 12:08 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
Quote:
I have a single flat table that needs need to get mapped into a small object model, ~6 levels.


I guess my first question is: Does it really need to to be mapped hierarchically? What if you mapped all your A's and then created a lot of functions to give you lists of the A's that you needed? Take an example of a table of locations in the world. It could look like this:

Code:
-- drop the tables first
drop table location;

-- create the tables
create table locations (
   location_id identity
   continent varchar(30)
  ,country varchar(30)
  ,region varchar(30)
  ,province varchar(30)
  ,city varchar(30)
  ,street varchar(30)
  ,street_number integer);

insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Elm', 1033);
insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Elm', 1034);
insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Elm', 1035);
insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Elm', 1036);
insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Elm', 1037);
insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Maple', 4);
insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Maple', 5);
insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Maple', 6);
insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Maple', 7);
insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Maple', 8);
insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Maple', 9);
insert into locations values (null, 'North America', 'USA', 'North', 'Michigan', 'Detroit', 'Maple', 10);

commit;


You could create functions to get lists of location objects based on criteria:

Code:
public interface LocationDao {
    public List locationsInContinent(String continent);
}


You could also pass in a list and have a function that would filter based on a criteria.

I know it's not true object orientation, but it would avoid the multi-level mapping that just seems like a nightmare to me...


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.