Archive for April, 2007

Geocities web hosting - 128 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER

Monday, April 30th, 2007

128 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER 6 n MAPPING WITH ANNOTATIONS Table 6-2. Continued Attribute Name Target Purpose NamedNativeQuery/ NamedNativeQueries Pk and T Extends the corresponding EJB 3 named native query functionality with various Hibernate-specific query hints. NamedQuery/NamedQueries Pk and T Extends the corresponding EJB 3 named query functionality with various Hibernate-specific query hints. NotFound M and F Allows the behavior to be defined for circumstances in which an expected entity is missing. The options drawn from the NotFoundAction enumeration are the self-explanatory EXCEPTION and IGNORE values. The default is EXCEPTION. OnDelete T, M, and F Allows Hibernate-specific behavior on deletion of collections, arrays, and joined subclasses. OrderBy M and F Allows a collection to be ordered by SQL rather than HQL (as with the EJB 3 annotation) ordering. ParamDef Pm Used to define parameters for Filter annotations. Parameter Pm Used to declare parameters for GenericGenerator annotations. Proxy T Allows the proxy behavior for an entity to be configured or disabled. Sort M and F Allows a collection to be sorted using a comparator. Table/Tables T Allows indexes to be applied to a table (see the Applying Indexes with @Tableand @Index section later in the chapter). Type M and F Marks a field or an attribute as being a composite user type. TypeDef/TypeDefs Pk and T Allows a composite user type to be defined. Where T, M, and F Applies a Where clause to an entity or association. Key to the Target column: Pk = package, T = type, M = method, F = field, Pm = parameter All the annotations and enumerations described here fall into the org.hibernate. annotations package. When we refer to an EJB 3 annotation or enumeration, we will use the fully qualified javax.persistence.* class name. @Entity The Hibernate-specific @Entity annotation extends the basic details of the @javax. persistence.Entity annotation, but is otherwise used in the same contexts. It allows the following additional attributes to be specified: dynamicInsert is used to flag that insert statements should be generated at run time (not at startup), allowing only the altered columns to be inserted. By default this is disabled. dynamicUpdate is used to flag that update statements should be generated at run time, allowing only the altered columns to be updated. By default this is disabled.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

CHAPTER 6 n MAPPING WITH ANNOTATIONS 127 (Web server type) n

Monday, April 30th, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS 127 n MAPPING WITH ANNOTATIONS 127 nTip It is possible to overstate the importance of portability most bespoke applications are never deployed to an environment other than the one for which they were originally developed. As a mature product, Hibernate 3 has numerous features to offer above and beyond the base EJB 3 specification. You should not waste too much time trying to achieve a portable solution in preference to these proprietary features unless you have a definite requirement for portability. Table 6-2. The Hibernate Annotations Attribute Name Target Purpose AccessType T, M, and F Allows the default access type (normally determined by the placement of the @javax. persistence.Id annotation) for the annotated object to be overridden. BatchSize T, M, and F Allows the batch size for a query to be specified. Cache T, M, and F Allows a cache concurrency strategy (NONE, NONSTRICT_READ_WRITE, READ_ONLY, READ_WRITE, or TRANSACTIONAL) to be selected. Cascade M and F Applies a Hibernate-specific cascade strategy to an association. Check T, M, and F Allows an arbitrary SQL constraint to be specified during the schema generation. CollectionOfElements M and F Allows a collection field or an attribute to be marked as a collection of elements or embedded objects, rather than a full-fledged association with an entity. Columns M and F Allows an array of @javax.persistence.Column annotations to be applied to an annotated Hibernate composite user type. DiscriminatorFormula T Allows the discriminator type to be determined with an HQL formula instead of the default EJB 3 mechanisms. Entity T Allows Hibernate-specific attributes to be applied in addition to the javax.persistence.Entity annotation information. Filter/Filters T, M, and F Adds named filters to an entity or a collection. FilterDef/FilterDefs Pk and T Allows named filters to be declared. Formula MF Allows an SQL formula to be used in place of values drawn from a column. GenericGenerator Pk, T, M, and F Allows a Hibernate-specific generator to be used when creating a primary key value for the entity. Index M and F Allows a database index to be defined for a column or columns. IndexColumn M and F Allows a collection to maintain an order on the basis of an index column maintaining the element ordering (i.e., collection ordering rather than database ordering). Continued
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

126 CHAPTER 6 n MAPPING WITH ANNOTATIONS (Web design rates) CHAPTER

Sunday, April 29th, 2007

126 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER 6 n MAPPING WITH ANNOTATIONS
jdbc:hsqldb:file:annotationsdb;shutdown=true sa 0 false org.hibernate.dialect.HSQLDialect You can also add an annotated class to your Hibernate configuration programmatically. The annotations toolset comes with an org.hibernate.cfg.AnnotationConfiguration object that extends the base Hibernate Configuration object for adding mappings. The methods on AnnotationConfiguration for adding annotated classes to the configuration are as follows: addAnnotatedClass(Class persistentClass) throws MappingException addAnnotatedClasses(List classes) addPackage(String packageName) throws MappingException Using these methods, you can add one annotated class, a list of annotated classes, or an entire package (by name) of annotated classes. As with the Hibernate XML configuration file, the annotated entities are interoperable with XML mapped entities. Hibernate 3 Specific Persistence Annotations Table 6-2 lists the Hibernate-specific annotations. We will now look at some of these Hibernate- specific annotations in more detail however, bear in mind that using any of this functionality potentially reduces the portability of your application to other EJB 3 solutions. Annotations that are not recognized by an EJB 3 environment will be ignored, rather than causing a runtime exception directly however, this may result in different runtime application behavior that may not be desirable. In some cases, Hibernate 3 annotations can be used to prepare resources that are referenced by standard EJB 3 annotations, in which case the application will fail when the EJB 3 environment attempts to use the missing resource.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

CHAPTER 6 n (Email web hosting) MAPPING WITH ANNOTATIONS 125 n

Sunday, April 29th, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS 125 n MAPPING WITH ANNOTATIONS 125 If you have multiple @NamedQuery annotations to apply to an entity, they can be provided as an array of values of the @NamedQueries annotation. Named Native Queries (SQL) EJB 3 also allows the database s native query language (usually a dialect of SQL) to be used in place of EJB QL. You risk losing portability here if you use a database-specific feature, but as long as you use reasonably generic SQL, you should be OK. The @NamedNativeQuery annotation is declared in almost exactly the same manner as the @NamedQuery annotation. The following block of code shows a simple example of the use of a named native query. @NamedNativeQuery( name=”nativeFindAuthorNames”, query=”select name from author” ) Multiple @NamedNativeQuery annotations can be grouped with the @NamedNativeQueries annotation. nNote Hibernate does not currently fully support named native queries. Configuring the Annotated Classes Once you have an annotated class, you will need to provide the class to your application s Hibernate configuration, just as if it were an XML mapping. With annotations, you can use either the declarative configuration in the hibernate.cfg.xml XML configuration document, or you can programmatically add annotated classes to Hibernate s org.hibernate.cfg. AnnotationConfiguration object. Your application may use both annotated entities and XML mapped entities in the same configuration. To provide declarative mapping, we use a normal hibernate.cfg.xml XML configuration file and add the annotated classes to the mapping using the mappingelement (see Listing 6-32). Notice that we have specified the name of the annotated class as a mapping. Listing 6-32. A Hibernate XML Configuration File with an Annotated Class
org.hsqldb.jdbcDriver Note: If you are looking for cheap webhost to host and run your apache application check Vision apache web hosting services

124 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER (Web hosting providers)

Saturday, April 28th, 2007

124 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER 6 n MAPPING WITH ANNOTATIONS Named Queries (HQL or EJB QL) @NamedQuery and @NamedQueriesallow one or more EJB QL queries to be associated with an entity. The required attributes are as follows: name is the name by which the query is retrieved. query is the EJB QL (or HQL) query associated with the name. Listing 6-29 shows an example associating a named query with the Author entity. The query would retrieve Author entities by name, so it is natural to associate it with that entity however, there is no actual requirement that a named query be associated in this way with the entity that it concerns. Listing 6-29. An EJB QL Named Query Annotation @Entity @NamedQuery( name=”findAuthorsByName”, query=”from Author where name = :author” ) public class Author { … } There is also a hints attribute, taking a QueryHint annotation name/value pair, which allows caching mode, timeout value, and a variety of other platform-specific tweaks to be applied (this can also be used to comment the SQL generated by the query). You do not need to directly associate the query with the entity against which it is declared, but it is normal to do so. If a query has no natural association with any of the entity declarations, it is possible to make the @NamedQueryannotation at the package level. There is no natural place to put a package-level annotation, so Java annotations allow for a specific file, called package-info.java, to contain them. Listing 6-30 gives an example of this. Listing 6-30. A package-info.java File @javax.annotations.NamedQuery( name=”findAuthorsByName”, query=”from Author where name = :author” ) package com.hibernatebook.annotations; Hibernate s session allows named queries to be accessed directly, as shown in Listing 6-31. Listing 6-31. Invoking a Named Query via the Session Query query = session.getNamedQuery(”findAuthorsByName”); query.setParameter(”author”, “Dave”); List booksByDave = query.list(); System.out.println(”There is/are ” + booksByDave.size() + ” author(s) called Dave in the catalog”);
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

Web server hosting - CHAPTER 6 n MAPPING WITH ANNOTATIONS 123 n

Saturday, April 28th, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS 123 n MAPPING WITH ANNOTATIONS 123 Large Objects A persistent property or field can be marked for persistence as a database-supported large object type by applying the @Lob annotation. The annotation takes no attributes, but the underlying large object type to be used will be inferred from the type of the field or parameter. String- and character-based types will be stored in an appropriate character-based type. All other objects will be stored in a BLOB. Listing 6-27 maps a String into a large object column type. Listing 6-27. An Example of a Large Object Property @Lob public String getTitle() { return this.title; } The @Lob annotation can be used in combination with the @Basic annotation. Mapped Superclasses A special case of inheritance occurs when the root of the hierarchy is not itself a persistent entity, but various classes derived from it are. Such a class can be abstract or concrete. The @MappedSuperclass annotation allows you to take advantage of this circumstance. The class marked with @MappedSuperclass is not an entity, and is not queryable (it cannot be passed to methods that expect an entity in the Session or EntityManager objects). It cannot be the target of an association. The mapping information for the columns of the superclass will be stored in the same table as the details of the derived class (in this way, the annotation resembles the use of the an @Inheritance tag with the SINGLE_TABLE strategy). In other respects, the superclass can be mapped as a normal entity, but the mappings will apply to the derived classes only (since the superclass itself does not have an associated table in the database). When a derived class needs to deviate from the superclass s behavior, the @AttributeOverride annotation can be used (much as with the use of an embeddable entity). For example, if in our example model Bookwas a superclass of ComputerBook, but Book objects themselves were never persisted directly, then Book could be marked as @MappedSuperclass, as in Listing 6-28. Listing 6-28. Marking the Book Class As a Mapped Superclass @Entity @MappedSuperclass public class Book { … } The fields of the ComputerBook entity derived from Book would then be stored in the ComputerBook entity class s table. Classes derived directly from Book but not mapped as entities in their own right, such as a hypothetical MarketingBook class, would not be persistable. In this respect alone, the mapped superclass approach behaves differently from the conventional @Inheritance approach with a SINGLE_TABLE strategy.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision christian web host services

122 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER (Web file server)

Friday, April 27th, 2007

122 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER 6 n MAPPING WITH ANNOTATIONS Listing 6-24. The Root of the Inheritance Hierarchy Mapped with the JOINED Strategy @Entity @Inheritance(strategy = JOINED) @DiscriminatorColumn( name=”DISCRIMINATOR” ) public class Book { … } Finally, there is the table-per-class approach, in which all of the fields of each type in the inheritance hierarchy are stored in distinct tables. Because of the close correspondence between the entity and its table, the @DiscriminatorColumn annotation is not applicable to this inheritance strategy. Listing 6-25 shows how our Book class could be mapped in this way. Listing 6-25. The Root of the Inheritance Hierarchy Mapped with the TABLE_PER_CLASSStrategy @Entity @Inheritance(strategy = TABLE_PER_CLASS) public class Book { … } Other EJB 3 Persistence Annotations Although we have now covered most of the core EJB 3 persistence annotations, there are a few others that you will encounter fairly frequently. We cover some of these in passing in the following sections. Temporal Data Fields or properties of an entity that have java.util.Date or java.util.Calendar types represent temporal data. By default, these will be stored in a column with the TIMESTAMP data type, but this default behavior can be overridden with the @Temporalannotation. The annotation accepts a single value attribute from the javax.persistence. TemporalType enumeration. This offers three possible values: DATE, TIME, and TIMESTAMP. These correspond respectively to java.sql.Date, java.sql.Time, and java.sql.Timestamp. The table column is given the appropriate data type at schema generation time. Listing 6-26 shows an example mapping a java.util.Date property as a TIME type the java.sql.Date and java.sql.Time classes are both derived from the java.util.Date class, so confusingly, both are capable of representing dates and times! Listing 6-26. A Date Property Mapped as a Time Temporal Field @Temporal(TIME) public java.util.Date getStartingTime() { return this.startingTime; }
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services

CHAPTER 6 n MAPPING WITH ANNOTATIONS 121 (Best web design) n

Friday, April 27th, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS 121 n MAPPING WITH ANNOTATIONS 121 name is the name of the discriminator column. discriminatorType is the type of value to be stored in the column as selected from the javax.persistence.DiscriminatorType enumeration of STRING, CHAR, or INTEGER. columnDefinition is a fragment of DDL defining the column type. Using this is liable to reduce the portability of your code across databases. length is the column length of STRING discriminator types. It is ignored for CHAR and INTEGER types. All of these (and the annotation itself) are optional, but we recommend supplying at least the name attribute. If no @DiscriminatorColumn is specified in the hierarchy, a default column name of DTYPE and type of STRING will be used. Hibernate will supply an appropriate discriminator value for each of your entities. For example, if the STRING discriminator type is used, the value this column contains will be the name of the entity (which defaults to the class name). You can also override this behavior with specific values using the @DiscriminatorValue annotation. If the discriminator type is INTEGER, any value provided via the @DiscriminatorValue annotation must be convertible directly into an integer. In Listing 6-22, we specify that an INTEGER discriminator type should be stored in the column named DISCRIMINATOR. Rows representing Book entities will have a value of 1 in this column, whereas the following mapping in Listing 6-23 requires that rows representing ComputerBook entities should have a value of 2 in the same column. Listing 6-22. The Root of the Inheritance Hierarchy Mapped with the SINGLE_TABLE Strategy @Entity @Inheritance(strategy = SINGLE_TABLE) @DiscriminatorColumn( name=”DISCRIMINATOR”, discriminatorType=INTEGER ) @DiscriminatorValue(”1″) public class Book { … } Listing 6-23. A Derived Entity in the Inheritance Hierarchy @Entity @DiscriminatorValue(”2″) public class ComputerBook extends Book { … } An alternative to the monolithic single table approach is the otherwise similar joined table approach. Here a discriminator column is used, but the fields of the various derived types are stored in distinct tables. Other than the differing strategy, this inheritance type is specified in the same way (as shown in Listing 6-24).
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

120 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER (Web file server)

Thursday, April 26th, 2007

120 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER 6 n MAPPING WITH ANNOTATIONS Listing 6-20. The Author Side of the Many-to-Many Association @ManyToMany(mappedBy = “authors”) public Set getBooks() { return books; } Alternatively, we could specify the link table in full, as in Listing 6-21. Listing 6-21. Specifying the Link Table in Full Using the Book Entity Annotations @ManyToMany(cascade = ALL) @JoinTable( name=”Books_to_Author”, joinColumns={@JoinColumn(name=”book_ident”)}, inverseJoinColumns={@JoinColumn(name=”author_ident”)} ) public Set getAuthors() { return authors; } Inheritance The EJB 3 standard and Hibernate both support three approaches to mapping inheritance hierarchies into the database. These are as follows: Single table (SINGLE_TABLE) Joined (JOINED) Table-per-class (TABLE_PER_CLASS) Persistent entities that are related by inheritance must be marked up with the @Inheritance annotation. This takes a single strategy attribute, which is set to one of three javax.persistence.InheritanceType enumeration values corresponding to these approaches (shown in brackets in the preceding bulleted list). The single table approach manages one class for the superclass and all its subtypes. There are columns for each mapped field or property of the superclass, and for each distinct field or property of the derived types. When following this strategy, you will need to ensure that columns are appropriately renamed when any field or property names collide in the hierarchy. To determine the appropriate type to instantiate when retrieving entities from the database, an @DiscriminatorColumn annotation should be provided in the root (and only in the root) of the persistent hierarchy.3 This defines a column containing a value that distinguishes between each of the types used. The attributes permitted by the @DiscriminatorColumn annotation are as follows: 3. That is to say, the highest class in the hierarchy that is mapped to the database as an entity should be annotated in this way.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

CHAPTER 6 n MAPPING WITH ANNOTATIONS 119 n (Web server on xp)

Thursday, April 26th, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS 119 n MAPPING WITH ANNOTATIONS 119 Listing 6-18 shows a fairly typical application of the @JoinTable annotation to specify the name of the join table and its foreign keys into the associated entities. Listing 6-18. A Unidirectional One-to-Many Association with a More Fully Specified Join Table @OneToMany(cascade = ALL) @JoinTable( name=”PublishedBooks”, joinColumns = { @JoinColumn( name = “publisher_id”) }, inverseJoinColumns = @JoinColumn( name = “book_id”) ) public Set getBooks() { return books; } Mapping a Many-to-Many Association When a many-to-many association does not involve a first-class entity joining the two sides of the relationship, a link table must be used to maintain the relationship. This can be generated automatically, or the details can be established in much the same way as with the link table described in the Mapping a Many-to-One or One-to-Many Association section of the chapter. The appropriate annotation is naturally @ManyToMany, and takes the following attributes: mappedBy is the field that owns the relationship this is only required if the association is bidirectional. If an entity provides this attribute, then the other end of the association is the owner of the association, and the attribute must name a field or property of that entity. targetEntity is the entity class that is the target of the association. Again, this may be inferred from the generic or array declaration, and only needs to be specified if this is not possible. cascade indicates the cascade behavior of the association, which defaults to none. fetch indicates the fetch behavior of the association, which defaults to LAZY. The example maintains a many-to-many association between the Book class and the Author class. The Book entity owns the association, so its getAuthors() method must be marked with an appropriate @ManyToManyattribute, as shown in Listing 6-19. Listing 6-19. The Book Side of the Many-to-Many Association @ManyToMany(cascade = ALL) public Set getAuthors() { return authors; } The Author entity is managed by the Book entity. The link table is not explicitly managed, so, as shown in Listing 6-20, we mark it with a @ManyToMany annotation and indicate that the foreign key is managed by the authors attribute of the associated Book entity.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services