So here is a sort of watch list for my self hopefully will be useful to someone else as well.
Implement hashCode and equals
You should always implement these methods in general but for your entities you should pay a bit more attention.The first thing you think when I say equals is probably to use the Id to distinguish between instance. Well, that’s gonna cause you a lot of troubles.
You need to keep in mind that you are working with db entities and not normal POJOs.
When Hibernate is fetching objects is your using collections and hence equals and hashCode to know if an object you are looking for is in the session. For new objects id will be null or 0.
That means when trying to save two objects of the same class the second is going to overwrite the first one.
Also when hibernate saves a new instance it will set the id, thus making it a different object while it is exactly the same.
You need to use some business keys. Unique codes are great but if you can’t think of anything just use a meaningful field and some timestamp (like creation date) to make it unique.
This is a good reference if you want to understand a bit further what’s happening.
Careful with One-to-One and Many-to-One relations
This is something you really need to know.
When mapping a relation as One-to-One or Many-to-One on the “One” side of the relation you can’t have lazy loading unless you specify the field as not nullable.
Why is that?
Essentially on the many side of the relation hibernate can use collection proxies and lazily load instances when required. On the “One” side there is no collection interface but instead a reference to one of your model classes.
Hibernate can proxy that one as well but only if it is sure the reference will never be null!
So remember if you want to have lazy loading use the not null on the one side together with the lazy annotation (or xml equivalent).
If your relation can be null but you still really want to make it lazy then you have some options:
- Create a value to represent that. For example if you have a relation like Person -&amp;gt;Partner just use a specific instance of Partner that means “no partner”.
- Use build time instrumentation. Check this
- Fake the one side using a List and getting the field with get(0)
Read more on the hibernate documentation.
Enable the statement logging
This is the only way to verify Hibernate is really doing what you expect him to do. Luckily enough there are different logging parameters that you can use to find out what is happening both at the HQL or if you want at the SQL level. You’ll be surprised how many times hibernate is running queries and you did not except it. Try to this from the very beginning and help the team understand the importance of having the best and least possible queries or you’ll surely have performance issue when running the application on some real data. To enable logging just set this property in the session configuration file
If you want to see it nicely formatted add
Watch what goes in the toString method.
This one is again related to what Hibernate fetches for you without you really being aware. Lots of times when you see queries but can’t figure out why some lazy list is being loaded then check the toString method.
It might be the culprit!
What are your hibernate gotchas?
Java Platform, Enterprise Edition is a widely used platform for enterprise server programming in the Java programming language.
This book covers exciting recipes on securing, tuning and extending enterprise applications using a Java EE 6 implementation.The book starts with the essential changes in Java EE 6. Then they will dive into the implementation of some of the new features of the JPA 2.0 specification, and look at implementing auditing for relational data stores.They will then look into how they can enable security for their software system using Java EE built-in features as well as using the well-known Spring Security framework. They will then look at recipes on testing various Java EE technologies including JPA, EJB, JSF, and Web services.Next they will explore various ways to extend a Java EE environment with the use of additional dynamic languages as well as frameworks.At the end of the book, they will cover managing enterprise application deployment and configuration, and recipes that will help you debug problems and enhance the performance of your applications.