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.  [ 8 posts ] 
Author Message
 Post subject: final field...how do I map??
PostPosted: Mon Oct 31, 2005 3:00 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
Hi,

I have this small problem. I have a class like..

public class TimePeriod implements Serializable {
private Integer id;
private final long startTimestamp;
private long endTimestamp;

protected TimePeriod() {
startTimestamp = 0;
}

public TimePeriod(long startTimestamp, long endTimestamp) {
this.startTimestamp = startTimestamp;
this.endTimestamp = endTimestamp;
}

public void setEndTimestamp(long endTimestamp) {
this.endTimestamp = endTimestamp;
}

public void setStartTimestamp(){}////what do I put here???

public long getStartTimestamp() {
return startTimestamp;
}

public long getEndTimestamp() {
return endTimestamp;
}

public Integer getId() {
return this.id;
}

public void setId(Integer value) {
this.id = value;
}
}

I have the following problems:
1) The start Timestamp field is final. I cannot assign it any value in the setter method. If I dont have the setter method or make the method empty it complains when I run the program. The field is assigned a value in the constructor any way. Also in the dafault hibernate constructor I have to initialize it.

Is there any way to avoid having these getters and setters.
Pleae let me know what I can do with this final field.

Thanx a lot,
Shefali


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 3:47 pm 
Beginner
Beginner

Joined: Wed Apr 06, 2005 9:29 pm
Posts: 24
Ok, you make it final so that it can't be change, right. Well you can remove the final and make the setter private. Hibernate wil be able to set it and all is well.

or

You could remove the setter and set the access to field in the hbm file.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 3:57 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
Hi,

well, they do not want me to change the design and want it in the constructor only. Also hibernate does not allow private setters, does it??..I have tried to make them private many times before and it complains...Am I missing something????

Can you please explain with some example:
"You could remove the setter and set the access to field in the hbm file"

Thank You


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 4:08 pm 
Beginner
Beginner

Joined: Wed Apr 06, 2005 9:29 pm
Posts: 24
Who is they and if you leave it final private or not hibernate can't do anything with it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 4:24 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 12:03 pm
Posts: 21
Im sorry but I would like to try the second option that u mentioned in ur reply.Can you please elaborate on it... I wud really appreciate it...

Thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 5:15 pm 
Beginner
Beginner

Joined: Wed Apr 06, 2005 9:29 pm
Posts: 24
Whatever you do you have to remove the final from that field. Hibernate can access the field directly, but can't change it if is final.
It instantiates the Object with the no arg constructor. So, if it is final there is no way for hibernate to set it.
Why does the field have to be final? If you just don't want it to be changed then removing the setter does that.
And removing final and setting
<property name="startTimestamp" access="field"/>
in the hbm file will do it for you.


Top
 Profile  
 
 Post subject: setters are private
PostPosted: Tue Nov 01, 2005 7:28 am 
Newbie

Joined: Tue Oct 25, 2005 12:28 pm
Posts: 18
Location: London
Setter methods should always be accessible, and are usually public, or protected or default. I have never seen a private setter.

Final methods are final because whoever wrote the code didn't want you to override the method. Either your requirements have changed, meaning you need to change the scope i.e. remove final modifier, or you are not fully aware of the requirements.

In an ideal world ;)

I am not a Hibernate 3 expert but I read you don't need getters and setters in 3.0, where you do in 2.1.

I hope this helps.

_________________
Cheers,
MikeR


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 10:31 am 
Beginner
Beginner

Joined: Wed Apr 06, 2005 9:29 pm
Posts: 24
It is not a final METHOD it is an instance variable that is final. If he needed
to put some kind of logic around setting the startTimestamp and didn't want
anyone to change the value making the setter private is perfectly reasonable
and hibernate can access it.

Reflexively putting mutators on all instance variables leads you away from OO
and toward procedural design. Additionally, he doesn't want anyone to change the
value after instantiation. Hence, don't make the setter public or don't have a
setter and have hibernate access it directly.


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