Archive for April, 2007

CHAPTER 6 (Top ten web hosting) n MAPPING WITH ANNOTATIONS n MAPPING

Monday, April 16th, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS n MAPPING WITH ANNOTATIONS Table 6-1. Continued Attribute Name Target Purpose FieldResult Pm Used as a parameter of the @SqlResultSetMapping annotation, permits the fields of an entity to be returned as columns in a conventional JDBC ResultSet. GeneratedValue M and F Allows generation strategies to be specified for the marked entity s primary key value(s). Id M and F Identifies the primary key of the entity. Placement of the @Id attribute also determines whether the default access mode for the entity class is field or property access. IdClass T Applied to indicate that an entity s primary key is represented with columns corresponding to the fields of another entity. The appropriate fields forming the primary key will be marked with the @Id attribute. Inheritance T Marks an entity as being the root of an entity inheritance hierarchy (i.e., the highest persistent class in the class inheritance hierarchy). JoinColumn/JoinColumns T, M, and F Defines the column(s) being used as a foreign key into another table. JoinTable M and F Allows the details of the link table to be specified in a one-to-many or many-to-many relationship. Lob M and F Marks a field or property as being stored as a large object data type typically a binary large object (BLOB). This can be used to remove the length limitations on strings and binary data, but usually implies reduced scope for querying the data so marked. ManyToMany M and F Allows a many-to-many association to be defined between entities. ManyToOne M and F Allows a many-to-one association to be defined between entities. MapKey M and F Allows a key to be specified when making an association with a Map object. MappedSuperclass T Allows a non-persistence class to be used as the basis of the mapping information for its derived classes. NamedNativeQuery/ NamedNativeQueries Pk and T Allows a named SQL query to be stored in the annotations. NamedQuery/NamedQueries Pk and T Allows a named EJB QL query to be stored in the annotations. OneToMany M and F Allows a one-to-many association to be defined between entities. OneToOne M and F Allows a one-to-one association to be defined between entities. OrderBy MF Allows the ordering of a collection to be defined as it is retrieved.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

CHAPTER 6 n MAPPING WITH (Crystaltech web hosting) ANNOTATIONS n MAPPING

Sunday, April 15th, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS n MAPPING WITH ANNOTATIONS mapping information. If you want to use the Hibernate tools to generate your database schema, you must compile your entity classes containing their annotations first. The full set of persistence annotations available in the EJB 3 API is listed in Table 6-1. In this section, we are going to introduce the significant core of these annotations alongside a simple set of classes to illustrate how they are applied. Table 6-1. The EJB 3 Annotations Attribute Name Target Purpose AttributeOverride/ T, M, and F Overrides the default column details of embedded AttributeOverrides (component) entities. Basic M and F Overrides the default fetch strategy and nullability of basic fields and properties. Column M and F Associates a field or property of the class with a column in the mapped table. ColumnResult Pm Used as a parameter of the @SqlResultSetMapping annotation; permits the fields of an entity to be returned as columns in a conventional JDBC ResultSet. DiscriminatorColumn T Overrides the default behavior of the discriminator column in single or joined table inheritance strategies. DiscriminatorValue T Determines the value associated with the entity in the discriminator column of the root of the entity s inheritance hierarchy. Embeddable T Marks an entity as being an embeddable (component) entity. Embedded M and F Marks a field or property as consisting of an embedded (component) entity. EmbeddedId M and F Marks a primary key field as consisting of an embedded (component) entity. This is mutually exclusive with the @Id annotation. Entity T Identifies an entity and allows attributes, such as its name, to be overridden from the defaults. EntityListeners T Allows appropriate javax.persistence. EntityListener classes to be invoked during the life cycle of the marked entity. EntityResult Pm Used as a parameter of the @SqlResultSetMapping annotation; permits the fields of an entity to be returned as columns in a conventional JDBC ResultSet. Enumerated M and F Defines a field or property as being an enumerated type. ExcludeDefaultListeners T Prevents the default EntityListeners from being invoked during the life cycle of the marked entity. ExcludeSuperclassListeners T Prevents the EntityListeners of the superclass from being invoked during the life cycle of the marked entity. Continued
Note: If you are looking for high quality webhost to host and run your jsp application check Vision christian web host services

CHAPTER 6 n MAPPING (Msn web hosting) WITH ANNOTATIONS n MAPPING

Sunday, April 15th, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS n MAPPING WITH ANNOTATIONS Using Annotations in Your Application You will need to install the Hibernate 3 annotations toolset, available from the Hibernate annotations page (http://annotations.hibernate.org). If you do not already use JDK 5.0, you will need to upgrade to take advantage of the native support for annotations. nTip If you want to declare your mappings inline with your source code, but cannot use a Java 5 environment, the XDoclet tool allows you to use javadoc-style comments to achieve a similar effect. XDoclet can be obtained from http://xdoclet.sourceforge.net. Your application needs the hibernate-annotations.jar and ejb3-persistence.jar files provided in the annotations toolset. If you are using a hibernate.cfg.xml file to establish the mapping configuration, you will need to provide the fully qualified name of the annotated class with the element: When you are configuring the SessionFactory, you will need to make use of an AnnotationConfiguration object instead of the Configuration object used with XML mappings, as follows: SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory(); If you prefer to configure the mappings manually rather than through the hibernate. cfg.xml file, you can do this through the AnnotationConfiguration object, as follows: AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(Book.class); SessionFactory factory = config.configure().buildSessionFactory(); If you need to use your annotated entities from within an EJB 3 container, you must use the standard EntityManager instead of the Hibernate-specific Session. Hibernate provides an implementation of EntityManager as a separate download. At the time of writing, this is still a beta version, but as it closely follows the state of the EJB 3 specification, you should have little trouble migrating code from the current implementation over to any final release (or to third- party EntityManagers). See Appendix A for details of how to use the Hibernate EntityManager. EJB 3 Persistence Annotations In Chapter 3, we walked you through the creation of a very simple application using the basic XML mapping files annotations might have been simpler, but as already noted, only Java 5 users would be able to use an annotation-based example. When you develop using annotations, you start with a Java class, and then annotate the source code listing with metadata notations. In J2SE 5.0, the Java Runtime Environment (JRE) parses these annotations. Hibernate uses Java reflection to read the annotations and apply the
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 n MAPPING (Sri lanka web server)

Saturday, April 14th, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS n MAPPING WITH ANNOTATIONS Listing 6-2. A Minimal Class Mapped Using XML
Some of this verbosity is in the nature of XML itself (the tag names and the boilerplate document type declaration), and some of it is due to the closer integration of annotations with the source code. Here, for example, the XML file must explicitly declare that field access is used in place of property access (i.e., the fields are accessed directly rather than through their get/set methods), but the annotation infers this from the fact that it has been applied to the id field rather than the getId() method. Hibernate uses and supports the EJB 3 persistence annotations. If you elect not to use Hibernate-specific features in your code and annotations, you will have the freedom to deploy your entities to environments using other ORM tools that support EJB 3. Finally and perhaps a minor point because the annotations are compiled directly into the appropriate class files, there is less risk of a missing or stale mapping file causing problems at deployment (this point will perhaps prove most persuasive to those who already have some experience with this hazard of the XML technique). Choosing Which to Use When you are creating a Hibernate application that has complete or primary ownership of its database, and that is a new project, we would generally recommend the use of annotations. If you intend to make your application portable to other EJB 3 compliant ORM applications, you must use annotations to represent the mapping information. Hibernate 3 XML file based mapping is a proprietary format. However, you may lose this benefit if you rely upon any of the Hibernate 3 specific annotations (that is to say, annotations taken from the org.hibernate package tree rather than the javax.persistence package tree). If you are migrating an existing application to Hibernate, or creating a new project reliant upon a database primarily owned by other applications, you should use the greater flexibility of XML-based mappings to ensure that your project will not be unduly inconvenienced by changes to the database schema.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

CHAPTER 6 n MAPPING WITH ANNOTATIONS n MAPPING (Web hosting colocation)

Saturday, April 14th, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS n MAPPING WITH ANNOTATIONS Cons of Annotations Using annotations immediately restricts your code to a Java 5 environment. This immediately rules out the use of annotations for some developers, as some application servers do not yet support this version of the JVM. Even when there are no technical reasons why a current JVM could not be used, many shops are quite conservative in the deployment of new technologies. If you are migrating from a Hibernate 2 environment or an existing Hibernate 3 environment, you will already have XML-based mapping files to support your code base. All else being equal, you will not want to re-express these mappings using annotations just for the sake of it. If you are migrating from a legacy environment, you may not want to alter the preexisting POJO source code, in order to avoid contaminating known-good code with possible bugs. If you do not have the source code to your POJOs (because it has been lost, or because it was generated by an automated tool), you may prefer the use of external XML-based mappings to the decompilation of class files to obtain Java source code for alteration. Maintaining the mapping information as external XML files allows the mapping information to be changed to reflect business changes or schema alterations without forcing you to rebuild the application as a whole. Hibernate 3 support for annotation-based mappings is not yet as mature as its support for XML-based mapping files. For example, while you can still make appropriate foreign key relationships for use in schema generation, you cannot generally name the foreign keys used. Pros of Annotations Having considered the drawbacks, there are some powerful benefits to contrast against them. First, and perhaps most persuasively, we find annotations-based mappings to be far more intuitive than their XML-based alternatives, as they are immediately in the source code along with the properties that they are associated with. Partly as a result of this, annotations are less verbose than their XML equivalents, as evidenced by the contrast between Listings 6-1 and 6-2. Listing 6-1. A Minimal Class Mapped Using Annotations import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Sample { @Id public Integer id; public String name; }
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

Mapping with Annotations CHAPTER 6 n n n

Friday, April 13th, 2007

Mapping with Annotations CHAPTER 6 n n n Mapping with Annotations CHAPTER 6 n n n In Chapter 5, we discussed the need to create mappings between the database model and the object model. Mappings can be created as separate XML files, or as Java 5 annotations inline with the source code for your POJOs. In this chapter, we discuss the use of annotations, and in the next chapter, we will discuss the use of XML files. Java 5 Features Java 5 was introduced in late 2004 as a major new release of the language. Annotations are not supported by versions of Java prior to this, so while core Hibernate 3 is compatible with earlier versions, you will not be able to take advantage of the features described in this chapter unless your development, compilation, and runtime tools support at least version 5 of the language (version 6 of Java, codenamed Mustang, is expected some time in late 2006). Since we must perforce assume that you have a Java 5 environment available to you, the examples in this chapter will also take advantage of some of the other enhanced language features introduced in Java 5, as follows: Generics Enhanced for loops Static imports Enumerations Autoboxing Variable parameter lists Using these features will make the source code for this chapter noticeably more compact. Similarly, annotation-based mappings are significantly terser than their XML-based counterparts. Creating Hibernate Mappings with Annotations Prior to annotations, the only way to create mappings was through XML files although tools from Hibernate and third-party projects allowed part or all of these to be generated from Java source code. Although using annotations is the newest way to define mappings, it is not automatically the best way to do so. We will briefly discuss the drawbacks and benefits of annotations before discussing when and how to apply them.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

CHAPTER 5 n AN OVERVIEW OF MAPPING n (Web design service)

Friday, April 13th, 2007

CHAPTER 5 n AN OVERVIEW OF MAPPING n AN OVERVIEW OF MAPPING Primary Key Hibernate demands that a primary key be used to identify entities. The choice of a surrogate key, a key chosen from the business data, and/or a compound primary key can be made via the mapping file. When a surrogate key is used, Hibernate also permits the key-generation technique to be selected from a range of techniques that vary in portability and efficiency. The Use of SQL Formula Based Properties It is sometimes desirable that a property of an entity should be maintained not as data directly stored in the database, but rather as a function performed on that data for example, a subtotal field should not be managed directly by the Java logic, but instead maintained as an aggregate function of some other property. Mandatory and Unique Constraints As well as the implicit constraints of a primary or foreign key relationship, you can specify that a field must not be duplicated for example, a username field should often be unique. Fields can also be made mandatory for example, requiring a message entity to have both a subject and message text. The generated database schema will contain corresponding NOT NULLand UNIQUEconstraints so that it is literally impossible to corrupt the table with invalid data (rather, the application logic will throw an exception if any attempt to do so is made). Note that primary keys are implicitly both mandatory and unique. Cascading of Operations As alterations are made to the object model, operations on some objects should cascade through to related objects. For example, deleting a stocked item should perhaps cause any associated catalog entries to be deleted. The reverse deleting a single catalog entry should not necessarily cause the stocked item to be deleted from the database entirely! It would be awkward to manage the appropriate cascading rules from code alone, so cascading rules can be specified at a fine level of detail within the mappings. Summary This chapter has given you an overview of the reason why mapping files are needed, and what features they support beyond these absolute requirements. It has discussed the various types of associations, and the circumstances under which you would choose to use them. The next two chapters look at how mappings are specified using annotations and XML files respectively.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

CHAPTER 5 n AN OVERVIEW OF MAPPING (Web hosting providers) n

Thursday, April 12th, 2007

CHAPTER 5 n AN OVERVIEW OF MAPPING n AN OVERVIEW OF MAPPING These files can be created directly with a text editor or with the help of various tools created by the Hibernate team and others. We discuss the details of XML mapping files in Chapter 8. Hibernate now also supports the Annotations feature introduced in Java 5. This permits the use of a special syntax to include metadata directly in the source code for the application. While this allows the core features of Hibernate to be controlled, many of the additional features cannot be specified in annotations. There is therefore something of a trade-off between the advantages of maintaining the mapping information directly within the associated source code, and the more flexible features available from the XML-based mappings. We discuss the details of annotation-based mapping in Chapter 6. Other Information Represented in Mappings While Hibernate can determine a lot of sensible default values for the mappings, most of these can be overridden by one or both of the file- and XML-based approaches. Some apply directly to mapping; others, such as the foreign key names, are really only pertinent when the mapping is used to create the database schema. Lastly, some mappings can also provide a place to configure some features that are perhaps not mappings in the purest sense. The final sections of this chapter discuss the features that Hibernate supports in addition to those already mentioned. Specification of (Database) Column Types and Sizes Java provides the primitive types and allows user declaration of interfaces and classes to extend these. Relational databases generally provide a small subset of standard types, and then provide additional proprietary types. Restricting yourself to the proprietary types will still cause problems, as there are only approximate correspondences between these and the Java primitive types. A typical example of a problematic type is java.lang.String (treated by Hibernate as if it were a primitive type since it is used so frequently), which by default will be mapped to a fixed-size character data database type. Typically, the database would perform poorly if a character field of unlimited size were chosen but lengthy String fields will be truncated as they are persisted into the database. By overriding the default type mappings, the developer can make appropriate trade-offs between storage space, performance, and fidelity to the original Java representation. The Mapping of Inheritance Relationships to the Database There is no SQL standard for representing inheritance relationships for the data in tables; and while some database implementations provide a proprietary syntax for this, not all do. Hibernate provides several configurable ways in which to represent inheritance relationships, and the mapping file permits users to select a suitable approach for their model.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision j2ee hosting services

CHAPTER 5 n AN OVERVIEW OF MAPPING n (Web server logs)

Thursday, April 12th, 2007

CHAPTER 5 n AN OVERVIEW OF MAPPING n AN OVERVIEW OF MAPPING Table 5-12. A Many-to-Many User/Email Link Table ID UserID EmailID 1 1 1 2 1 2 3 1 3 4 1 4 5 2 1 6 2 2 Table 5-12 might describe a situation in which the user dcminter receives all e-mail sent to any of the four addresses, whereas jlinwoodreceives only e-mail sent to his own accounts. When the link table has its own independent primary key, as with the association shown in Figure 5-8, thought should be given to the possibility that a new class should be created to represent the contents of the link table as an entity in its own right. Figure 5-8. A many-to-many relationship represented by a link table (duplicates are permitted because of the use of a surrogate key) Applying Mappings to Associations The mappings are applied to express the various different ways of forming associations in the underlying tables there is no automatically correct way to represent them. In addition to the basic choice of the approach to take, the mappings are used to specify the minutiae of the tables representations. While Hibernate tends to use sensible default values when possible, it is often desirable to override these. For example, the foreign key names generated automatically by Hibernate will be effectively random whereas an informed developer can apply a name (e.g., FK_USER_EMAIL_LINK) to aid in the debugging of constraint violations at run time. Types of Mapping At present, Hibernate supports two standard ways to express the mappings. The technique that has been available the longest is the use of XML mapping files. As the most mature approach, this is currently the best way to control Hibernate, and gives the most sophisticated control over the Hibernate feature set. You have seen examples of simple mapping files in Chapters 1 and 3.
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

Abyss web server - CHAPTER 5 n AN OVERVIEW OF MAPPING n

Thursday, April 12th, 2007

CHAPTER 5 n AN OVERVIEW OF MAPPING n AN OVERVIEW OF MAPPING Table 5-10. A Simple Email Table ID Email 1 dcminter@example.com 2 dave@example.com 3 jlinwood@example.com 4 jeff@example.com Table 5-11. A Link Table Joining User and Email in a One-to-Many Relationship UserID EmailID 11 12 23 24 Additional columns can be added to the link table to maintain information on the ordering of the entities in the association. A unique constraint must be applied to the one side of the relationship (the userId column of the UserEmailLinktable in Figure 5-7); otherwise, the link table can represent the set of all possible relationships between User and Email entities a many-to-many set association. Figure 5-7. A relationship represented by a link table (duplicates are not permitted because of the use of a compound primary key) The Many-to-Many Association As noted at the end of the previous section, if a unique constraint is not applied to the one end of the relationship when using a link table, it becomes a limited sort of many-to-many relationship. All of the possible combinations of User and Email can be represented, but it is not possible for the same User to have the same e-mail address entity associated twice, because that would require the compound primary key to be duplicated. If instead of using the foreign keys together as a compound primary key, we give the link table its own primary key (usually a surrogate key), the association between the two entities can be transformed into a full many-to-many relationship, as shown in Table 5-12.
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services