CocheLee
CocheLee

Reputation: 121

Fetching the same object Hibernate to different objects

I discovered this problem today and haven't found any solution yet, I'm kind of learning how to use hibernate at this point so I'll try my best to explain the situation.

I have a this very simple class:

public class Descripcion {
    private long id;
    private String descripcion;
    private AuxTitularidad titularidad = new AuxTitularidad();
    private AuxTitularidad titularidad2 = new AuxTitularidad();

    //getters and setters below... all of them auto-generated
}

And this is my mapping file

<class name="Descripcion" table="DESCRIPCION">
    <id name="id" type="long" column="id">
        <generator class="identity"></generator>
    </id>
    <property name="descripcion" column="descripcion"/>
    <many-to-one name="titularidad" class="AuxTitularidad" column="id_titularidad1"  />
    <many-to-one name="titularidad2" class="AuxTitularidad" column="id_titularidad2"/>
</class>

This mapping worked for me, no problems at all unless "titularidad" and "titularidad2" have the same value (reference the same object). When this happens, hibernate just fetch a single object of the class AuxTitularidad and uses it for both "titularidad" and "titularidad2".

It shouldn't be a problem if I was fetching them just for reading, but I need to make the update using an HTML form, and since the instance of "Descripcion" is holding two references to the same instance of "AuxTitularidad", it just keeps a single value.

Examples:

Number 1: These are the values stored in my DB:

 titularidad: 17
 titularidad2: 23

So I use the HTML form to make the update, and assign them these new values:

 titularidad: 30
 titularidad2: 28

Works!

Number 2: These are the values stored in my DB:

 titularidad: 18
 titularidad2: 18

So I use the HTML form to make the update, and assign them these new values:

 titularidad: 12
 titularidad2: 32

Doesn't work. It saves this in the DB:

 titularidad: 32
 titularidad2: 32

I've checked the binding of the object from the HTML form and it is correct, I know for sure the problem is that both "titularidad" and "titularidad2" hold a reference to the same object (even looked at the ids of the objects). ¿Is there anyway to tell hibernate to get these into separate objects? ¿Some configuration or parameter?

Thanks in advance!

Upvotes: 0

Views: 1454

Answers (1)

JB Nizet
JB Nizet

Reputation: 691765

That's expected behavior. In a given session, a given entity exists only once.

If you want to assign another titularidad to your descripcion entity, you shouldn't modify the fields of the existing titularidads. Instead, you should get a reference to the titularidad you want to assign, and assign this reference:

Titularidad newTitularidad = session.load(Titularidad.class, newTitularidadId);
Titularidad newTitularidad2 = session.load(Titularidad.class, newTitularidad2Id);
descripcion.setTitularidad(newTitularidad);
descripcion.setTitularidad2(newTitularidad2);

Upvotes: 4

Related Questions