Python Rocks! and other rants 7.8.2004
Weblog of Kent S Johnson

2004-08-07

You know you're a geek when...

the book that is keeping you up at night has EJB in the title :-)

I am reading Rod Johnson's new book, J2EE Development without EJB and I have trouble putting it down. He talks about alternative solutions to many of the issues that arise in web application development such as persistence, transaction management and UI layer MVC architecture. He gives many examples of the use of tools such as Hibernate and JDO. Through it all he shows how Spring Framework integrates with other solutions and generally makes your life easier.

Somehow this all keeps me on the edge of my seat. I can't wait to see what alternatives he proposes for transaction management, or how Spring supports AOP. I'm definitely a geek.

This book is a great way to learn about Spring. It is much more practically focused than Johnson's previous book. And by the way, it is half price at bookpool at the moment (as are all Wiley and Wrox titles)!

posted at 14:46:24    #    comment []    trackback []
 

Hibernate

I have been trying out the Hibernate persistence framework for my current work project, Curriculum Builder. So far I am very impressed with it.

Hibernate is a transparent object/relational mapping framework. It makes it very easy to map your objects to a relational database.

"Transparent" means that your business objects need little or no change to work with Hibernate. Persistent objects are POJOs - they don't have to extend a Hibernate base class or implement a Hibernate interface.

The mapping between your objects and the database is defined in an xml configuration file. The mapping is very flexible. For example your business object can have a collection of associated objects that map through a foreign key in the database.

Several features of Hibernate made me think it was worth trying it out for Curriculum Builder.

Hibernate transparently supports the unit-of-work pattern. The way this works is, you access your persistent objects in the scope of a Hibernate Session. Any changes you make to the persistent objects will be detected automatically by Hibernate and persisted when the session is committed.

Hibernate supports optimistic locking with object versioning. Some form of optimistic locking will be essential in CB and I'm very happy not to write it myself!

Hibernate supports lazy loading, so you can control when dependent objects are loaded. For example a Learning Path object might contain a list of courses. With lazy loading, the actual course objects are not loaded unless the collection is accessed.

For example, to create a new Course object and persist it to the database, I use this code. The session has to be told to save the course:

Session s = openSession();
Transaction tx = s.beginTransaction();
Course course = new Course("ABC0101", "Test course", "enUS", "_ss_bs", false);
s.save(course);
tx.commit();
s.close();

To load a course and change it, I do this. Note that I don't have to tell Hibernate that the course has changed, it figures that out by itself:

s = openSession();
tx = s.beginTransaction();
course = findOneCourse(s, "ABC0101");
course.setTitle("Test course new title");
tx.commit();
s.close();

findOneCourse() uses Hibernate's query mechanism:

private Course findOneCourse(Session s, String code) throws HibernateException {
    
Query query = s.createQuery("from Course where code= :code");
    
query.setString("code", code);
    
Course course = (Course)query.uniqueResult();
    
return course;
}

Here is a test case that checks optimistic locking. It creates an object, then modifies it in two concurrent sessions. When the second session commits it gets a StaleObjectStateException:

public void testLocking() throws Exception {
    
Session s = openSession();
    
Transaction tx = s.beginTransaction();
    
Course course = new Course("ABC0103", "Test course 3", "enUS", "_ss_bs", false);
    
s.save(course);
    
tx.commit();
    
s.close();
    
    
Session s1=null, s2=null;
    
try {
        
// Change the course in two different sessions
        
s1 = openSession();
        
Transaction tx1 = s1.beginTransaction();
        
Course course1 = findOneCourse(s1, "ABC0103");
        
assertEquals("Test course 3", course1.getTitle());
        
        
s2 = openSession();
        
Transaction tx2 = s2.beginTransaction();
        
Course course2 = findOneCourse(s2, "ABC0103");
        
assertEquals("Test course 3", course2.getTitle());
        
        
course1.setTitle("Test course three");
        
tx1.commit();
        
s1.close();
        
        
try {
            
course2.setTitle("Conflict!");
            
tx2.commit();
            
s2.close();
            
fail("Should throw StaleObjectStateException");
        
} catch (StaleObjectStateException e) { }
        
    
} finally {
        
if (s1 != null) s1.close();
        
if (s2 != null) s2.close();
    
}
}

For more information, visit the Hibernate web site: http://www.hibernate.org/

Take a look at the road map: http://www.hibernate.org/152.html

Hibernate: A Developers Notebook is a good place to get started.

posted at 14:29:20    #    comment []    trackback []
August 2004
MoTuWeThFrSaSu
       1
2 3 4 5 6 7 8
9101112131415
16171819202122
23242526272829
3031     
Jun
2004
 Sep
2004

Comments about life, the universe and Python, from the imagination of Kent S Johnson.

XML-Image Letterimage

BlogRoll

© 2004, Kent Johnson