Archive for May, 2007

CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE XML (Web site developers)

Tuesday, May 8th, 2007

CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE XML FILES n CREATING MAPPINGS WITH HIBERNATE XML FILES 145 Attribute Values Default Description optimistic-lock persister polymorphism none, version implicit, explicit version implicit Specifies the optimistic locking dirty, all strategy to use. The strategy applies at a class level, but in Hibernate 3 can also be specified (or overridden) at an attribute level. Optimistic locking is discussed in Appendix A. Allows a custom ClassPersister object to be used when persisting the entity. Determines how polymorphism is to be used. The default implicit behavior will return instances of the class if superclasses or implemented interfaces are named in the query, and will return subclasses if the class itself is named in the query. proxy Specifies a class or an interface to use as the proxy for lazy initialization. Hibernate uses runtime-generated proxies by default, but you can specify your own implementation of org.hibernate. HibernateProxy in their place. rowid Flags that row IDs should be used (a database-implementation detail allowing Hibernate to optimize updates). schema Optionally overrides the schema specified by the element. select-beforeupdate true, false false Flags that Hibernate should carry out extra work to avoid issuing unnecessary UPDATE statements. If set to true, Hibernate issues a SELECT statement before attempting to issue an UPDATE statement in order to ensure that the UPDATE statement is actually required (i.e., that columns have been modified). While this is likely to be less efficient, it can prevent database triggers from being invoked unnecessarily. subselect A subselection of the contents of the underlying table. A class can only use a subselect if it is immutable and read- only (because the SQL defined here cannot be reversed). Generally, the use of a database view is preferable. table where The table name associated with the class (if unspecified, the unqualified class name will be used). An arbitrary SQL where condition to be used when retrieving objects of this class from the table. Many of these attributes in the element are designed to support preexisting database schemas. In practice, the name attribute is very often the only one set.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

144 CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE (Cpanel web hosting)

Tuesday, May 8th, 2007

144 CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE XML FILES CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE XML FILES Table 7-3. Continued Attribute Values Default Description check Defines an additional row-level check constraint, effectively adding this as a SQL CHECK( … ) clause during table generation (for example, check=”salary < 1000000"). discriminator-value A value used to distinguish between otherwise identical subclasses of a common type persisted to the same table. is null and is not null are permissible values. To distinguish between a Cat and a Dog derivative of the Mammal abstract class, for example, you might use discriminator values of C and D, respectively. dynamic-insert true, false false Indicates whether all columns should appear in INSERT statements. If the attribute is set to true, null columns will not appear in generated INSERT commands. On very wide tables, this may improve performance; but because insert statements are cached, dynamic-insert can easily produce a performance hit. dynamic-update true, false false Indicates whether all columns should appear in UPDATE statements. If the attribute is set to true, unchanged columns will not appear in generated UPDATE commands. As with dynamic- insert, this can be tweaked for performance reasons. You must enable dynamic-update if you want to use version- based optimistic locking (discussed in Appendix A). entity-name The name of the entity to use in place of the class name (therefore required if dynamic mapping is used). lazy true, false Used to disable or enable lazy fetching against the enclosing mapping s default. mutable true, false true Used to flag that a class is immutable (allowing Hibernate to make some performance optimizations when dealing with these classes). name The fully qualified Java name, or optionally unqualified if the element declares a package attribute, of the class (or interface) that is to be made persistent. node Specifies the name of the XML element or attribute that should be used by the XML relational persistence features.
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

CHAPTER 7 n (X web hosting) CREATING MAPPINGS WITH HIBERNATE XML

Monday, May 7th, 2007

CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE XML FILES 143 n CREATING MAPPINGS WITH HIBERNATE XML FILES 143 THE ORDER AND CARDINALITY INFORMATION FROM THE DTD The mapping files used by Hibernate have a great many elements and are somewhat self-referential. For example, the element permits you to include within it further elements, and within those further elements and so on, ad infinitum. While we do not quote exhaustively from the mapping file s DTD, we sometimes quote the part of it that specifies the permitted ordering and cardinality (number of occurrences) of the child elements of a given element. The cardinality is expressed by a symbol after the end of the name of the element: * means zero or more occurrences, ? means zero or one occurrences, and no trailing symbol means exactly one occurrence. The elements can be grouped using brackets, and where the elements are interchangeable, | (the pipe symbol) means or. In practical terms, this allows us to tell from the order and cardinality information quoted for the hibernate-mapping file that all of the elements immediately below it are, in fact, optional. We can also see that there is no limit to the number of elements that can be included. You can look up this ordering and cardinality information in the DTD for the mapping file for all the elements, including the ones that we have omitted from this chapter. You will also find within the DTD the specification of which attributes are permitted to each element, the values they may take (when they are constrained), and their default values when provided. We recommend that you look at the DTD for enlight- enment whenever you are trying to work out whether a specific mapping file should be syntactically valid. Throughout this book, we have assumed that the mappings are defined in one mapping file for each significant class that is to be mapped to the database. We suggest that you follow this practice in general, but there are some exceptions to this rule. You may, for instance, find it useful to place query and sql-query entries into an independent mapping file, particularly when they do not fall clearly into the context of a single class. The Element The child element that you will use most often indeed, in nearly all of your mapping files is . As you have seen in earlier chapters, we generally describe the relationships between Java objects and database entities in the body of the element. The element permits the following attributes to be defined (see Table 7-3). Table 7-3. The Attributes Attribute Values Default Description abstract true, false false The flag that should be set if the class being mapped is abstract. batch-size 1 Specifies the number of items that can be batched together when retrieving instances of the class by identifier. catalog The database catalog against which the queries should apply. Continued
Note: If you are looking for cheap webhost to host and run your apache application check Vision jboss web hosting services

Web hosting asp - 142 CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE

Monday, May 7th, 2007

142 CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE XML FILES CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE XML FILES Table 7-2. The Attributes Attribute Values Default Description auto-import true, false true By default, allows you to use the unqualified class names in Hibernate queries. You would normally only set this to false if the class name would otherwise be ambiguous. catalog The database catalog against which queries should apply. default-access property The default access type. If set to property, then get and set methods are used to access the data. If set to field, then the data is accessed directly. Alternatively, you can provide the class name of a PropertyAccessor implementation defining any other access mechanism. default-cascade Defines how (and whether) direct changes to data should affect dependent data by default. default-lazy true, false true Defines whether lazy instantiation is used by default. Generally, the performance benefits are such that you will want to use lazy instantiation whenever possible. package The package from which all implicit imports are considered to occur. schema The database schema against which queries should apply. The default cascade modes available for the default-cascade attribute (and for the cascade attributes in all other elements) are as follows: create, merge, delete, save-update, evict, replicate, lock, refresh These correspond to the various possible changes in the lifestyle of the parent object. When set (you can include combinations of them as comma-separated values), the relevant changes to the parent will be cascaded to the relation. For example, you may want to apply the save-update cascade option to a class that includes Set attributes, so that when new persistent classes are added to these, they will not have to be saved explicitly in the session. There are also three special options: all, delete-orphan, none allspecifies that all changes to the parent should be propagated to the relation, and none specifies that none should. delete-orphanapplies only to one-to-many associations, and specifies that the relation should be deleted when it is no longer referenced by the parent. The required order and cardinality of the child elements of are as follows: (meta*, typedef*, import*, (class | subclass | joined-subclass | union-subclass)*, (query | sql-query)*, filter-def*)
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

Web design rates - CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE XML

Sunday, May 6th, 2007

CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE XML FILES n CREATING MAPPINGS WITH HIBERNATE XML FILES 141 Hibernate 3 Type Corresponding Java Type big_decimal java.math.BigDecimal big_integer locale java.math.BigInteger java.util.Locale timezone java.util.TimeZone currency java.util.Currency class java.lang.Class binary byte[] text serializable java.lang.String java.io.Serializable clob java.sql.Clob blob java.sql.Blob In addition to these standard types, you can create your own. Your user type class should implement either the org.hibernate.usertype.UserType interface or the org.hibernate. usertype.CompositeUserType interface. Once implemented, a custom type can behave identically to the standard types; though depending on your requirements, it may be necessary to specify multiple column names to contain its values, or to provide initialization parameters for your implementation. For one-off cases, we recommend that you use components these have similar behavior, but they can be created in the mapping file without needing to write Hibernate-specific code. Unless you propose to make substantial use of a custom type throughout your application, it will not be worth the effort. We do not discuss this feature further in this book. The Anatomy of a Mapping File A mapping file is a normal XML file. It is validated against a DTD, which can be downloaded from http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd. You can also look through the annotated version at http://hibernatebook.com. The terminology used in the naming of elements and attributes is somewhat confusing at first because it is the point of contact between the jargon of the object-oriented and relational worlds. The Element The root element of any mapping file is . As the top-level element, its attributes mostly define default behaviors and settings to apply to the child elements (see Table 7-2).
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

Web domain - 140 CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE

Sunday, May 6th, 2007

140 CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE XML FILES CHAPTER 7 n CREATING MAPPINGS WITH HIBERNATE XML FILES Components Lying somewhere between entities and values are component types. When the class representation is simple and its instances have a strong one-to-one relationship with instances of another class, then it is a good candidate to become a component of that other class. The component will normally be mapped as columns in the same table that represents most of the other attributes of the owning class, so the strength of the relationship must justify this inclusion. In the following code, the MacAddress class might a good candidate for a com ponent relationship. public class NetworkInterface { public int id; public String name; public String manufacturer; public MacAddress physicalAddress; } The advantage of this approach is that it allows you to dispense with the primary key of the component and the join to its containing table. If a poor choice of component is made (for example, when a many-to-one relationship actually holds), then data will be duplicated unnecessarily in the component columns. Values Everything that is not an entity or a component is a value. Generally, these correspond to the data types supported by your database, the collection types, and, optionally, some user- defined types. The details of these mappings will be vendor-specific, so Hibernate provides its own value type names; the Java types are defined in terms of these (see Table 7-1). Table 7-1. The Standard Hibernate 3 Value Names Hibernate 3 Type Corresponding Java Type Primitives and Wrappers integer int, java.lang.Integer long long, java.lang.Long short short, java.lang.Short float float, java.lang.Float double double, java.lang.Double character char, java.lang.Character byte byte, java.lang.Byte boolean, yes_no, true_false boolean, java.lang.Boolean Other Classes string java.lang.String date, time, timestamp java.util.Date calendar, calendar_date java.util.Calendar
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

Creating Mappings with Hibernate XML (My web server) Files CHAPTER 7

Saturday, May 5th, 2007

Creating Mappings with Hibernate XML Files CHAPTER 7 n n n Creating Mappings with Hibernate XML Files CHAPTER 7 n n n In the simple example programs in Chapters 1 and 3, we demonstrated how a mapping file could be used to establish the relationship between the object model and the database schema. A mapping file can map a single class or multiple classes to the database. The mapping can also describe standard queries (in HQL and SQL) and filters. Hibernate Types Although we have referred to the Hibernate types in passing, we have not discussed the terminology in any depth. In order to express the behavior of the mapping file elements, we need to make these fine distinctions explicit. Hibernate types fall into three broad categories: entities, components, and values. Entities Generally, an entity is a POJO class that has been mapped into the database using the or elements. An entity can also be a dynamic map (actually a Map of Maps). These are mapped against the database in the same way as a POJO, but with the default entity mode of the SessionFactory set to dynamic-map. The advantage of POJOs over the dynamic-map approach is that compile-time type safety is retained. Conversely, dynamic maps are a quick way to get up and running when building prototypes. It is also possible to represent your entities as Dom4J Document objects. This is a useful feature when importing and exporting data from a preexisting Hibernate database, but it is not really central to the everyday use of Hibernate. We recommend that you use the standard entity mode unless you need to sacrifice accuracy for timeliness, so the alternate approaches are not discussed in this chapter however, we give some simple examples of the Dom4J- and Map-based mappings in Appendix A.
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 (Hosting web) 137 n

Friday, May 4th, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS 137 n MAPPING WITH ANNOTATIONS 137 create table ComputerBook ( BOOK_ID integer not null, softwareName varchar(255), primary key (BOOK_ID) ); create table Publisher ( id integer generated by default as identity (start with 1), name varchar(255), address_id integer, primary key (id) ); alter table Book add constraint FK1FAF0990BF1C70 foreign key (publisher_id) references Publisher; alter table Book_Author add constraint FK1A9A0FA1B629DD87 foreign key (authors_id) references Author; alter table Book_Author add constraint FK1A9A0FA1D3BA8BC3 foreign key (books_id) references Book; alter table ComputerBook add constraint FK98D97CC4600B1724 foreign key (BOOK_ID) references Book; alter table Publisher add constraint FKCDB7C1DC158ECEF0 foreign key (address_id) references Address; Summary In this chapter, we used EJB 3 annotations to add metadata to our POJOs for Hibernate, and we looked at the Hibernate-specific annotations that can enhance these at the cost of reduced portability. In the next chapter, we discuss the alternative approach of using XML mapping documents to express the mapping requirements.
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

Web hosting unlimited bandwidth - 136 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER

Friday, May 4th, 2007

136 CHAPTER 6 n MAPPING WITH ANNOTATIONS CHAPTER 6 n MAPPING WITH ANNOTATIONS public void setName(String name) { this.name = name; } public void setBooks(Set books) { this.books = books; } public void setAddress(AuthorAddress address) { this.address = address; } } Finally, Listing 6-36 shows the database schema for the classes supporting this chapter, as generated by the Ant hbm2ddl export task for the HSQL database. You will note that we are unable to control the names selected for the foreign key relationships. This is one area in which the Hibernate XML mapping files are superior to the EJB 3 annotations. Listing 6-36. The Database Schema for the Example create table Address ( id integer not null, city varchar(255), country varchar(255), primary key (id) ); create table Author ( id integer generated by default as identity (start with 1), ADDR varchar(255), NATION varchar(255), name varchar(255), primary key (id) ); create table Book ( id integer generated by default as identity (start with 1), pages integer not null, working_title varchar(200) not null, publisher_id integer, primary key (id) ); create table Book_Author ( books_id integer not null, authors_id integer not null, primary key (books_id, authors_id) );
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services

Unable to start debugging on the web server - CHAPTER 6 n MAPPING WITH ANNOTATIONS 135 n

Thursday, May 3rd, 2007

CHAPTER 6 n MAPPING WITH ANNOTATIONS 135 n MAPPING WITH ANNOTATIONS 135 private int id; private String name; private Set books = new HashSet(); private AuthorAddress address; // Constructors… protected Author() { } public Author(String name, AuthorAddress address) { this.name = name; this.address = address; } // Getters… @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } @ManyToMany(mappedBy = “authors”) public Set getBooks() { return books; } @Embedded @AttributeOverrides({ @AttributeOverride(name=”address”,column=@Column(name=”ADDR”)), @AttributeOverride(name=”country”,column=@Column(name=”NATION”)) }) public AuthorAddress getAddress() { return this.address; } // Setters… public void setId(int id) { this.id = id; }
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services