3

I have an ORM entity loaded by Hibernate, with certain associations LAZY loaded when I need them. This entity is transported thru MQ to client application (and the client is .NET, so I'm using MessagePack to serialize/deserialize) and when the entity is serialized by MsgPack, it tries to access the lazy loaded association and it fails, as the session is already closed. And even if it did not failed, I do not want it to load the association in some cases.

Is there a way to tell hibernate to fill the lazy associations with empty values instead of proxies for some query results or do I have to iterate the returned list and perform these changes by myself?

Thanks for your answers!

1
  • 1
    Pass a data transfer object rather than the POJO - this way you can control what you pass along. Commented Mar 25, 2013 at 21:33

2 Answers 2

2

You have no other way, but to use DTO objects, to tranfer it through MQ,

  1. Load entity from DB using hibernate
  2. convert it to DTO object which implents Serializable.
  3. Transfer it to consumer using MQ
  4. Convert it to any other entity on other side.
Sign up to request clarification or add additional context in comments.

3 Comments

I was really afraid, this is the only option. I was trying hard to avoid DTOs as much as possible...
I believe that the best practice is to use DTO, while transferring object in MQ. Don't serialize JPA annotated object. I faced such dilemma in the past and believe that DTO is the best solution.
BTW, I don't know whether you are you using Spring or not, but if you are I recommend you to use ConversionServiceFactoryBean, it might give you pretty good "out of the box" conversion service solution
0

I think that @Transient only applies to ORM. If you do not want your field to be serialized, you should use key word "transient".

private transient List<Object> myTransientList;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.