58

I have Hibernate entity that I have to convert to JSON, and I have to translate some values in entity, but when I translate values, these values are instantly saved to database, but I don't want to save these changes to database. Is there any workaround for this problem?

JonasCz
  • 11,660
  • 6
  • 43
  • 64
newbie
  • 23,730
  • 77
  • 197
  • 300

6 Answers6

51

You can detach an entity by calling Session.evict().

Other options are create a defensive copy of your entity before translation of values, or use a DTO instead of the entity in that code. I think these options are more elegant since they don't couple conversion to JSON and persistence layer.

Patrick
  • 2,624
  • 3
  • 27
  • 46
axtavt
  • 233,923
  • 40
  • 499
  • 475
  • 22
    In case of JPA, you can use: [`EntityManager.detach(object)`](http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#detach%28java.lang.Object%29) – Christian Müller Jan 13 '14 at 13:48
  • 3
    I was always using DTO for this purpose. Using defensive copy is good idea but the maintenance becomes tougher as down the year, people think the new object is a managed one but in realy it is actualy a local copy. – VimalKumar Jul 29 '15 at 19:47
3

I am also converting hibernate entities to JSON.

The bad thing when you close the session you cannot lazy load objects. For this reason you can use

hSession.setDefaultReadOnly(true);

and close the session after when you're done with the JSON.

elkarel
  • 701
  • 2
  • 6
  • 20
3

You can also avoid that your entities are attached to the Hibernate Session by using a StatelessSession:

StatelessSession session = sessionFactory.openStatelessSession();

instead of

Session session = sessionFactory.getCurrentSession();

Note that you must take care yourself of closing the StatelessSession, unlike the regular Hibernate Session:

session.close(); // do this after you are done with the session

Another difference compared to the regular Session is that a StatelessSession can not fetch collections. I see it's main purpose for data-fetching-only SQLQuery stuff.

You can read more about the different session types here:

http://www.interviewadda.com/difference-between-getcurrentsession-opensession-and-openstatelesssession/

yglodt
  • 12,552
  • 14
  • 82
  • 121
0
public static <E> E deepClone(E e) {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream oo;
    try {
        oo = new ObjectOutputStream(bo);
        oo.writeObject(e);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
    ObjectInputStream oi;
    try {
        oi = new ObjectInputStream(bi);
        return (E) (oi.readObject());
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
        return null;
    }
}

first: deepClone the session pojo
second: alter fields
then: do whatever you want to do

0

Close the Session. That will detach your entity for you, and no modifications will be flushed. If that's not possible, look into disabling autoFlush...but that's a whole other can of worms. The easiest is to close the Session and be done with it!

stevevls
  • 10,535
  • 1
  • 44
  • 50
0

In my case I just flushed and cleared the session.

session.flush(); session.clear();

Elias
  • 574
  • 8
  • 22