Java Reference
In-Depth Information
Listing 6-10. An Example of a Field Access Entity Mapped Across Two Tables
package com.hibernatebook.annotations;
import javax.persistence.*;
@Entity
@Table(name="CUSTOMER")
@SecondaryTable(name="CUSTOMER_DETAILS")
public class Customer {
@Id
public int id;
public String name;
@Column(table="CUSTOMER_DETAILS")
public String address;
}
Columns in the primary or secondary tables can be marked as having unique values
within their tables by adding one or more appropriate @UniqueConstraint annotations to
@Table or @SecondaryTable 's uniqueConstraints attribute. For example, to mark the name
field in the preceding declaration as being unique, use the following:
@Entity
@Table(
name="CUSTOMER",
uniqueConstraints={@UniqueConstraint(columnNames="name")}
)
@SecondaryTable(name="CUSTOMER_DETAILS")
public class Customer {
...
}
Persisting Basic Types with @Basic
By default, properties and instance variables in your POJO are persistent—Hibernate will store
their values for you. The simplest mappings are therefore for the “basic” types. These include
primitives, primitive wrappers, arrays of primitives or wrappers, enumerations, and any types
that implement Serializable but are not themselves mapped entities. These are all mapped
implicitly—no annotation is needed. By default, such fields are mapped to a single column,
and eager fetching is used to retrieve them (i.e., when the entity is retrieved from the database,
all the basic fields and properties are retrieved). Also, when the field or property is not a primi-
tive, it can be stored and retrieved as a null value.
This default behavior can be overridden by applying the @Basic annotation to the appro-
priate class member. This annotation takes two optional attributes, and is itself entirely
Search WWH ::




Custom Search