Jonathan Huang

Ranch Hand
+ Follow
since Jun 23, 2006
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jonathan Huang

There is very limited documentation. You have to buy their expert guides to get everything you need.
Hibernate isn't a framework or follow one. It's a service.

Spring MVC is a good MVC Framework which hibernate works very well in.
I found that WTP 0.7 for Eclipse was using older lib files then what Hibernate Tools was using causing classpath issues.

That was my reasons for Hibernate Tools not working. There could be others though.
Sometimes if the object's values changed in the DB hibernate needs to refresh the Session level cache or it might use old values. This is because if the item is in the session, it'll pull it from the session instead of the DB. Try a session.refresh on your object.
You also have to remember that if you have a Timestamp in the DB and take it out, Hibernate takes it as a date, you lose precision... Or am I backwards?

Anywho, to keep your precision you need to write a custom type to type cast properly.

There are some good dicusions of this at the official forums.
The alternatives are what you can dream up.

Session per transaction
Session per app
Session per HTTPSession

Session for every 2 transactions....

etc. . .

It all depends on how many users may potentially be using the system, how well you want to be able to grow your system, etc, that will ultimately decide which way to go with your session.

The reason to use the session in view pattern is so you can lazy load objects in the view. It is easy to write a filter that opens and closes the thread bound session so you don't have to take care of it programatically.

Also, the Spring framework comes with some handy features such as transaction AOP point cuts and a OpenSessionInViewFilter already made for you. It removes all the boiler plate code for you, and you can focus on your business logic.

As to not mapping collections . . . Sounds interesting. Let me know how that goes. And if you don't have any collections you could use the session per transaction strategy. It works very well for simple schemas.

Look at the CaveatEmptor example hibernate provides. You can also check out a plugin for Eclipse called HibernateSync. I wouldn't recommend using it, but just to look at the code it generates. Gives you insight on other posibilities.

good luck
I started by reading the entire Hibernate 3 docs. Then doing the Hibernate in Action demo. Then just testing things out and writing code/mappings.

Then reading the offical and these forums whenever possible. Someone elses problem could always be my own.

Most problems you get you can figure out from the docs or reading forums. The more in depth stuff you are going to have to go through source code and their Jira issues.

There are lots of best practices you can learn from the Hibernate Caveat Emptor example and the Hibernate Wiki pages are nice.
The reason might be because everytime you call HibernateSessionFactory it is instantiating a new SessionFactory.... which is bad. But I haven't seen all your code so I could be wrong.

For examples of a DAO patterns used with Hibernate, you can check out the CaveatEmptor example, HibernateSync, and after you have an understanding of those, you might like to check out Spring's IOC container.

CaveatEmptor can be found at:
Just do a google for HibernateSync
Could you post the calling code and stacktrace? Off hand I don't see anything wrong with your code/mappings.
Well, at least I know I am not going crazy.

I've been working through the second level cache functionality, returning the CacheTypes and playing around with them. Im trying to find an association between the Role Name, and the Entity Name so I can evict them myself progamatically. The best I can do so far is evict the entire region, and not the individual associated values.

I'll let you know how it goes.
Im looping through the cache stats (in a .jsp page):
This is how I know what exactly is being evicted and what is not.

And just so we're both clear, The Parent.Child collection is being evicted from the Second Level Cache.

The Child objects that Parent.Child points to are NOT being evicted. That's where im having issues.
[ August 18, 2006: Message edited by: Jonathan Huang ]
So each different DS will need a different .cfg.xml file.

in each one you can do <class-cache> and <collection-cache> tags. Just set the region="someRegion" different in each one so that even though they're in the same JVM, they'll be looking things up from different locations.

Check out the Hib3 documentation for more details.
Sorry, let me try to explain myself a little bit better. This is essentailly my expectations:

A1 has a collection B1
A2 has a Collection B2

B1 contains B_B1, B_B2, B_B3
B2 contains B_B5

B_B1 Points to Q1, B_B2->Q2 and B_B3->Q3.
If I evict A1 (and it's collections), shouldn't A1, B1, and Q1, Q2, Q3 all be Evicted from the cache. So, not the entire cache region, but just the associated values.

So for a more concrete example (all set to be cachable): that has a collection of,, that has a collection of,,

In hibernate's cache stats, I will see 3 regions:
Parent, Parent.Child, and Child.

In the above example, and share

There are 2 things which I don't understand going on:

1) If you set Cascade=evict on the Parent.Child collection in the mapping file, why does:
SessionFactory.evict(, not evict Parent.Child where

2) If you do SessionFactory.evictCollection(Parent.child, why does:,, and (the parent's collection) is evicted from the cache. But why isn't,, and NOT evicted.

Still left in the cache SHOULD be:,,

It is my understanding that if cascade=evict (or as the documentation states, should be set to "all" or "all-delete-orphan"), on the parent's collections, the associated collections will be evicted. On top of that, since the associated collections, are really pointers to another entity in the cache, those should be evicted as well. Not the entire cache region, but just the ones associated.

Hope that helps explain myself better...
[ August 18, 2006: Message edited by: Jonathan Huang ]