Other Hibernate Features


Up to now in this chapter, we have looked at some basic Hibernate features. Next, let's review some additional, slightly more advanced, Hibernate concepts.

Associations

The physical database design, the mapping, and the Java classes for Time Expression are all fairly straightforward. We have essentially used a one-class-per-table mapping strategy to keep the design simple and fast. However, we have utilized a many-to-one association to fetch the corresponding Department record, which we can use to obtain the name of the department. We will need this functionality on various Time Expression screens that display the full Department.name (versus just the Department.departmentCode).

Let's dissect the Java code and XML mapping related to this association.

First, the persistent attribute Java bean code can be found in Department.java file; an excerpt of it, is shown here:

private Department department; public Department getDepartment() {      return department; } public void setDepartment(Department department) {      this.department = department; }


Secondly, the many-to-one mapping can be found in our Timesheet.hbm.xml file:

<many-to-one name="department" column="departmentCode"                            lazy="false" not-found="ignore" cascade="none"              insert="false" update="false"/>


Finally, the code on how we obtain the department name can be found in our TimesheetManagerTest.java file:

System.out.println(">>>> Department name = " + timesheet.getDepartment().getName());


Locking Objects (Concurrency Control)

Database locking can apply to any database applications, not just ones based on ORM technologies. There are two common strategies when dealing with updates to database records, pessimistic locking and optimistic locking.

Optimistic locking is more scalable than pessimistic locking when dealing with a highly concurrent environment. However pessimistic locking is a better solution for situations where the possibility of simultaneous updates to the same data by multiple sources (for example, users) is common, hence making the possibility of "data clobbering," a likely scenario. Let's look at a brief explanation of each of these two locking strategies.

Pessimistic locking is when you want to reserve a record for exclusive update by locking the database record (or entire table). Hibernate supports pessimistic locking (using the underlying database, not in-memory) via one of the following methods:

  • Session.get

  • Session.load

  • Session.lock

  • Session.refresh

  • Query.setLockMode

Although each of the methods accepts different parameters, the one common parameter across all is the LockMode class, which provides various locking modes such as NONE, READ, UPGRADE, UPGRADE_NOWAIT, and WRITE. For example, to obtain a Timesheet record for updating, we could use the following code (assuming the underlying database supports locking):

public Timesheet getTimesheetWithLock(int timesheetId) {     Session session =             HibernateUtil.getSessionFactory().getCurrentSession();     session.beginTransaction();     Timesheet timesheet = (Timesheet)session.get(Timesheet.class,                new Integer(timesheetId), LockMode.UPGRADE);     session.getTransaction().commit();     session.close();0    return timesheet; }


Optimistic locking means that you will not lock a given database record or table and instead check a column/property of some sort (for example, a timestamp column) to ensure the data has not changed since you read it. Hibernate supports this using a version property, which can either be checked manually by the application or automatically by Hibernate for a given session. For example, the following code excerpt is taken verbatim out of the Hibernate reference documentation and shows how an application can manually compare the oldVersion with the current version using a getter method (for example, getVersion):

// foo is an instance loaded by a previous Session session = factory.openSession(); int oldVersion = foo.getVersion(); session.load( foo, foo.getKey() ); if ( oldVersion!=foo.getVersion ) throw new StaleObjectStateException(); foo.setProperty("bar"); session.flush(); session.connection().commit(); session.close();


StaleObjectStateException, shown in the previous example, is an exception in the org.hibernate package.

Lots More Hibernate

Although we covered a lot of material in this chapter, there is much more to Hibernate. However, as I mentioned earlier, entire books exist on Hibernate, and we cannot cover everything about this technology in one chapter. Nevertheless, I have given you enough here to build some reasonably complex applications using Java, Hibernate, and relational databases.

Other Hibernate advanced topics not covered here, but ones you might want to explore, include

  • Advanced mappings (for example, bidirectional associations, ternary associations, sorted collections, component mapping, inheritance mapping, and more)

  • Advanced HQL

  • Annotations (and XDoclet)

  • Filters

  • Hibernate SchemaExport utility

  • Inheritance mapping

  • Interceptors

  • Locking objects

  • Performance improvement strategies (for example, fetching strategies, second-level cache)

  • Scrollable iteration and pagination

  • Transaction management (advanced topics)

  • Other areas such as using stored procedures, native SQL, and more



Agile Java Development with Spring, Hibernate and Eclipse
Agile Java Development with Spring, Hibernate and Eclipse
ISBN: 0672328968
EAN: 2147483647
Year: 2006
Pages: 219

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net