|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 5
As you can see, the XML document isn't difficult to understand. You can easily write and maintain it by hand. Whichever method
you choose, Hibernate has enough information to completely generate all the SQL statements that would be needed to insert,
update, delete, and retrieve instances of the Message class. You no longer need to write these SQL statements by hand.
| Note |
|---|
| Many Java developers have complained of the "metadata hell" that accompanies J2EE development. Some have suggested a movement away from XML metadata back to plain Java code. Although we applaud this suggestion for some problems, ORM represents a case where text-based metadata really is necessary. Hibernate has sensible defaults that minimize typing and a mature document type definition that can be used for auto-completion or validation in editors. You can even automatically generate metadata with various tools. |
Now, let's change our first message and, while we're at it, create a new message associated with the first, as shown in Listing 3.
Listing 3. Updating a message
Session session = getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
// 1 is the generated id of the first message
Message message =
(Message) session.load( Message.class, new Long(1) );
message.setText("Greetings Earthling");
Message nextMessage = new Message("Take me to your leader (please)");
message.setNextMessage( nextMessage );
tx.commit();
session.close();
This code calls three SQL statements inside the same transaction:
select m.MESSAGE_ID, m.MESSAGE_TEXT, m.NEXT_MESSAGE_ID from MESSAGES m where m.MESSAGE_ID = 1 insert into MESSAGES (MESSAGE_ID, MESSAGE_TEXT, NEXT_MESSAGE_ID) values (2, 'Take me to your leader (please)', null) update MESSAGES set MESSAGE_TEXT = 'Greetings Earthling', NEXT_MESSAGE_ID = 2 where MESSAGE_ID = 1
Notice how Hibernate detected the modification to the text and nextMessage properties of the first message and automatically updated the database. We've taken advantage of a Hibernate feature called
automatic dirty checking: this feature saves us the effort of explicitly asking Hibernate to update the database when we modify the state of an object
inside a transaction. Similarly, you can see that the new message was made persistent when a reference was created from the
first message. This feature is called cascading save: it saves us the effort of explicitly making the new object persistent by calling save(), as long as it's reachable by an already persistent instance. Also notice that the ordering of the SQL statements isn't the
same as the order in which we set property values. Hibernate uses a sophisticated algorithm to determine an efficient ordering
that avoids database foreign key constraint violations but is still sufficiently predictable to the user. This feature is
called transactional write-behind.
If we run "Hello World" again, it prints:
2 message(s) found: Greetings Earthling Take me to your leader (please)
This is as far as we'll take the "Hello World" application. Now that we finally have some code under our belt, we'll take a step back and present an overview of Hibernate's main APIs.
Archived Discussions (Read only)