Java Reference
In-Depth Information
nullable permits the column to be marked NOT NULL when the schema is generated. The
default is that fields should be permitted to be null; however, it is common to override
this when a field is, or ought to be, mandatory.
unique permits the column to be marked as containing only unique values. This defaults
to false , but commonly would be set for a value that might not be a primary key but
would still cause problems if duplicated (such as username ).
We have marked up the title field of our Book entity using the @Column entity to show how
three of these attributes would be applied:
@Column(name="working_title",length=200,nullable=false)
public String getTitle() {
return title;
}
The remaining attributes, less commonly used, are as follows:
table is used when the owning entity has been mapped across one or more secondary
tables. By default, the value is assumed to be drawn from the primary table, but the name
of one of the secondary tables can be substituted here (see the @SecondaryTable annota-
tion example earlier in this chapter).
insertable defaults to true , but if set to false , the annotated field will be omitted from
insert statements generated by Hibernate (i.e., it won't be persisted).
updatable defaults to true , but if set to false , the annotated field will be omitted from
update statements generated by Hibernate (i.e., it won't be altered once it has been
persisted).
columnDefinition can be set to an appropriate DDL fragment to be used when generating
the column in the database. This can only be used during schema generation from the
annotated entity, and should be avoided if possible, since it is likely to reduce the porta-
bility of your application between database dialects.
precision permits the precision of decimal numeric columns to be specified for schema
generation, and will be ignored when a non-decimal value is persisted. The value given
represents the number of digits in the number (usually requiring a minimum length of
n+1, where n is the scale).
scale permits the scale of decimal numeric columns to be specified for schema genera-
tion and will be ignored where a non-decimal value is persisted. The value given
represents the number of places after the decimal point.
Modeling Entity Relationships
Naturally, annotations also allow you to model associations between entities. EJB 3 supports
one-to-one, one-to-many, many-to-one, and many-to-many associations. Each of these has
its corresponding annotation.
We discussed the various ways in which these mappings can be established in the tables
in Chapter 5. In this section, we will show how the various mappings are requested using the
annotations.
Search WWH ::




Custom Search