java:fiststepswithhibernate
Differences
This shows you the differences between two versions of the page.
Previous revision | |||
— | java:fiststepswithhibernate [2022/12/02 22:02] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Put hibernate into your life ====== | ||
+ | |||
+ | I've created from zero various projects with hibernate, and because it a step somewhat complex, I've decided to document it, so in the following stages it will be easier to achieve. | ||
+ | |||
+ | ===== Get the mysql driver ===== | ||
+ | |||
+ | Ok, if you are using oracle, you have to change this step for the proper download of the oracle JDBC driver. | ||
+ | |||
+ | To download the mysql driver, check out www.mysql.com. Gather the jar file (something called " | ||
+ | |||
+ | ===== Download Hibernate ===== | ||
+ | |||
+ | Point your browser to http:// | ||
+ | |||
+ | Put all the following jar files: | ||
+ | |||
+ | * from the " | ||
+ | * from the " | ||
+ | * from the " | ||
+ | * from the " | ||
+ | * from the " | ||
+ | |||
+ | |||
+ | ===== Add the new jar files into your eclipse project ===== | ||
+ | |||
+ | Of course, you have to add all the new jar files into your eclipse project. | ||
+ | |||
+ | ===== Open your applicationContext.xml and let's start the party ===== | ||
+ | |||
+ | ==== First Step: create a datasource ===== | ||
+ | |||
+ | You have to have in place a running database, so you will have to have the following values: | ||
+ | |||
+ | * server address (and port if it is different than 3306) | ||
+ | * user (for god's sake, don't use root!!!!) | ||
+ | * password (Have I to say that the more complex the better?) | ||
+ | |||
+ | And put these values into the proper places in the following xml snippet. And by the moment, you are done. | ||
+ | |||
+ | <code xml> | ||
+ | < | ||
+ | STEP ONE | ||
+ | | ||
+ | Datasource, the access to mysql. This datasource uses | ||
+ | c3p0, a connection pooling interface and hence it | ||
+ | optimizes the use of connections. This configuration | ||
+ | uses a new connection each time is requested. | ||
+ | | ||
+ | --> | ||
+ | <bean id=" | ||
+ | class=" | ||
+ | destroy-method=" | ||
+ | scope=" | ||
+ | < | ||
+ | To test a connection: | ||
+ | mysql -h HOSTNAME -u USERNAME -p | ||
+ | and aftwerwards introduce the password when requested | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Second Step: create a JdbcTemplate bean ===== | ||
+ | |||
+ | We will create a bean named " | ||
+ | |||
+ | <code xml> | ||
+ | |||
+ | <bean id=" | ||
+ | class=" | ||
+ | scope=" | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Third Step: create a hibernateTemplate bean to add to your beans ===== | ||
+ | |||
+ | <code xml> | ||
+ | <bean id=" | ||
+ | class=" | ||
+ | scope=" | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Hola, este es un texto de ejemplo para Oscar. | ||
+ | |||
+ | |||
+ | ===== Four step: create a sessionFactory (the mother of all hibernate sessions) ===== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | Of course, you can put the configuration in a properties file, but haven' | ||
+ | |||
+ | Here is my example: | ||
+ | |||
+ | |||
+ | <code xml> | ||
+ | <bean id=" | ||
+ | class=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <!-- | ||
+ | < | ||
+ | --> | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | <prop key=" | ||
+ | <prop key=" | ||
+ | <prop key=" | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====== Map your first table ====== | ||
+ | |||
+ | |||
+ | Despite you can use the Hibernate Tools to create it, by the moment I prefer to map the tables to objects by hand. | ||
+ | |||
+ | I've created this table: | ||
+ | |||
+ | <code sql> | ||
+ | drop table if exists jn_local_folder; | ||
+ | |||
+ | create table jn_local_folder | ||
+ | ( | ||
+ | id | ||
+ | message_id | ||
+ | ); | ||
+ | |||
+ | </ | ||
+ | |||
+ | and I mapped it into an object: | ||
+ | |||
+ | <code java> | ||
+ | import javax.persistence.Column; | ||
+ | import javax.persistence.Entity; | ||
+ | import javax.persistence.GeneratedValue; | ||
+ | import javax.persistence.Id; | ||
+ | import javax.persistence.Table; | ||
+ | |||
+ | |||
+ | @Entity | ||
+ | @Table( name = " | ||
+ | public class LocalFolder | ||
+ | { | ||
+ | private long id; | ||
+ | private String messageId; | ||
+ | |||
+ | @Id | ||
+ | @Column( name=" | ||
+ | @GeneratedValue | ||
+ | public long getId() | ||
+ | { | ||
+ | return id; | ||
+ | } | ||
+ | public void setId(long id) | ||
+ | { | ||
+ | this.id = id; | ||
+ | } | ||
+ | |||
+ | @Column( name=" | ||
+ | public String getMessageId() | ||
+ | { | ||
+ | return messageId; | ||
+ | } | ||
+ | public void setMessageId(String messageId) | ||
+ | { | ||
+ | this.messageId = messageId; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ====== Use it into your own beans ====== | ||
+ | |||
+ | It's the first time I've used hibernate 4 with spring, so I am in the process of investigating their possibilities. The first shock I've received is that there is no [[http:// | ||
+ | |||
+ | Thinking it twice, it's better because the over-use of HibernateTemplate tied your code very much to Spring. With this approximation, | ||
+ | |||
+ | I've made a very simple DAO object that can act as a basis for future developments. Here it is: | ||
+ | |||
+ | <code java> | ||
+ | |||
+ | import org.hibernate.Query; | ||
+ | |||
+ | import org.hibernate.Session; | ||
+ | import org.hibernate.SessionFactory; | ||
+ | |||
+ | public class LocalFolderDao | ||
+ | { | ||
+ | private SessionFactory sessionFactory; | ||
+ | private Session ses; | ||
+ | |||
+ | |||
+ | public void setSessionFactory( SessionFactory sessionFactory ) | ||
+ | { | ||
+ | this.sessionFactory = sessionFactory; | ||
+ | this.ses = sessionFactory.openSession(); | ||
+ | } | ||
+ | public SessionFactory getSessionFactory() | ||
+ | { | ||
+ | return this.sessionFactory; | ||
+ | } | ||
+ | |||
+ | public void doSomething() | ||
+ | { | ||
+ | |||
+ | StringBuilder hql = new StringBuilder(); | ||
+ | |||
+ | hql.append( "from LocalFolder " ); | ||
+ | hql.append( "where messageId = :messageId " ); | ||
+ | |||
+ | Query q = ses.createQuery( hql.toString() ); | ||
+ | |||
+ | q.setParameter( " | ||
+ | |||
+ | System.out.println( q.list().size() ); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | <code java> | ||
+ | package com.supermanhamuerto.nestor.localfolder; | ||
+ | |||
+ | import org.apache.commons.logging.Log; | ||
+ | import org.apache.commons.logging.LogFactory; | ||
+ | import org.hibernate.Query; | ||
+ | |||
+ | import org.hibernate.Session; | ||
+ | import org.hibernate.SessionFactory; | ||
+ | import org.hibernate.Transaction; | ||
+ | |||
+ | import com.supermanhamuerto.nestor.MailMessage; | ||
+ | import com.supermanhamuerto.nestor.entity.MessageEnt; | ||
+ | |||
+ | public class LocalFolderDaoImpl implements LocalFolder | ||
+ | { | ||
+ | private static Log log = LogFactory.getLog( LocalFolderDaoImpl.class ); | ||
+ | private SessionFactory sessionFactory; | ||
+ | private Session ses; | ||
+ | |||
+ | |||
+ | public void setSessionFactory( SessionFactory sessionFactory ) | ||
+ | { | ||
+ | this.sessionFactory = sessionFactory; | ||
+ | this.ses = sessionFactory.openSession(); | ||
+ | } | ||
+ | public SessionFactory getSessionFactory() | ||
+ | { | ||
+ | return this.sessionFactory; | ||
+ | } | ||
+ | |||
+ | /* (non-Javadoc) | ||
+ | * @see com.supermanhamuerto.nestor.localfolder.LocalFolder# | ||
+ | */ | ||
+ | @Override | ||
+ | public boolean messageExist( MailMessage msg ) | ||
+ | { | ||
+ | return messageExist( msg.getId() ); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean messageExist(String id) | ||
+ | { | ||
+ | // in case of error, the message is declared as " | ||
+ | // to avoid entering the same message again and again | ||
+ | boolean result = true; | ||
+ | |||
+ | try | ||
+ | { | ||
+ | StringBuilder hql = new StringBuilder(); | ||
+ | |||
+ | hql.append( "from MessageEnt " ); | ||
+ | hql.append( "where messageId = :messageId " ); | ||
+ | |||
+ | Query q = ses.createQuery( hql.toString() ); | ||
+ | |||
+ | q.setParameter( " | ||
+ | |||
+ | result = !q.list().isEmpty(); | ||
+ | |||
+ | } | ||
+ | catch( Exception e ) | ||
+ | { | ||
+ | log.error( e ); | ||
+ | } | ||
+ | |||
+ | return result; | ||
+ | } | ||
+ | |||
+ | /* (non-Javadoc) | ||
+ | * @see com.supermanhamuerto.nestor.localfolder.LocalFolder# | ||
+ | */ | ||
+ | @Override | ||
+ | public void saveMessage( MailMessage msg ) | ||
+ | { | ||
+ | MessageEnt msgEntity = new MessageEnt(); | ||
+ | |||
+ | Transaction tx = ses.beginTransaction(); | ||
+ | |||
+ | msgEntity.setMessageId( msg.getId() ); | ||
+ | msgEntity.setSent( msg.getSentDateAsDate() ); | ||
+ | msgEntity.setMsgFrom( msg.getFrom() ); | ||
+ | msgEntity.setMsgTo( msg.getTo() ); | ||
+ | msgEntity.setSubject( msg.getSubject() ); | ||
+ | msgEntity.setBody( msg.getBodyAsText() ); | ||
+ | ses.saveOrUpdate( msgEntity ); | ||
+ | |||
+ | tx.commit(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void deleteMessage( MailMessage msg ) | ||
+ | { | ||
+ | MessageEnt msgEntity = new MessageEnt(); | ||
+ | |||
+ | Transaction tx = ses.beginTransaction(); | ||
+ | |||
+ | msgEntity.setMessageId( msg.getId() ); | ||
+ | msgEntity.setSent( msg.getSentDateAsDate() ); | ||
+ | msgEntity.setMsgFrom( msg.getFrom() ); | ||
+ | msgEntity.setMsgTo( msg.getTo() ); | ||
+ | msgEntity.setSubject( msg.getSubject() ); | ||
+ | msgEntity.setBody( msg.getBodyAsText() ); | ||
+ | ses.delete( msgEntity ); | ||
+ | |||
+ | tx.commit(); | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ~~DISQUS~~ | ||
+ | |||