Java Reference
In-Depth Information
LISTING 12-1 (continued)
}
public void setDescription(String description) {
this.description = description;
}
public int getPrice() {
return this.price;
}
public void setPrice(int price) {
this.price = price;
}
public int getRuntimeId() {
return this.runtimeId;
}
public void setRuntimeId(int runtimeId) {
this.runtimeId = runtimeId;
}
}
The class in Listing 12‐1 is a simple POJO with appropriate JPA annotations. As briel y mentioned
earlier, the @Entity class level annotation indicates that this class should be treated as an entity class
and should be managed by the persistence provider. The entity class must have a no‐arg constructor
that has to be public or protected, although it may have other constructors. It must be a top‐level
class, which means that it cannot be an enum or an interface, and it must not be i nal. Also, none of
the persistent instance variables or setter/getter methods of the entity class can be i nal. The entity
class must implement the Serializable interface.
You have annotated the id member with @Id and @GeneratedValue , which marks the id member as
an auto‐generated primary key. All entities must have a primary key, which can be a single member
or a combination of members.
The primary key can be one of the following types:
Primitive Java types— byte , char , short , int , long
Wrapper classes of primitive Java types— Byte , Character , Short , Integer , Long
Arrays of primitive or wrapper types— long[] , Long[] , and so on
Java types— String , BigInteger , Date
All members of the entity class are automatically mapped to i elds of the same name in the movie
table unless they're annotated with @Transient . This means that the id member maps to the id
i eld in the movie table, the title member maps to the title i eld in the movie table, and so on.
Next, in Listing 12‐2, you create the DAO interface. This should dei ne the basic CRUD methods
and any other methods that might prove useful.
Search WWH ::




Custom Search