Thursday, October 29, 2009

Section 7.3. An Alternate Approach








7.3. An Alternate
Approach


This exercise has shown that annotations are clearly a viable way
of mapping model classes. By jumping through a few hoops we
were able to maintain the exact schema we'd evolved in the preceding
chapters, though we lost the ability to cascade creation of
Tracks during the creation of an
Album. There's another approach we could have taken
in the schema which would maintain that automatic cascade, and give us
some other abilities, as well, if we think about the
AlbumTrack class slightly differently.


Mapping AlbumTrack as a full-blown entity
gives us places to put cascade annotations that Hibernate will honor all
the way from the Album definition to the embedded
Track reference. It also gives us a few new
complications to think about, but some of those can be seen as
opportunities. First of all, AlbumTrack as an
entity will need an ID. And since we will then be able to get our hands on
AlbumTrack objects without starting from an
Album, the AlbumTrack model
ought to be enhanced to expose the link back to the ALBUM table from the ALBUM_TRACKS table (which used to be hidden in
Hibernate's composite key). We'd achieve that by adding an album property. Example 7-12 shows
the key parts of the AlbumTrack mapping as they
would differ in this approach.


Example 7-12. Annotating the AlbumTrack class as an entity



package com.oreilly.hh.data;

import java.io.Serializable;
import javax.persistence.*;
@Entity

@Table(name="ALBUM_TRACKS")

public class AlbumTrack {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;


@ManyToOne
@JoinColumn(name="ALBUM_ID", insertable=false, updatable=false,

nullable=false)
private Album album;


@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="TRACK_ID", nullable=false)
private Track track;
...

public Integer getId() {
return id;
}

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

public Album getAlbum() {
return album;
}


public Track getTrack() {
...
}






There would be a small difference in the way the relationship is
mapped in Album.java, as well, of course. This is
shown in Example 7-13.



Example 7-13. The AlbumTracks entity mapping in Album.java



...
@OneToMany(cascade=CascadeType.ALL)
@IndexColumn(name="LIST_POS")
@JoinColumn(name="ALBUM_ID", nullable=false)
private List<AlbumTrack> tracks;
...




The cascade setting in the
@OneToMany annotation sets up the
same unbroken cascade from Album through
to its embedded Track references we had in the
final version of Album.hbm.xml,
developed in Example 5-13, in which albums manage their tracks' life
cycles. This lets us comment out the track-saving line in the
addAlbumTrack⁠⁠(⁠ ⁠) method of
AlbumTest again. So we've recreated the old
functionality with a different schema. If we were willing to go to the
trouble of creating a class to manage a composite key, we could keep both
the functionality and database schema intact.


As with many other parts of the Hibernate API
(and with object-oriented modeling in general), there are a great many
ways to reach your goals.



7.3.1. What now?


Hopefully this chapter has given you a feel for how to use
annotations to express data mappings, and will serve as a good starting
point when you want to explore that option in your own projects. As with
the rest of the book, we've not tried to list all the details and
features available—that's what the reference documentation is for,
although it can seem a bit sparse at times, so hopefully some of the
issues we sorted out in this discussion will also serve as examples of
how to resolve ambiguity. If all else fails, just keep trying variations
and pasting error messages into Google! Or, if you're a good citizen,
delve into the source code and post questions on the Hibernate forum, to
lay down a trail of crumbs for future users, and help highlight where
the documentation could stand shoring up.


We'll be going back to the XML-based world for
the next few chapters as we explore more ways to query for data. But
keep the annotations concepts at the back of your mind—they'll be put to
good use again in the Spring and Stripes chapters at the end of the
book.










No comments:

Post a Comment