
Here's a snap of my current firefox extensions. Not too few, not too many, but just enough...
This is not, unfortunately, a blog dedicated to that great twelve volume series of novels by Anthony Powell. However, it is a form of record of my journey through the world of information technology; a journey motivated by both profit (it is intimately related to my profession) and inquisitiveness (I love to know how things work). It is my intention, therefore to record. These records are primarily for my benefit, but may be useful to others as a side effect.
If it looks simple, it's supposed to. Lets create the classes.
Creating the Movie Entity Class
This will represent the table “MOVIE” in the database. We first need to generate a simple, serializable java POJO class with the attributes shown above encapsulated as appropriate (you can do this automatically with your chosen GUI). This now needs to be annotated to let the compiler know how to treat it. (Note: This requires JDK annotation support which you'll get if you use Java 5)
...
public class Movie implents Serializable {
private Long id;
private String title;
private int runningTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
...
First up we need to let on this is an Entity Class. We do this simply by using the “@Entity” annotation before the “public class Movie implements Serializable” line. The compiler is now clever enough to know that, unless told otherwise, the attributes on this class will be represented as columns in our table. That's how EJB 3.0 works. It assumes the default, unless you tell it different. Very handy.
...
import javax.persistence.Entity;
...
@Entity
public class Movie implents Serializable {
...
The observant amongst you will realise that we most likely need a primary key. Correct, each entity class must have a primary key. When you create the entity class, we must add an @Id annotation to the “id” attribute to declare we plan to use it as the primary key. We also add the @Generated... annotation to specify the key generation strategy for the primary Id. In our case:
...
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
...
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
...
Finally for Movie, there is the interesting case of the relationship with the Actor Entity class. Firstly we need to add the collection attribute, “Collection
import javax.persistence.OneToMany;
...
@OneToMany(mappedBy="movie")
private Collectionactors;
...
And that's the Movie Entity class coded. We now need to do the same for the Actors. As before, create a POJO class (ensuring you “implement Serializable”) with the required attributes encapsulated. This will represent the "ACTOR" table in the database. Again define it as an Entity with the @Entity annotation and mark the “id” attribute as the primary key and set it to
be auto generated with the @Id and @Generated tags.
Again we need to add the new attribute. in this case it's the Actior nowing about the film he has appeared in:
private Movie movie;
Again, encapsulate this attribute. Finally we need to add the annotation to let on that this is the reverse direction of the previous relationship. We do this by putting the following annotation above the new movie declaration:
import javax.persistence.ManyToOne;
...
@ManyToOne
private Movie movie;
Note: You'll have noticed that you require imports for these annotations. These are contained in the “javaee.jar” package which I needed to declare as a dependency in my maven project.xml file. I found the one I used in my GlassFish server's /lib directory. If you're doing it differently maybe your IDE has suggested one for you.
// Start the Derby Database
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver")
.newInstance();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
// Set the connection to the database
Connection conn = null;
try {
conn = DriverManager
.getConnection("jdbc:derby:sample;create=true");
} catch (SQLException ex) {
ex.printStackTrace();
}