Mapping hibernation components of an immutable object

I have an immutable object that is part of a Hibernate object stored using component mapping. Example: PinDropcorresponds to a table that has an immutable type field Point:

public class PinDrop {
    private String name;
    private Point location;
    // Getters and setters for name and location
}

// Immutable Point
public class Point {
    private final double x;
    private final double y;
    // Getters for x and y, no setters
}

In mine PinDrop.hbm.xml:

<property name="name" column="name" type="string"/>
<component name="location" class="Point>
    <property name="x" column="location_x" type="double"/>
    <property name="y" column="location_y" type="double"/>
</component>

This does not work, because at runtime, Hibernate complains that it Pointdoes not have setters for xand y. Is there a way to use an immutable object as a component of a Hibernate persistent object?

Follow-up : I do not use annotations, but rather hbm.xml. Neither mutablenor immutableare valid attributes on componentand propertyin hbm.xml.

+5
source
3

, hibernate ( access=field hbm), .

Hibernate , .

+5

. :

  • - , final, , Hibernate .
  • -, . , , BO/DTO .
+3

@ ?

. , .

An immutable entity cannot be updated by an application. Updates to an immutable object will be ignored, but an exception will not be thrown. @Immutable should only be used for root objects.

@Immutable, placed in the collection, makes the collection immutable, the meaning of adding and removing to and from the collection is not allowed. In this case, a HibernateException is thrown.

As far as I know, to create a table immutable in * .hbm.xml, an attribute mutable="false"can help.

+1
source

All Articles