-->
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.  [ 9 posts ] 
Author Message
 Post subject: Possible to do Field Inheritance?
PostPosted: Wed Aug 17, 2005 1:20 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
I have a lot of persistent classes. They all descend from a particular base class in my java that has some core fields that I want every object to have e.g.

createdOn
createdBy
lastUpdateOn
lastUpdateBy

For my java, it's a no brainer. If I add a new class "foo" and I subclass "parent", it gets those four fields automatically by virtue of the class inheritance.

When I go to map foo in my .hbm.xml file though, I end up having to map those four fields manually. The problem gets more annoying when, like this morning, we add a new "universal" field and I have to go out and write a script to update like 100 .hbm files to add the new field.

Is there any way (short of the performance killer of making *everything* a joined subclass), to tell hibernate that I want *everything* I map to derive from or otherwise contain a specific set of fields?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 1:21 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
<property name="metaInfo" type="myCustomUserTypeThatKnowsHowToMap4Fields"/>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 1:51 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
Can you point me to somewhere in the doc that discusses how to do this? Neither my copy of Hibernate in action, nor the online doc seem to offer much in the way of insight.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 2:14 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
pcasey wrote:
Can you point me to somewhere in the doc that discusses how to do this? Neither my copy of Hibernate in action, nor the online doc seem to offer much in the way of insight.


Section 6.2.3 of the 3.0.5 docs

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 2:17 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There is half a chapter about UserTypes in HiA...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 2:17 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
I hit the submit button a little quick...

I would also recommend looking at the org.hibernate.test package. You can do a search for UserType and find several examples.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 2:34 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
christian wrote:
There is half a chapter about UserTypes in HiA...


Yah, I found that, but I wasn't sure if that's the implementation you were pointing to because I don't think it's going to work for me.

As I understand it, if I go down the custom type road, I'll end up with an object foo which contains a customer type metaInfo. I can then reference the meta fields via

foo.metaInfo.createdOn

Right now though, I have lots of code in existence which relies on being able to do

foo.createdOn

So by going to a custom type I'm changing the access path, am I not?

Likewise, can I even use a custom type if I want to universally map a collection as part of the type? This whole thing started this morning when we decided to add an attachments set to each and every class in the model. So my metadata atm contains:

createdOn -- timestamp
createdBy -- user
lastUpdatedOn -- timestamp
lastUpdatedBy - user
attachments -- <set>

Looking through the custom type API it seems like I can whip up a metaInfo for the first four e.g.

public String[] getPropertyNames() {
return new String[] {"lastUpdatedOn", "lastUpdatedBy", "createdOn", "createdBy"}
}


public String[] getPropertyNames() {
return new String[] {Hibernate.TIMESTAMP, ?Hibernate.CLASS?, Hibernate.TIMESTAMP, ?Hibernate.CLASS?,}
}

How do I map a *set* though? There is no Hibernate.SET, and there's the additional issue that you need a lot more information to define a set than a scalar.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 2:47 pm 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
pcasey wrote:
christian wrote:
There is half a chapter about UserTypes in HiA...
As I understand it, if I go down the custom type road, I'll end up with an object foo which contains a customer type metaInfo. I can then reference the meta fields via

foo.metaInfo.createdOn

Right now though, I have lots of code in existence which relies on being able to do

foo.createdOn

So by going to a custom type I'm changing the access path, am I not?


Use delegate methods. Eclipse will even write them for you if you're using that.

Code:

public class Foo {

    private MetaInfo metaInfo;

    // ...

    public Date getCreatedOn() {
        return getMetaInfo().getCreatedOn();
    }

    // etc...
}



Add in null protection, etc. as appropriate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 2:52 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
jdl wrote:
,snip>

Use delegate methods. Eclipse will even write them for you if you're using that.


<Bonk> Yah, that'll solve the access path concern. Any idea if there exists a way to put a set in a User Type though (the second concern I had)?


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