-->
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: Persisting Derived Attributes
PostPosted: Fri Dec 19, 2008 6:35 am 
Newbie

Joined: Fri Dec 19, 2008 5:39 am
Posts: 1
Hi,

think of the following scenario: you have a class Person with firstname and surename, both with getters and setters.

Since your table is large you want to avoid a full table scan when doing queries and therefore introduce an additional "SEARCH_NAME" column with an index.

In the Java class you have a derived attribute returning this search name:

public String getSearchName() {
return getFirstname() + getSurname();
}


If I use

<property name="SearchName" column="SEARCH_NAME" length="80" type="java.lang.String" not-null="true" index="IDX_SEARCH_NAME"/>

I get an error that there is no "setter" method for this property on the Java class - but since it is derived I dont want to provide a setter method.

How would one map these in the *.hbm.xml file, so that:
- the search name is persisted too
- I dont have to provide a setter (since it is computed)

Is there something like a "write-only" property?

Thx
T.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 12:03 pm 
Newbie

Joined: Mon Mar 17, 2008 3:51 pm
Posts: 8
Location: Colorado Springs, CO
As far as I know Hibernate requires a public setter in order to assign the value to your POJO.

Having said that, there are a couple of ways you could deal with this:
If you do not want the users of your POJO to be able to have access to that method then you would probably have to have them interact with the POJO through an interface that does not have the setter on it. Of course this would not keep someone from casting the interface to the implementation and getting access to the setter that way. If that level of information hiding is not acceptable then you'll have to find a different way.

An alternative and possibly more attractive way is to use a component to combine the first name and surname properties. I don't know how this would affect indexing though.


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.