CHAPTER 6 n MAPPING WITH (Make a web site) ANNOTATIONS 105 n
CHAPTER 6 n MAPPING WITH ANNOTATIONS 105 n MAPPING WITH ANNOTATIONS 105 schema: Allows the schema that the table resides within to be specified. table: The name of the table containing the primary key values. uniqueConstraints: Allows additional constraints to be applied to the table for schema generation. valueColumnName: Allows the column containing the primary key generation information for the current entity to be identified. Because the table can be used to contain the primary key values for a variety of entries, it is likely to contain a single row for each of the entities using it. It therefore needs its own primary key (pkColumnName), as well as a column containing the next primary key value to be used (pkColumnValue) for any of the entities obtaining their primary keys from it. Compound Primary Keys with @Id, @IdClass, or @EmbeddedId While the use of single column surrogate keys is advantageous for various reasons, you may sometimes be forced to work with business keys. When these are contained in a single column, you can use @Id without specifying a generation strategy (forcing the user to assign a primary key value before the entity can be persisted). However, when the primary key consists of multiple columns, you need to take a different strategy to group these together in a way that allows the persistence engine to manipulate the key values as a single object. You must create a class to represent this primary key. It will not require a primary key of its own, of course, but it must be a public class, must have a default constructor, must be serializable, and must implement hashCode() and equals()methods to allow the Hibernate code to test for primary key collisions (i.e., they must be implemented with the appropriate database semantics for the primary key values). Your three strategies for using this primary key class once it has been created are as follows: Mark it as @Embeddable and add to your entity class a normal property for it, marked with @Id. Add to your entity class a normal property for it, marked with @EmbeddableId. Add properties to your entity class for all of its fields, mark them with @Id, and mark your entity class with @IdClass, supplying the class of your primary key class. All these techniques require the use of an id class because Hibernate must be supplied with a primary key object when various parts of its persistence API are invoked. For example, you can retrieve an instance of an entity by invoking the Sessionobject s get() method, which takes as its parameter a single serializable object representing the entity s primary key. The use of @Id with a class marked as @Embeddable, as shown in Listing 6-6, is the most natural approach. The @Embeddable tag can be used for non primary key embeddable values anyway (@Embeddable is discussed in more detail later in the chapter). It allows you to treat the compound primary key as a single property, and it permits the reuse of the @Embeddable class in other tables.
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services