-->
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.  [ 7 posts ] 
Author Message
 Post subject: Formula properties and Inheritance
PostPosted: Tue May 11, 2004 6:43 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 1:17 pm
Posts: 28
I just bumped into a small problem. I have a couple of classes that all have a different field mapped to the identifier property. I'm using <property formula="otherFieldName"> so hibernate will understand this.

Similar logic is implemented in the classes.

I have two problems with this.

A. Hibernate wants a setter for the generated property, eventhough my javaclasses have their own logic to compute its value. I worked around this by adding a setter that actually sets nothing. but I don't like this as it is ugly.

B. with Inheritance there is no way to override the way this property works. I'd like to be able to use a different field or formula in child classes, but I don't see a way to do this in Hibernate mappings. Hibernate will always use the formula of the parent class which is undesirable. Hibernate should allow to override the formula of a property in subclasses as long as the types remain the same.

I need this because I want to be able to query on the baseclass with a certain value assigned to this property so my baseclass has an abstract getter that must be overridden by the child classes. The children handle this by returning a different field from their own private members.

For example:

Parent:
a getIdentifier()

Child 1
p getEmail()
p getIdentifier (returns getEmail)

Child 2
p getUsername()
p getIdentifier (returns getUsername)

Is there a way to do this without having to redundantly save the identifier field or by having a redundant setter for the identifier field?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 8:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Why do you map this property at all? Just don't map it and in your getIdentifier() method do something like "return getUsername()"


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:03 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 1:17 pm
Posts: 28
because I want to use a hibernate query that does

from users a where a.identifier=:ident

and I cannot do that unless I map the value right?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:05 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 1:17 pm
Posts: 28
Regardless of why; it would be nice if dynamic properties could be overwritten by inheritance as it is possible in normal Java objects.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Oh well, if you insist to do that: You can implement your own PropertyAccessor for the property, which does nothing on set.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 7:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
PS: you know that you can just map the username as the identifier and still do "where x.id = ? " in your HQL?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 17, 2004 4:31 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 1:17 pm
Posts: 28
I tried but it does not work.

The point is, I have an abstract base class with a getIdentifier function. Every class that inherits from that will provide its own implementation. I want to be able to query against this so I set up the following:

user (abstract)
- getIdentifier (not mapped in hibernate)

subuser1
- getIdentifier (mapped in hibernate using formula to give the email address)

subuser2
- getIdentifier (mapped in hibernate using formula to give the username)

This works as long as you query for the exact subtype, but a query against just user will fail.

I tried this next

user (abstract)
- getIdentifier (mapped as formula returning "")

subuser1
- getIdentifier (mapped in hibernate using formula to give the email address)

subuser2
- getIdentifier (mapped in hibernate using formula to give the username)

This allows to query against the general user type, but all queries for identifier return "", because the base property formula overrides those of its children.

From an OO perspective this should not happen and as formula properties are no static members, but dynamic fields (more like functions) I want to be able to alter their implementation for each subclass.

The reason I don't want to map both the identifier and the username and the email column is that this would give me redundant data in the database as the email and username field are already saved. But I'll stick to this path as I see no other working alternative at this point.


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