Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

java - MVC With Lazy Loading

Correct me if this is an exact duplicate, I know this topic is discussed often but can't find a definitive answer.

The question:

What is the best practical solution to handling Hibernate objects in a MVC webapp?

The details:

I am using Hibernate and want to leverage lazy loading where possible.
I am working in a MVC style webapp.
I hate getting lazy load initialization exceptions.
I hate having to reattach Hibernate objects between transactions.

The options:

  1. Eager load everything
    • Solves the lazy initialization problem but makes my queries bigger
  2. Use some 'Open Session in View' concept
    • I love the simplicity of it
    • Objects still need to be reattached, and in an AJAXy setup, quite frequently
    • A session is opened for EVERY request
  3. 'touch' items I need before leaving the transaction
    • Seems flimsy at best.. and tedious
  4. Create different, simplified, 'detached' objects so the view never sees real Hibernate objects
    • These could be simpler than full Hibernate objects so it's not like a full eager load of the model
    • I've heard this recommended in places to but just seems like more liability/code/work
  5. Open a session when ever I want to interact with Hibernate objects.
    • This can be wrapped up in a Spring Service layer pretty nicely, but seems excessive at times. Eg: I want hibernateObject.getRelatedObjects() but need to say something like springService.getRelatedObjects(hibernateObject)

Am I missing something?
Have I over-thought things?
Have I under-thought things?

PS:

For a web framework I'm using ZK but don't really want a ZK specific answer.
I'm also using Spring and am cool with a Spring specific answer as it's so ubiquitous.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use 4-ish - Don't use open session in view, don't have your hibernate entities bubble all the way up to the view instead have transformers translate between the hibernate entities and your domain objects or 'view beans' depending on how you want to work it.

I think of Hibernate entities as just a persistence strategy not a domain model or UI representation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...