Archive for May, 2007

134 CHAPTER 6 n (Managed web hosting) MAPPING WITH ANNOTATIONS CHAPTER

Thursday, May 3rd, 2007

134 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER 6 n MAPPING WITH ANNOTATIONS public void setId(int id) { this.id = id; } public void setTitle(String title) { this.title = title; } public void setPages(int pages) { this.pages = pages; } public void setPublicationDate(Date publicationDate) { this.publicationDate = publicationDate; } public void setPublisher(Publisher publisher) { this.publisher = publisher; } public void setAuthors(Set authors) { this.authors = authors; } // Helpers… public void addAuthor(Author author) { authors.add(author); } } Listing 6-35 demonstrates the use of the @NamedQuery, @Embedded, @AttributeOverrides, and @AttributeOverride annotations. Listing 6-35. The Fully Annotated Author Class package com.hibernatebook.annotations; import java.util.HashSet; import java.util.Set; import javax.persistence.*; @Entity @NamedQuery( name=”findAuthorsByName”, query=”from Author where name = :author” ) public class Author {
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

CHAPTER 6 n MAPPING WITH ANNOTATIONS 133 n (Web server version)

Thursday, May 3rd, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS 133 n MAPPING WITH ANNOTATIONS 133 // Constructors… protected Book() { } public Book(String title, int pages) { this.title = title; this.pages = pages; } // Getters… @Id @GeneratedValue public int getId() { return id; } @Column(name = “working_title”, length = 200, nullable = false) public String getTitle() { return title; } public int getPages() { return pages; } @Transient public Date getPublicationDate() { return publicationDate; } @ManyToOne @JoinColumn(name = “publisher_id”) public Publisher getPublisher() { return publisher; } @ManyToMany(cascade = ALL) public Set getAuthors() { return authors; } // Setters…
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

132 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER (Web site construction)

Wednesday, May 2nd, 2007

132 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER 6 n MAPPING WITH ANNOTATIONS Listing 6-33. A Sample Excerpt from this Chapter s Task to Perform Schema Generation A full Ant script is provided with the online source code for this chapter (at www.apress.com). Code Listings Listings 6-34 and 6-35 contain the completed annotated source code for the Author and Book classes described in this chapter. The database schema also follows in Listing 6-36. Listing 6-34 illustrates use of the @Entity, @Inheritance, @Id, @GeneratedValue, @Column, @Transient, @ManyToOne, @JoinColumn, and @ManyToMany annotations. Listing 6-34. The Fully Annotated Book Class package com.hibernatebook.annotations; import static javax.persistence.CascadeType.ALL; import static javax.persistence.InheritanceType.JOINED; import java.util.*; import javax.persistence.*; @Entity @Inheritance(strategy = JOINED) public class Book { private String title; private Publisher publisher; private Set authors = new HashSet(); private int pages; private int id; protected Date publicationDate;
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

Shared web hosting - CHAPTER 6 n MAPPING WITH ANNOTATIONS 131 n

Wednesday, May 2nd, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS 131 n MAPPING WITH ANNOTATIONS 131 The available standard Hibernate strategies are increment, identity, sequence, hilo, seqhilo, uuid, guid, native, assigned, select, and foreign. For example, the non-standard uuid strategy for a primary key is configured as follows: @Id @GenericGenerator(name=”unique_id”,strategy=”uuid”) @GeneratedValue(generator=”unique_id”) public String getId() { return this.id; } Alternatively, to configure the sequence strategy (equivalent to specifying a strategy of SEQUENCE in the @GeneratedValueannotation), you can supply the following parameters: @Id @GenericGenerator(name=”seq_id”,strategy=”sequence”, parameters= { @Parameter(name=”sequence”,value=”HIB_SEQ”) } ) @GeneratedValue(generator=”seq_id”) public Integer getId() { return this.id; } Using Ant with Annotation-Based Mappings When using the Hibernate Ant tasks in conjunction with the annotation-based mappings, you operate under one important constraint: the Ant task cannot read the mapping information from the raw source files. The annotated files must be compiled before you can perform any operations on them (including schema generation). You should therefore ensure that any Hibernate Ant tasks are granted a dependency upon the compile task for the entities. The Ant task will also need access to the classes via the configuration object you will therefore need to explicitly include any annotated classes in the hibernate.cfg.xmlfile as described in the first part of the previous Configuring the Annotated Classes section. You cannot use programmatic configuration of the classes in conjunction with tasks such as hbm2ddl, so this is an important step. The various Hibernate JAR files, including hibernate-annotations.jar, will need to be in the classpath of the task definition. Finally, you will need to specify an element, rather than the usual element. An example Ant target to build a DDL script from annotated classes is shown in Listing 6-33.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

Make my own web site - 130 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER

Tuesday, May 1st, 2007

130 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER 6 n MAPPING WITH ANNOTATIONS from the annotations, the column is assumed to be an integertype, but this can be overridden by supplying a columnDefinitionattribute specifying a different column definition string. Applying Indexes with @Table and @Index The Hibernate-specific @Table annotation supplements the standard table annotation and allows additional index hints to be provided to Hibernate. These will be used at schema generation time to apply indexes to the columns specified. The following code gives an example. // Standard persistence annotations: @javax.persistence.Entity @javax.persistence.Table(name=”FOO”) // Hibernate-specific table annotation: @Table( appliesTo=”FOO”, indexes = { @Index(name=”FOO_FROM_TO_IDX”,columnNames={”FIRST”,”LAST”}), @Index(name=”FOO_EMPLOYEE_IDX”,columnNames={”EMPLOYEE_NUM”})) public class Foo { … } Restricting Collections with @Where The contents of a collection that will be retrieved from the database can be restricted with a Hibernate-specific @Where annotation. This simply adds a Where clause to the query that will be used to obtain the entities contained within the collection. Here s an example: @javax.persistence.OneToMany @org.hibernate.annotations.Where(clause=”grade > 2″) public Set getEmployees() { return this.employees; } Alternative Key Generation Strategies with @GenericGenerator As mentioned in the Primary Keys with @Id and @GeneratedValue section, the full gamut of Hibernate primary key value generators is not supported by the standard set of annotations. Hibernate therefore supplies the @GenericGenerator annotation to fill the void. The attributes that can be supplied to the annotation are as follows: name is mandatory, and is used to identify the generic generator in the @GeneratedValue annotation. strategy is mandatory, and determines the generator type to be used. This can be a standard Hibernate generator type or the name of a class implementing the org.hibernate. id.IdentifierGenerator interface. parameters is a list of @Parameter annotations defining any parameter values required by the generator strategy.
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

CHAPTER 6 (How to cite a web site) n MAPPING WITH ANNOTATIONS 129 n

Tuesday, May 1st, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS 129 n MAPPING WITH ANNOTATIONS 129 mutable is true by default, but if set to false, it allows the persistence engine to cache the values read from the database, and the persistence engine will make no attempt to update them in response to changes (changes that should not be made if this flag is set to false). optimisticLock allows an optimistic lock strategy to be selected from the OptimisticLockType enumeration values of ALL, DIRTY, NONE, and VERSION. This defaults to VERSION. persister allows a persister class other than the default Hibernate one to be selected for the entity (for example, allowing serialization to be used instead of relational persistence). polymorphism allows the polymorphism strategy to be selected from the PolymorphismType enumeration values of EXPLICIT and IMPLICIT. This defaults to IMPLICIT. selectBeforeUpdate allows the user to request that a SELECT be performed to retrieve the entity before any potential update. Sorting Collections with @Sort The Hibernate-specific @Sort annotation allows a collection managed by Hibernate to be sorted by a standard Java comparator. The following code gives an example. @javac.persistence.OneToMany @org.hibernate.annotations.Sort( type=org.hibernate.annotations.SortType.COMPARATOR, comparator=EmployeeComparator.class ) public Set getEmployees() { return this.employees; } Ordering Collections with @IndexColumn While @Sort allows data to be sorted once it has been retrieved from the database, Hibernate also provides a non-standard persistence feature that allows the ordering of appropriate collection types such as List to be maintained within the database by maintaining an index column to represent that order. Here s an example: @javax.persistence.OneToMany @org.hibernate.annotations.IndexColumn( name=”employeeNumber” ) public List getEmployees() { return this.employees; } Here, we are declaring that an employeeNumbercolumn will maintain a value, starting at 0 and incrementing as each entry is added to the list. The default starting value can be overridden by the baseattribute. By default, the column can contain null (unordered) values. This can be overridden by setting the nullableattribute to false. By default, when the schema is generated
Note: If you are looking for high quality webhost to host and run your jsp application check Vision christian web host services