“From the unreal, lead us to the Real; from darkness, lead us unto Light; from death, lead us to Immortality. Om peace, peace, peace.”

Saturday, July 2, 2011

DRY your code with Hibernate Naming Strategy

DRY,KISS,Zero configuration,Configuration By Default etc..are the terms that we are frequently hearing from the world of Agile Software Development. In modern software development, lots of significance is there for Productivity. Various approaches are applying to achieve this including the above mentioned terms. NamingStrategy is such an approach to reduce your code or in the other way, to remove the boiler plate codes from your project.
2 kinds of mapping is used in Hibernate ORM. XML based and Annotation based. Both requires some work to map the entity class with corresponding table. By using
Naming strategy lots of work can be reduced and at the same time the coding standards can be maintained to a high level perfection.

Here I am explaining one naming strategy that I am using. The ImprovedNamingStrategy, which comes from the Master Gavin King.
The Naming strategy can be modified by writing your own style of working. Or in some exceptional cases you can use either annotation or xml to specify the special behaviour for some fields.

Here I am listing down a few pieces of code to explain the functionality of ImprovedNamingStrategy.


1)hibernate.cfg.xml





com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/naming_strategy
username
password

1

org.hibernate.dialect.MySQLDialect 

thread

org.hibernate.cache.NoCacheProvider

true

create




2)UserInfo.java(Entity class)
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class UserInfo {

@Id
@GeneratedValue
private Integer id;
private String email;
private String firstName;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

}


3.UserInfoDao.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.ImprovedNamingStrategy;

public class UserInfoDao {

public void save() {
UserInfo userInfo = new UserInfo();
userInfo.setEmail("naming@hibernate.com");
userInfo.setFirstName("Improved Naming Strategy");
SessionFactory sessionfactory = new AnnotationConfiguration()
.configure().setNamingStrategy(new ImprovedNamingStrategy())
.buildSessionFactory();
Session session = sessionfactory.getCurrentSession();
Transaction tx = session.beginTransaction();
session.save(userInfo);
tx.commit();
sessionfactory.close();

}
}



4)Main.java
public class Main {
public static void main(String a[]) {
UserInfoDao dao = new UserInfoDao();
dao.save();
}
}
In the above codes, you couldn't see the nasty XML mapping files. Also the entity class looks clean and simple in the absence of @Column annotaion every where. But if you want, you can write those things for overriding the behaviour in exceptional cases.

Table will be created automatically with following naming convention.


Table:user_info
Fields:id,email,first_name


These naming can be changed by creating your own naming strategy by implementing the hibernate NamingStrategy interface.


Following Libraries are used for the above application:
hibernate3.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
log4j-1.2.16.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
slf4j-api-1.6.1.jar
mysql-connector-java-5.1.13-bin.jar

References:-
1.ImprovedNamingStrategy
2.DZONE-Article
3.DRY 4.KISS

2 comments:

  1. Great post. Would you consider joining DZone's MVB program (see dzone.com/aboutmvb for more details)? It would be great to have you on board.
    If you're interested, send me an email and we can discuss further.

    Regards
    James

    ReplyDelete