-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can I update another field from setter methods?
PostPosted: Tue Dec 08, 2009 9:26 am 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
I have a Name entity with first, middle, last and full persisted fields. The idea is that I need the full name automatically updated whenever the first, middle or last names are updated. They can never be out of sync. Am I required to use setFull() instead of full = in the calculateFullName() method? Is this the proper way to achieve what I need in hibernate?

Name table
------
Id
First
Middle
Last
Full

Code:
    public void setFirst(String first) {
        this.first = first;
        calculateFullName();
    }

    public void setMiddle(String middle) {
        this.middle = middle;
        calculateFullName();
    }

    public void setLast(String last) {
        this.last = last;
        calculateFullName();
    }

    public void calculateFullName() {
        full = first;
        full = (middle == null ? full : full == null ? middle : " " + middle);
        full = (last == null ? full : full == null ? last : " " + last);
    }

so the setFull(String full) method should actually never be called, unless the calculateFullName() requires its use instead of full =. It's ok if hibernate sets the full field from the database. It should get the same value whether setFull or setFirst is called in either order.


Top
 Profile  
 
 Post subject: Re: Can I update another field from setter methods?
PostPosted: Wed Dec 09, 2009 4:17 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
If you use JPA you can set the @Transient annotation on the full field or getFull() method to make it non-persistent, and only use the getFull() or calculateFull() method or whatever to get a concatenation of the necessary names.

If you use plain Hibernate, just don't map the full property to the database in the hbm.xml file.


Top
 Profile  
 
 Post subject: Re: Can I update another field from setter methods?
PostPosted: Wed Dec 09, 2009 4:20 am 
Newbie

Joined: Mon Nov 30, 2009 10:13 pm
Posts: 5
is calculatefull() method feasible?

_________________
links of london
ugg boots
wow gold


Top
 Profile  
 
 Post subject: Re: Can I update another field from setter methods?
PostPosted: Wed Dec 09, 2009 8:05 am 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
don stevo wrote:
If you use plain Hibernate, just don't map the full property to the database in the hbm.xml file.

the whole point of this is that I end up with a field in the database that is the full name. In the application it's no problem to calculate thr full name whenever I need it, but I can't put a db index on the full name and the query for a full name gets very long and ugly

@yvang, I don't care what the method names are... How does that help?


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