|By Michael Muller||
|April 26, 2013 10:00 AM EDT||
Hibernate is one of the most used ORM Java frameworks out there. It is really simple to use, just add few annotations and you're ready to go. However, it is also really easy to experience strange behaviors and bugs if you don't respect Hibernate's best practices. That's why at Tocea we developed rules to detect coding mistakes and to make sure that your experience with Hibernate will be painless.
Our Hibernate repository currently contains 30 rules. Some of them are related to the detection of possible bugs, other treat of maintainability or performance. In this article, we're going to present four rules that deal with a famous Hibernate's usage problem : the implementation of equals() and hashCode() methods.
What's the problem?
Equals and hashCode are used to compare objects. The default implementation of equals compares the object's address in memory. This is good as long as your objects are in memory, but Hibernate's goal is precisely to move them out of memory. Within a Hibernate session, Hibernate takes care of managing objects equality between a persistent identity and a Java identity.
However, when dealing with objects that come from different sessions, Hibernate can't do it. That's why it is necessary to override equals and hashCode.
This is our rule n°1 : HbEntitiesMustImplementEqualsHashCode detects any Hibernate entity that does not implement both methods. We also have HbEmbeddableEntitiesMustImplementEqualsHashCodethat does the same for embeddable entities.
First step to solve it : implement equals()
So, I have a User and I want to implement the equals() method. I can just use its Id...
Ooops, or can't I? Indeed, if your Id is generated (as it is likely to be), it will be assigned once the object is persisted. Before that, all non-persisted entities have the same null id. So you could experience strange equality results... The good solution is to rely on the objects business fields to compare them, like the first name and the last name of my user.
This is our rule n°3 : HbEntitiesEqualsMustAvoidIDField detects usage of id field in equals().
Second step to solve it : implement hashCode()
I'm now ready to implement hashCode(). The Java language requires that if a.equals(b), then a.hashCode() is the same as b.hashCode(). As a consequence, I must use the same fields in hashCode() as I used in equals(). This may seem trivial for simple entities, but through evolution of an entity, this can be easy to forget to add a new field to one of the method.
This is our rule n°4 : HbEntityEqualsAndHashCodeUseDifferentFields detects equals and hashCode methods that use different fields.
To sum up, with this four rules you should be able to prevent a good deal of bugs related to incorrect usage of equals and hashCode in Hibernate entities. Have you already encountered such problems? Do you think there should be more rules regarding the implementation of these two methods? Please let us know in the comments!
Oh, and we have presented four of our thirty Hibernate rules, so that leaves plenty of interesting stuff to talk about : stay tuned!
Whether they’re located in a public, private, or hybrid cloud environment, cloud technologies are constantly evolving. While the innovation is exciting, the end mission of delivering business value and rapidly producing incremental product features is paramount. In his session at @DevOpsSummit at 19th Cloud Expo, Kiran Chitturi, CTO Architect at Sungard AS, will discuss DevOps culture, its evolution of frameworks and technologies, and how it is achieving maturity. He will also cover various st...
Sep. 27, 2016 06:45 PM EDT Reads: 1,786
Identity is in everything and customers are looking to their providers to ensure the security of their identities, transactions and data. With the increased reliance on cloud-based services, service providers must build security and trust into their offerings, adding value to customers and improving the user experience. Making identity, security and privacy easy for customers provides a unique advantage over the competition.
Sep. 27, 2016 06:30 PM EDT Reads: 3,554
There are several IoTs: the Industrial Internet, Consumer Wearables, Wearables and Healthcare, Supply Chains, and the movement toward Smart Grids, Cities, Regions, and Nations. There are competing communications standards every step of the way, a bewildering array of sensors and devices, and an entire world of competing data analytics platforms. To some this appears to be chaos. In this power panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, Bradley Holt, Developer Advocate a...
Sep. 27, 2016 06:30 PM EDT Reads: 2,168
SYS-CON Events announced today that Niagara Networks will exhibit at the 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. Niagara Networks offers the highest port-density systems, and the most complete Next-Generation Network Visibility systems including Network Packet Brokers, Bypass Switches, and Network TAPs.
Sep. 27, 2016 06:15 PM EDT Reads: 344
SYS-CON Events announced today that Secure Channels will exhibit at the 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. The bedrock of Secure Channels Technology is a uniquely modified and enhanced process based on superencipherment. Superencipherment is the process of encrypting an already encrypted message one or more times, either using the same or a different algorithm.
Sep. 27, 2016 06:00 PM EDT Reads: 1,688
If you’re responsible for an application that depends on the data or functionality of various IoT endpoints – either sensors or devices – your brand reputation depends on the security, reliability, and compliance of its many integrated parts. If your application fails to deliver the expected business results, your customers and partners won't care if that failure stems from the code you developed or from a component that you integrated. What can you do to ensure that the endpoints work as expect...
Sep. 27, 2016 05:45 PM EDT Reads: 1,653
So, you bought into the current machine learning craze and went on to collect millions/billions of records from this promising new data source. Now, what do you do with them? Too often, the abundance of data quickly turns into an abundance of problems. How do you extract that "magic essence" from your data without falling into the common pitfalls? In her session at @ThingsExpo, Natalia Ponomareva, Software Engineer at Google, provided tips on how to be successful in large scale machine learning...
Sep. 27, 2016 05:30 PM EDT Reads: 1,989
If you had a chance to enter on the ground level of the largest e-commerce market in the world – would you? China is the world’s most populated country with the second largest economy and the world’s fastest growing market. It is estimated that by 2018 the Chinese market will be reaching over $30 billion in gaming revenue alone. Admittedly for a foreign company, doing business in China can be challenging. Often changing laws, administrative regulations and the often inscrutable Chinese Interne...
Sep. 27, 2016 05:15 PM EDT Reads: 285
In his general session at 18th Cloud Expo, Lee Atchison, Principal Cloud Architect and Advocate at New Relic, discussed cloud as a ‘better data center’ and how it adds new capacity (faster) and improves application availability (redundancy). The cloud is a ‘Dynamic Tool for Dynamic Apps’ and resource allocation is an integral part of your application architecture, so use only the resources you need and allocate /de-allocate resources on the fly.
Sep. 27, 2016 05:15 PM EDT Reads: 2,756
Enterprise IT has been in the era of Hybrid Cloud for some time now. But it seems most conversations about Hybrid are focused on integrating AWS, Microsoft Azure, or Google ECM into existing on-premises systems. Where is all the Private Cloud? What do technology providers need to do to make their offerings more compelling? How should enterprise IT executives and buyers define their focus, needs, and roadmap, and communicate that clearly to the providers?
Sep. 27, 2016 05:00 PM EDT Reads: 1,593
Complete Internet of Things (IoT) embedded device security is not just about the device but involves the entire product’s identity, data and control integrity, and services traversing the cloud. A device can no longer be looked at as an island; it is a part of a system. In fact, given the cross-domain interactions enabled by IoT it could be a part of many systems. Also, depending on where the device is deployed, for example, in the office building versus a factory floor or oil field, security ha...
Sep. 27, 2016 04:45 PM EDT Reads: 391
More and more companies are looking to microservices as an architectural pattern for breaking apart applications into more manageable pieces so that agile teams can deliver new features quicker and more effectively. What this pattern has done more than anything to date is spark organizational transformations, setting the foundation for future application development. In practice, however, there are a number of considerations to make that go beyond simply “build, ship, and run,” which changes ho...
Sep. 27, 2016 04:00 PM EDT Reads: 2,650
Using new techniques of information modeling, indexing, and processing, new cloud-based systems can support cloud-based workloads previously not possible for high-throughput insurance, banking, and case-based applications. In his session at 18th Cloud Expo, John Newton, CTO, Founder and Chairman of Alfresco, described how to scale cloud-based content management repositories to store, manage, and retrieve billions of documents and related information with fast and linear scalability. He addres...
Sep. 27, 2016 03:15 PM EDT Reads: 2,863
SYS-CON Events announced today that Commvault, a global leader in enterprise data protection and information management, has been named “Bronze Sponsor” of SYS-CON's 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. Commvault is a leading provider of data protection and information management solutions, helping companies worldwide activate their data to drive more value and business insight and to transform moder...
Sep. 27, 2016 03:15 PM EDT Reads: 2,760
SYS-CON Events announced today that eCube Systems, a leading provider of middleware modernization, integration, and management solutions, will exhibit at @DevOpsSummit at 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. eCube Systems offers a family of middleware evolution products and services that maximize return on technology investment by leveraging existing technical equity to meet evolving business needs. ...
Sep. 27, 2016 03:15 PM EDT Reads: 1,394