Dodge Hibernate coding mistakes with Scertify’s code analysis – Part 2

Posted by Armel GOURIOU in Blog on May 10, 2013 with No Comments

In the post Dodge Hibernate coding mistakes with Scertify’s code analysis we presented some of our coding rules that deal with the implementation of equals and hashCode methods with Hibernate. In this article, we present other rules from our Hibernate/JPA repository.

We’re going to present 5 rules related to different topics and that aim at improving the robustness and the maintainability of your code.

Rule 1 : Hibernate Entity Does Not Define No Argument Constructor

This rule detects any Hibernate entity that does not define a no argument constructor.

Hibernate creates new instances of entities through reflexion, with the Class.newInstance() method. This method requires a no argument constructor to succeed. If an entity does not provide a no argument constructor, an Hibernate’s InstantiationException will be thrown.

Rule 2 : Always declare the type for a date

This rule detects entities’ field that are of the Java type java.util.Date but where the “type” attribute is not set. This attribute enables Hibernate to know how to map the java.util.Date to a SQL type : DATE, TIME or TIMESTAMP.

For example, let’s suppose we have an entity with a field called “birthday”. This entity is defined in an hibernate xml mapping file and the field “birthday” is defined as follow :

This would be flagged as a violation by the rule. To correct it, one could add the “type” attribute :

Rule 3 : Hibernate/XML Limit the number of class per mapping file

This rule detects hibernate mapping files that define more than one mapping. For sake of clarity and readability, one file should correspond to one entity and no more. It is possible to configure the number of mappings allowed in a file, however we recommend to use the default value : 1.

Rule 4 : JPA Entity class must not be final

This rule detects hibernate entities declared as final. This can be limiting in term of performance optimization as stated in http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/persistent-classes.html

A central feature of Hibernate, proxies, depends upon the persistent class being either non-final, or the implementation of an interface that declares all public methods.

You can persist final classes that do not implement an interface with Hibernate. You will not, however, be able to use proxies for lazy association fetching which will ultimately limit your options for performance tuning.

Rule 5 : Hibernate Composite Key Are Prohibited

This rule detects usages of the “Composite key” feature of hibernate. Composite keys complicate the code and the mappings. There can be situations where they are required, like when dealing with a legacy database. However, many teams may want to forbid their usage in new applications. This rule is there for them!

So, we’ve presented 5 rules to check various aspects of Hibernate’s best practices. What do you think of those best practices? Do you think they’re worth checking through code analysis? Please let us know in the comments!

About Scertify™ for Hibernate

Scertify™ for Hibernate is a product add-on for Scertify™. It includes a repository of 30 exclusive coding rules and best practices for Hibernate and JPA-based ORM frameworks. This rules repository works with both the Scertify Professional edition on the developers’ IDE (Eclipse), and the Scertify Enterprise edition in continuous integration (Maven and CLI).

Posted by
Armel GOURIOU (See other posts)

Armel Gouriou has a Master's degree in computer science and is a huge Java addict. He originally worked for Tocea's R&D team where he took part in the development of Tocea's audit and refactoring softwares. He is now a member of the professional services team where his mission is to provide companies with solutions to tackle their technical debt.

300888-blackangel

Technical Debt & Code Quality Management: Participate to our online survey!

Posted by Michael MULLER in Blog on May 6, 2013 with No Comments

How do you manage Technical Debt of your development projects? What tools & metrics do you use to monitor Code Quality? If you’re involved near or far into software development, code quality or technical debt management, please participate to our survey. The results will be commented during a webinar scheduled for this summer.
  • Wich code metrics have the biggest impact on Technical Debt?
  • What part of development time do you allocate to prevent code quality defects?
  • What tools do you use to control/measure Code Quality?
  • Do you share a common coding rule repository with developers?

» Participate to the survey

Posted by
Michael MULLER (See other posts)

+10 years experienced Marketing & Communication Manager, specialized in Technology & Innovating companies. Marketing Manager at Tocea. Executive editor at http://dsisionnel.com, a french IT magazine. Creator of http://d8p.it, a cool url shortener. Dad of 2 kids.

Technical Debt: Refactor your Code on the fly in Eclise with Scertify’s Quick-Fix feature

Posted by Armel GOURIOU in Blog on April 29, 2013 with No Comments

When dealing with technical debt, it is important to handle it as soon as possible. The best way to handle it, of course, it to get rid of it as soon as it appears. This may not be always possible, but when it is, then it should be done. That’s why at Tocea we developed a feature for our Eclipse’s plugin that gives you the opportunity to immediately correct a code defect.

Read more…

Easycoverage 0.2.3 is available for download

Posted by Tocea in Release Notes on April 24, 2013 with No Comments

Scertify™ EasyCoverage is an open-source framework that can dynamically generate unit tests to perform basic verifications, thus leaving more time for the developers. The latest version (0.2.3) is now available for download. Find below the changelog.

Read more…

Scertify™ now integrates 225 Javascript coding rules from Closure Compiler

Posted by Michael MULLER in HowTos & Tutorials on April 24, 2013 with No Comments

After the integration of JSLint’s coding rules, Scertify™ Professional now integrates 225 coding rules from Closure Compiler, making code quality of Javascript projects possible directly within Eclipse.

Read more…

Scertify™ now integrates JSLint’s Javascript coding rules

Posted by Michael MULLER in HowTos & Tutorials on April 22, 2013 with No Comments

Scertify™ Professional now supports code quality analysis of Javascript projects, directly within Eclipse. It integrates 281 coding rules of JSLint, one of the most popular static code analysis tool for Javascript.

Read more…

Scertify™ Eclipse Plugin 1.10.2

Posted by Tocea in News on April 5, 2013 with No Comments

The version 1.10.2 of our Scertify™ Eclipse plugin has been released. Find below the changelog. This plugin is included in Scertify™ Professional Edition.

Read more…

How to extend Scertify™ features with product add-ons in Eclipse?

Posted by Michael MULLER in HowTos & Tutorials on April 4, 2013 with No Comments

Build your Scertify™ version that exactly matches with your needs

As we plan to release something like 20 Scertify™ product add-ons this year (ie: metrics and coding rules for testability, detection of duplicated code, Javascript, security, PHP, Spring, etc.), learn in this post how to extend Scertify™ to get the features that exactly match with your needs. Read more…

Dodge Hibernate coding mistakes with Scertify’s code analysis

Posted by Armel GOURIOU in HowTos & Tutorials on April 4, 2013 with No Comments

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.

Read more…

Scertify™ for Hibernate 1.0 is available for download

Posted by Tocea in Release Notes on April 3, 2013 with No Comments

Our first add-on dedicated to Hibernate is released and available for download. Scertify™ for Hibernate is a repository of coding rules and best practices for Hibernate and JPA-based ORM frameworks. The repository now includes 30 exclusive rules.

Scertify™ for Hibernate is a product add-on and works with Scertify™ Professional (Eclipse) and Enterprise (Maven and CLI) editions.
Read more…

Your cart

  • No products in the cart.
Scertify™ is a suite of tools dedicated to development teams, designed to continuously track and eliminate software-related risks, through code quality & architecture compliance monitoring associated with computer-aided refactoring capabilities.
» Get your trial version!

Who we are?

Tocea provides Development Teams with innovative solutions enabling them to continuously control and improve the quality and integrity of their applications. We support developers, architects and project managers in their permanent effort to tackle IT debt and costs, by providing them with unique information on Code & Architecture Quality and some exclusive computer-aided refactoring features.

LoadingRetrieving latest tweet...

Back to Top

2013 © All rights reserved. Tocea SA. Legal Notice