-->
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.  [ 13 posts ] 
Author Message
 Post subject: mapping multiple classes to 1 table?
PostPosted: Mon Sep 08, 2003 10:10 am 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
Does Hibernate allow more than 1 class to be mapped to the same db table? I'm trying to break up a 493 col table into smaller classes with 1-to-1 relationships. See Console dump/stack trace & Mappings.

A previous posting (http://forum.hibernate.org/viewtopic.ph ... t=onetoone) says "Hibernate guarantees identical objects (VM identity be memory location) only in the same Session scope. Use equals() to check for equality." This leads me to think I might just need adjustment to the .equals method (see code) but I can't figure out what that would be.

Although, another posting (http://forum.hibernate.org/viewtopic.ph ... t=onetoone) seems to indicate this is not supported. "It is an API-level requirement of Hibernate that a class maps unambiguously to a particular table."

jeff.boring@siemens.com


Console Dump/ Stack Trace:
Code:
12:29:19,508 DEBUG Loader:220 - total objects hydrated: 1
12:29:19,508 DEBUG SessionImpl:1986 - resolving associations for [bidAlt.mapManyClasses.BpMBidAlt#1]
12:29:19,518 DEBUG SessionImpl:1782 - loading [bidAlt.mapManyClasses.BpMBidAlt1#1]
12:29:19,518 DEBUG SessionImpl:1874 - attempting to resolve [bidAlt.mapManyClasses.BpMBidAlt1#1]
12:29:19,518 DEBUG SessionImpl:1889 - resolved object in session cache [bidAlt.mapManyClasses.BpMBidAlt1#1]
12:29:19,518 ERROR ReflectHelper:67 - IllegalArgumentException in class: bidAlt.mapManyClasses.BpMBidAlt, setter method of property: bpMBidAlt1
12:29:19,528 ERROR ReflectHelper:71 - expected type: bidAlt.mapManyClasses.BpMBidAlt1, actual value: bidAlt.mapManyClasses.BpMBidAlt
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.sf.hibernate.util.ReflectHelper$Setter.set(ReflectHelper.java:45)
at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:182)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:1993)
at net.sf.hibernate.loader.Loader.doResultSet(Loader.java:221)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:113)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:661)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:676)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:52)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:44)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:354)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:1920)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1787)
at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1718)
at bidAlt.mapManyClasses.Driver.main(Driver.java:76)
rethrown as net.sf.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling: argument type mismatch setter of bidAlt.mapManyClasses.BpMBidAlt.bpMBidAlt1


Mappings:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="bidAlt.mapManyClasses.BpMBidAlt" table="BP_M_BID_ALT" schema="TGBPDBA2">
   <id name="bidAltSkey" column="BID_ALT_SKEY" type="java.lang.Long">
      <generator class="assigned"/>
   </id>
   <property name="bidSkey" column="BID_SKEY" type="java.lang.Long" length="12" not-null="true"/>
   .......
   <one-to-one
      name="bpMBidAlt1"
      class="bidAlt.mapManyClasses.BpMBidAlt1"
      cascade="none"
      outer-join="false"
      constrained="true"
   />
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="bidAlt.mapManyClasses.BpMBidAlt1" table="BP_M_BID_ALT" schema="TGBPDBA2">
      <id name="bidAlt1Skey" column="BID_ALT_SKEY" type="java.lang.Long">
         <generator class="assigned"/>
      </id>
   <property name="comBidBondReqdInd" column="COM_BID_BOND_REQD_IND" type="java.lang.String" length="1"/>
   ....
</class>
</hibernate-mapping>

Code:
Code:
public boolean equals(Object other) {
        if ( !(other instanceof BpMBidAlt) ) return false;
        BpMBidAlt castOther = (BpMBidAlt) other;
        return new EqualsBuilder()
            .append(this.getBidAltSkey(), castOther.getBidAltSkey())
            .isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder()
            .append(getBidAltSkey())
            .toHashCode();
    }


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 11:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes it is supported.

Read this quote again:
Quote:
"It is an API-level requirement of Hibernate that a class maps unambiguously to a particular table."


It does not say that a "table must map unambiguously to a single class". It says the converse.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 11:07 am 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
Then is it my equals method that causing the error?

Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 11:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
OK, there is one more rule.

(This is a contraversial one)


You may not have two instances that represent the same database row in the same session at one time.


It will take a lot to convince me that this is a bad rule ;)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 12:24 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
Yea, I understand your point. However, it seems my only option is to ask the DBA to split up the 493 col table. Right? Would interfaces/proxies help me at all here? The app will take about a 5-10 sec hit for every request just hydrate 1 instance of this monster so I need to change something and you know DBA's.

Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 12:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
What you really need is lazy-loading components. But we won't have this feature for a while yet.


Would just projection (ie. select foo.bar, foo.baz, ....) help?

or do you need to update this table.

could you make some updateable views?

To be honest (you should know my attitude on this by now), if it were me I would just handcode JDBC for this stuff. I've gone out of my way to make sure its easy to mix 'n match handcoded JDBC + Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 1:30 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
By hand coding, do you mean implement a custom persister?

Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 8:50 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
gavin wrote:
You may not have two instances that represent the same database row in the same session at one time.


I have two Classes, A & B, that map to the same table.
In the current session, I'm using an instance of Class A to represent a row.
I then decide, using the same session, that I want instead to use Class B to manipulate the row.
Do I just issue an evict() call on the A instance so that I can load a B instance pointing at that row? Are there any other steps I have to go through? Is that even the right method to call? Is there a right method to call?

Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 08, 2003 10:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
By hand coding, do you mean implement a custom persister?


That is the Hard Way.

An easier way is to write a DAO and a UserType.


One of these days I need to look seriously into the notion of a UserAssociationType. Its slightly nontrivial to expose all the semantics of Hibernate associations in something user friendly.


Quote:
Do I just issue an evict() call on the A instance so that I can load a B instance pointing at that row?


correct.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2003 9:27 am 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
UPDATE:

Performance numbers of 5-10 sec to hydrate a 494 field instance can NOT be repeated. New test show around 1 sec. Don't know what cause the results on the first test set. The same computer & code was used both times.

Jeff


Top
 Profile  
 
 Post subject: Mapping one table per hierarchy
PostPosted: Wed Sep 10, 2003 10:45 am 
Newbie

Joined: Wed Sep 10, 2003 10:24 am
Posts: 5
Hi All,

I got a problem when tried to map a hierarchy using the "one table per hierarchy" strategy.

I have an abstract superclass, named A, and 3 subclasses, B, C, and D.
The problem is that I programmed B, C, and D, as private static classes of the superclass A. Something like this:

public class A
{
int oid;
String name;

public A(String name)
{
this.nome = nome;
}

...

private static class B extends A()
{
public B()
{
super("B");
}

}
...
}

My intention is to use attribute "name" as a discriminator. But the problem is that I don't know how to indicate the subclass.

How could I implement that mapping strategy for this implementation ?


Thanks in adv

Gustavo


Top
 Profile  
 
 Post subject: Same issue - can it be done?
PostPosted: Wed Sep 17, 2003 4:19 am 
Newbie

Joined: Mon Sep 08, 2003 5:47 pm
Posts: 9
Location: Denver
I'm implementing a trading system that operates off of the following fields:
symbol, date, open, high, low, close, volume

We have a data feed that provides an additional 40 fields. All of that data goes straight into the database.

I've got a simple TradingDay class that contains the basic data an provides the API upon which the rest of the sytem depends.

We have to traverse years worth of trading data (millions of rows) as fast as possible - it is a waste to query, transmit, and unmarshall all those other fields. Yet I'd like to access that extra data when appropriate.

How to I model this with Hibernate?

A subclass relationship makes alot of sense from an object point of view - but there is no mechanism in hibernate to descriminate which class (parent/child) is being queried.

I just tried model the extra data as an adornment to the simple class - but that leads to a 1-to-1 association on a single table.

It seems we'll all run into this same problem anytime we've got a entity (object or db) that spans several problem domains. For example, a lease agreement in an oil & gas system has sets of attributes for surface rights, minerals, federal regulations, state regulations, royalties - etc... it's a giant one-to-one mess. Same thing happens with commodities trading contracts as they're nominated (negotiated), in-process, and executed - the trade will accumluate attributes as it goes along.

How can we model this?

Thanks,
- Chris

p.s. Hibernate rocks!


Top
 Profile  
 
 Post subject: Re: mapping multiple classes to 1 table?
PostPosted: Wed Sep 17, 2003 5:25 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
jwboring wrote:
Does Hibernate allow more than 1 class to be mapped to the same db table? I'm trying to break up a 493 col table into smaller classes with 1-to-1 relationships. See Console dump/stack trace & Mappings.

I think It must be better to use Map for this kind of tables without denormalizations in DB. Bean style datastructures are very nice, but Map must be more practical for this use case.


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