views:

650

answers:

6

I'm just getting started with Hibernate, and all the examples I'm seeing so far look pretty much like the tutorial in the Hibernate documentation:

package org.hibernate.tutorial.domain;
import java.util.Date;

public class Event {

    private Long id;
    private String title;
    private Date date;

    public Event() {}

    /* Accessor methods... */
}

Specifically: none of the fields are declared as 'final', and there must be a no-argument constructor so that the Hibernate framework can instantiate the class and set its fields.

But here's the thing - I really don't like making my classes mutable in any way whenever I can avoid it (Java Practices: Immutable Objects make a pretty strong argument for doing this). So is there any way to get Hibernate to work even if I were to declare each of the fields 'final'?

I understand that Hibernate uses Reflection to instantiate its classes and therefore needs to be able to invoke a constructor of some sort without taking the risk that it would pick the wrong constructor or pass the wrong value to one of its parameters, so it's probably safer to invoke the no-arg constructor and set each field one at a time. However, shouldn't it be possible to provide the necessary information to Hibernate so that it can safely instantiate immutable objects?

public class Event {

private final Long id;
private final String title;
private final Date date;

public Event(@SetsProperty("id") Long id,
    @SetsProperty("title") String title,
    @SetsProperty("date") Date date) {

    this.id = id;
    this.title = title;
    this.date = new Date(date.getTime());
}

/* Accessor methods... */

}

The @SetsProperty annotation is of course fictitious, but doesn't seem like it should be out of reach.

+1  A: 

Just remove the finals. Final is not essential for an immutable object.

bwalliser
Yes it is. In particular in a multithreaded environement. It has special semantics that makes using final proper way to handle the publishing of an object.
egaga
Sorry, no it's not. See the answer of tkopec. Ever tried to add an element to a final collection variable? Well, it works. That's because final doesn't mean it is immutable, it just means it's final.
bwalliser
Drew Stephens
@bwalliser- Final menas that you assign the property only once, if that property is of a mutable type (like a collection) it can change internaly but your reference to it can never change.It's up to the developer to verify that the property type is immutable in the sense they want it to be.Also, the answer Tadeusz Kopec gave is a bit incomplete as when you remove the final Reflection can still be used to change the fields (hence Hibernate's requirement) and so in certain development scenarios that is problematic.
Ittai
+2  A: 

This sounds like it is not a use case for Hibernate, since many operations it performs concern mutable state:

  • merging objects
  • dirty state checking
  • flushing changes

That being said, if you're concerned about immutability you may choose to provide wrappers around your objects, with copy-constructors:

public class FinalEvent {
    private final Integer id;

    public FinalEvent(Event event) {
        id = event.id;
    }
}

It does mean extra work though.


Now that I'm thinking of it, hibernate sessions are usually thread-bound and this voids at least one the benefits of the final fields - safe publication.

What other the benefits of final fields are you looking for?

Robert Munteanu
+1: Nice idea on using a wrapper class - it looks like it would be a good solution when final classes / fields are needed. One could even avoid ambiguity on which version (mutable vs. wrapper) should be used externally by making the mutable version visible only to data access classes.As for using final, my main reason is that it's often just easier to guarantee (rather than assume) that each field is assigned exactly once upon construction. We have a lot of data that's meant to be constant throughout the life of the JVM that uses it, so it's helpful to make this clear.
Kyle Krull
+6  A: 

Immutable object means an object with no methods that modify its state (i.e. its fields). The fields needn't be final. So you can remove all mutators and configure Hibernate to use fields acces instead of accessors or you can just mark no-arg constructor and mutators as deprecated. It's a bit workaround but better that than nothing.

Tadeusz Kopec
+1: Thanks - this is also helpful (removing setters and using field-based access) for cases where it's not possible/practical to make a wrapper class.Can't say as I'm too thrilled with the kind of Reflection kung-fu that's going on behind the scenes though - private fields that aren't actually private and all. Kind of makes me feel like I should add some @ReadTheHibernateBookBeforeUsingThisClass tag for users (like me) who are not so familiar with the details of how Hibernate works.
Kyle Krull
+1  A: 

You may be able to accomplish the desired result by using a Builder pattern. I read a posting a while ago on the Hibernate Forums discussing the idea (though I never implemented it myself...)

+1  A: 

This one has been bugging me for a long time too. One idea I've been trying out recently is this - define read-only interfaces for your model classes and have your DAOs and any factories return those instead on the object. That means that even though the implementation is mutable, once it's left the DAO/factory object it can no longer be tweaked.

Like so:

public interface Grape {
  public Color getColor();
}

public class HibernateGrapeDao {
  public Grape findGrape(int grapeId) {
    HibernateGrape grape = hibernate.find(...
    return grape;
  }
}

class HibernateGrape implements Grape {
 ....
}

May even want to keep the implementing classes package-private to the dao package, so nobody can fiddle with them directly. A bit more work, but probably helps keep things cleaner in the long run. And, obviously, be careful with the whole equality/identity business.

George
A: 

Actually in JDK 1.5+ hibernate can handle (thru reflection) changing final fields. Create a protected default constructor() that sets the fields to some defaults/null etc... Hibernate can and will override those values when it instantiates the object.

This is all possible thanks to changes to Java 1.5 memory model - the changes of interest (allowing final to be not so final) where made to enable serialization/deserialization.

So...

public class Event {

final private Long id;
final private String title;
final private Date date;

// Purely for serialization/deserialization
protected Event() {
    id = null;
    title = null;
    date = null;
}

public Event(Long id, String title, Data date) {
    this.id = id;
    this.title = title;
    this.date = date;
}

/* Accessor methods... */

}

Andy