-->
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: Are immutable classes really immutable?
PostPosted: Wed Nov 26, 2003 10:03 am 
I have a class to persist that has only 'final' attributes and only getters. In the mapping document I put <class... mutable="false" ...>. This should handle my class as immutable. When Hibernate builds its session factory it returns this:

net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property username in class xxx
at net.sf.hibernate.util.ReflectHelper.getSetter(ReflectHelper.java:146)
at net.sf.hibernate.persister.AbstractEntityPersister.<init>(AbstractEntityPersister.java:568)
at net.sf.hibernate.persister.EntityPersister.<init>(EntityPersister.java:651)
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:218)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:618)

Any idea? Why do I need to have setters for an immutable class? That kills the purpose of immutability.


Top
  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 6:12 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Having a attribute final does not mean you cannot set it the first time. Thus setter is mandatory

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 11:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Hibernate need SOME way to set your values - right ?

Besides in the new 2.1 you can use direct field access - then you don't have setters, but have even worse direct field access - what do you prefer ? ;) (remember that the setters can just be private if you want)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 5:07 pm 
Beginner
Beginner

Joined: Fri Jun 09, 2006 4:21 pm
Posts: 23
Location: Reading
emmanuel wrote:
Having a attribute final does not mean you cannot set it the first time. Thus setter is mandatory

Eh??
My IDE strongly opposes declaring a variable final and not having it in the constructor declaration.
And I've always thought that you'll get a compilation error if you try to set final variable after you've created your instance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 3:51 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hibernate can't inject via the constructor so if you want the property to be loaded from the database it need to be set *somehow*

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 7:54 am 
Beginner
Beginner

Joined: Fri Jun 09, 2006 4:21 pm
Posts: 23
Location: Reading
max wrote:
hibernate can't inject via the constructor so if you want the property to be loaded from the database it need to be set *somehow*


Many software design gurus advocate using immutability where possible and Hibernate states it is transparent to the domain model. If immutability according to Hibernate means making all setters private (without using final keyword), then fine.

Hang on a sec... In Cavet Emptor final fields are used:

Code:
public class MonetaryAmount implements Serializable {
private final BigDecimal amount;
private final Currency currency;
public MonetaryAmount(BigDecimal amount, Currency currency) {
this.amount = amount;
this.currency = currency;
}


Ah, that's why MonetaryAmountSimpleUserType.java is also present.

Ironicly, there is comment to this in the book:

Quote:
We have made MonetaryAmount an immutable class. This is a good practice in Java
because it simplifies coding.


With all due respect, needing to write a new custom UserType for each immutable class doesn't sound too simple.

I wounder if there was some other solution for using final fields, e.g. reflexion or ... advanced reflexion )


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 9:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
fell free to contribute it ;)

note: for entities to be loaded from the db a 2-phase loading is used and we need the object to be instantiated during that process. That means we might not have the value for the final field until later and hence need a .set method.

Value types like monetary amount is different and can be loaded directly from the resultset and you can write customtypes for it...if you find that too cumbersome then you most likely can write a generic custom type that loads it more generically for more than one type.

_________________
Max
Don't forget to rate


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.