Archive for December, 2007

CHAPTER 5 Inheritance and custom types @Lob @Column(name (Web host music)

Monday, December 31st, 2007

CHAPTER 5 Inheritance and custom types @Lob @Column(name = “ITEM_IMAGE”) private byte[] image; The same is true for any property that is of type byte[], Byte[], or java. sql.Blob. Note that for all cases, except properties that are of java.sql.Clob or java.sql.Blob type, the values are again loaded immediately by Hibernate, and not lazily on demand. Instrumenting bytecode with interception code is again an option to enable lazy loading of individual properties transparently. To create and set a java.sql.Blob or java.sql.Clob value, if you have these property types in your domain model, use the static Hibernate.createBlob() and Hibernate.createClob() methods and provide a byte array, an input stream, or a string. Finally, note that both Hibernate and JPA provide a serialization fallback for any property type that is Serializable. This mapping type converts the value of a property to a byte stream that is then stored in a VARBINARY (or equivalent) column. When the owner of the property is loaded, the property value is deserialized. Naturally, you should use this strategy with extreme caution (data lives longer than an application), and it may be useful only for temporary data (user preferences, login session data, and so on). JDK mapping types Table 5.4 lists Hibernate types for various other Java types of the JDK that may be represented as a VARCHAR in the database. You may have noticed that
isn t the only Hibernate mapping element that has a type attribute. Table 5.4 Other JDK-related types Mapping type Java type Standard SQL built-in type class java.lang.Class VARCHAR locale java.util.Locale VARCHAR timezone java.util.TimeZone VARCHAR currency java.util.Currency VARCHAR
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

The Hibernate type (Web hosts) system type depends on the

Monday, December 31st, 2007

The Hibernate type system type depends on the dialect; for example, in PostgreSQL, the SQL type is BYTEA, and in Oracle it s RAW.) If a property in your persistent Java class is of type java.lang.String, Hibernate can map it to an SQL CLOB column, with the text mapping type. Note that in both cases, Hibernate initializes the property value right away, when the entity instance that holds the property variable is loaded. This is incon venient when you have to deal with potentially large values. One solution is lazy loading through interception of field access, on demand. However, this approach requires bytecode instrumentation of your persistent classes for the injection of extra code. We ll discuss lazy loading through bytecode instrumentation and interception in chapter 13, section 13.1.6, Lazy loading with interception. A second solution is a different kind of property in your Java class. JDBC supports locator objects (LOBs) directly.1 If your Java property is of type java.sql.Clob or java.sql.Blob, you can map it with the clob or blob mapping type to get lazy loading of large values without bytecode instrumentation. When the owner of the property is loaded, the property value is a locator object effectively, a pointer to the real value that isn t yet materialized. Once you access the property, the value is materialized. This on-demand loading works only as long as the database transaction is open, so you need to access any property of such a type when the owning entity instance is in a persistent and transactional state, not in detached state. Your domain model is now also bound to JDBC, because the import of the java.sql package is required. Although domain model classes are executable in isolated unit tests, you can t access LOB properties without a database connection. Mapping properties with potentially large values is slightly different if you rely on Java Persistence annotations. By default, a property of type java.lang.String is mapped to an SQL VARCHAR column (or equivalent, depending on the SQL dialect). If you want to map a java.lang.String, char[], Character[], or even a java.sql.Clob typed property to a CLOB column, you need to map it with the @Lob annotation: @Lob @Column(name = “ITEM_DESCRIPTION”) private String description; 1 Jim Starkey, who came up with the idea of LOBs, says that the terms BLOB and CLOB don t mean anything but were created by the marketing department. You can interpret them any way you like. We prefer locator objects, as a hint that they work like pointers.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

CHAPTER 5 Inheritance and custom types Table 5.2 (Domain and web hosting)

Sunday, December 30th, 2007

CHAPTER 5 Inheritance and custom types Table 5.2 Date and time types Mapping type Java type Standard SQL built-in type date java.util.Date or java.sql.Date DATE time java.util.Date or java.sql.Time TIME timestamp java.util.Date or java.sql.Timestamp TIMESTAMP calendar java.util.Calendar TIMESTAMP calendar_date java.util.Calendar DATE nanosecond information that may be present in the database. Hibernate can t just cut off this information. This can lead to problems if you try to compare your java.util.Date properties with the equals() method, because it isn t symmetric with the java.sql.Timestamp subclass equals() method. First, the right way (in any case) to compare two java.util.Date objects, which also works for any subclass, is aDate.getTime() > bDate.getTime() (for a greater-than comparison). Second, you can write a custom mapping type that cuts off the database nanosecond information and returns a java.util.Date in all cases. Currently (although this may change in the future), no such mapping type is built into Hibernate. Binary and large value mapping types Table 5.3 lists Hibernate types for handling binary data and large values. Note that only binary is supported as the type of an identifier property. If a property in your persistent Java class is of type byte[], Hibernate can map it to a VARBINARY column with the binary mapping type. (Note that the real SQL Table 5.3 Binary and large value types Mapping type Java type Standard SQL built-in type binary byte[] VARBINARY text java.lang.String CLOB clob java.sql.Clob CLOB blob java.sql.Blob BLOB serializable Any Java class that implements java.io.Serializable VARBINARY
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Free web space - The Hibernate type system You ve probably noticed that

Sunday, December 30th, 2007

The Hibernate type system You ve probably noticed that your database doesn t support some of the SQL types mentioned in table 5.1. The listed type names are names of ANSI-standard datatypes. Most database vendors ignore this part of the SQL standard (because their legacy type systems often predate the standard). However, the JDBC driver provides a partial abstraction of vendor-specific SQL datatypes, allowing Hibernate to work with ANSI-standard types when executing DML. For database-specific DDL generation, Hibernate translates from the ANSI-standard type to an appropriate vendor-specific type, using the built-in support for specific SQL dialects. (This means you usually don t have to worry about SQL datatypes if you re using Hibernate for data access and SQL schema definition.) Furthermore, the Hibernate type system is smart and can switch SQL datatypes depending on the defined length of a value. The most obvious case is string: If you declare a string property mapping with a length attribute, Hibernate picks the correct SQL datatype depending on the selected dialect. For MySQL, for example, a length of up to 65535 results in a regular VARCHAR(length) column when Hibernate exports the schema. For a length of up to 16777215, a MEDIUMTEXT datatype is used. Larger string mappings result in a LONGTEXT. Check your SQL dialect (the source code comes with Hibernate) if you want to know the ranges for this and other mapping types. You can customize this behavior by sub- classing your dialect and overriding these settings. Most dialects also support setting the scale and precision of decimal SQL datatypes. For example, a precision or scale setting in your mapping of a Big- Decimal creates a NUMERIC(precision, scale) datatype for MySQL. Finally, the yes_no and true_false mapping types are converters that are mostly useful for legacy schemas and Oracle users; Oracle DBMS products don t have a built-in boolean or truth-valued type (the only built-in datatype actually required by the relational data model). Date and time mapping types Table 5.2 lists Hibernate types associated with dates, times, and timestamps. In your domain model, you may choose to represent date and time data using java.util.Date, java.util.Calendar, or the subclasses of java.util.Date defined in the java.sql package. This is a matter of taste, and we leave the decision to you make sure you re consistent, however. (In practice, binding your domain model to types from the JDBC package isn t the best idea.) A caveat: If you map a java.util.Date property with timestamp (the most common case), Hibernate returns a java.sql.Timestamp when loading the property from the database. Hibernate has to use the JDBC subclass because it includes
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

CHAPTER 5 Inheritance and custom types The string (Web server logs)

Saturday, December 29th, 2007

CHAPTER 5 Inheritance and custom types The string mapping type isn t the only one built into Hibernate. Hibernate comes with various mapping types that define default persistence strategies for primitive Java types and certain JDK classes. 5.2.2 Built-in mapping types Hibernate s built-in mapping types usually share the name of the Java type they map. However, there may be more than one Hibernate mapping type for a particular Java type. The built-in types may not be used to perform arbitrary conversions, such as mapping a VARCHAR database value to a Java Integer property value. You may define your own custom value types for this kind of conversation, as shown later in this chapter. We now discuss the basic, date and time, locator object, and various other built-in mapping types and show you what Java and SQL datatype they handle. Java primitive mapping types The basic mapping types in table 5.1 map Java primitive types (or their wrapper types) to appropriate built-in SQL standard types. Table 5.1 Primitive types Mapping type Java type Standard SQL built-in type integer intor java.lang.Integer INTEGER long long or java.lang.Long BIGINT short short or java.lang.Short SMALLINT float float or java.lang.Float FLOAT double double or java.lang.Double DOUBLE big_decimal java.math.BigDecimal NUMERIC character java.lang.String CHAR(1) string java.lang.String VARCHAR byte byte or java.lang.Byte TINYINT boolean boolean or java.lang.Boolean BIT yes_no boolean or java.lang.Boolean CHAR(1) (’Y’ or ‘N’) true_false boolean or java.lang.Boolean CHAR(1) (’T’ or ‘F’)
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

The Hibernate type system In Hibernate, a value (Web hosting company)

Saturday, December 29th, 2007

The Hibernate type system In Hibernate, a value type may define associations; it s possible to navigate from a value type instance to some other entity. However, it s never possible to navigate from the other entity back to the value type instance. Associations always point to entities. This means that a value type instance is owned by exactly one entity when it s retrieved from the database; it s never shared. At the level of the database, any table is considered an entity. However, Hibernate provides certain constructs to hide the existence of a database-level entity from the Java code. For example, a many-to-many association mapping hides the intermediate association table from the application. A collection of strings (more accurately, a collection of value-typed instances) behaves like a value type from the point of view of the application; however, it s mapped to its own table. Although these features seem nice at first (they simplify the Java code), we have over time become suspicious of them. Inevitably, these hidden entities end up needing to be exposed to the application as business requirements evolve. The many-to-many association table, for example, often has additional columns added as the application matures. We re almost prepared to recommend that every database- level entity be exposed to the application as an entity class. For example, we would be inclined to model the many-to-many association as two one-to-many associations to an intervening entity class. We ll leave the final decision to you, however, and come back to the topic of many-to-many entity associations in the future chapters. Entity classes are always mapped to the database using , , , and mapping elements. How are value types mapped? You ve already met two different kinds of value type mappings:
and . The value type of a component is obvious: It s the class that is mapped as embeddable. However, the type of a property is a more generic notion. Consider this mapping of the CaveatEmptor User and email address:
Let s focus on that type=”string” attribute. You know that in ORM you have to deal with Java types and SQL datatypes. The two different type systems must be bridged. This is the job of the Hibernate mapping types, and string is the name of a built-in Hibernate mapping type.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

CHAPTER 5 Inheritance and custom types You now (Web host)

Friday, December 28th, 2007

CHAPTER 5 Inheritance and custom types You now know everything you need to know about the mapping of entities, properties, and inheritance hierarchies. You can already map complex domain models. In the second half of this chapter, we discuss another important feature that you should know by heart as a Hibernate user: the Hibernate mapping type system. 5.2 The Hibernate type system In chapter 4, we first distinguished between entity and value types a central concept of ORM in Java. We must elaborate on that distinction in order for you to fully understand the Hibernate type system of entities, value types, and mapping types. 5.2.1 Recapitulating entity and value types Entities are the coarse-grained classes in your system. You usually define the features of a system in terms of the entities involved. The user places a bid for an item is a typical feature definition; it mentions three entities. Classes of value types often don t even appear in the business requirements they re usually the fine-grained classes representing strings, numbers, and monetary amounts. Occasionally, value types do appear in feature definitions: the user changes billing address is one example, assuming that Address is a value type. More formally, an entity is any class whose instances have their own persistent identity. A value type is a class that doesn t define some kind of persistent identity. In practice, this means that entity types are classes with identifier properties, and value type classes depend on an entity. At runtime, you have a network of entity instances interleaved with value type instances. The entity instances may be in any of the three persistent lifecycle states: transient, detached, or persistent. We don t consider these lifecycle states to apply to the value type instances. (We ll come back to this discussion of object states in chapter 9.) Therefore, entities have their own lifecycle. The save()and delete()methods of the Hibernate Session interface apply to instances of entity classes, never to value type instances. The persistence lifecycle of a value type instance is completely tied to the lifecycle of the owning entity instance. For example, the username becomes persistent when the user is saved; it never becomes persistent independently of the user.
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Mapping class inheritance class at runtime) or queries, (Web hosting contract)

Friday, December 28th, 2007

Mapping class inheritance class at runtime) or queries, and subclasses declare relatively few properties (particularly if the main difference between subclasses is in their behavior), lean toward table-per-class-hierarchy. Your goal is to minimize the number of nullable columns and to convince yourself (and your DBA) that a denormalized schema won t create problems in the long run. If you do require polymorphic associations or queries, and subclasses declare many properties (subclasses differ mainly by the data they hold), lean toward table-per-subclass. Or, depending on the width and depth of your inheritance hierarchy and the possible cost of joins versus unions, use table-per-concrete-class. By default, choose table-per-class-hierarchy only for simple problems. For more complex cases (or when you re overruled by a data modeler insisting on the importance of nullability constraints and normalization), you should consider the table-per-subclass strategy. But at that point, ask yourself whether it may not be better to remodel inheritance as delegation in the object model. Complex inheritance is often best avoided for all sorts of reasons unrelated to persistence or ORM. Hibernate acts as a buffer between the domain and relational models, but that doesn t mean you can ignore persistence concerns when designing your classes. When you start thinking about mixing inheritance strategies, remember that implicit polymorphism in Hibernate is smart enough to handle more exotic cases. For example, consider an additional interface in our application, Electronic- PaymentOption. This is a business interface that doesn t have a persistence aspect except that in our application, a persistent class such as CreditCard will likely implement this interface. No matter how you map the BillingDetails hierarchy, Hibernate can answer a query from ElectronicPaymentOption correctly. This even works if other classes, which aren t part of the BillingDetails hierarchy, are mapped persistent and implement this interface. Hibernate always know what tables to query, which instances to construct, and how to return a polymorphic result. Finally, you can also use , , and mapping elements in a separate mapping file (as a top-level element instead of ). You then have to declare the class that is extended, such as , and the superclass mapping must be loaded programmatically before the subclass mapping file (you don t have to worry about this order when you list mapping resources in the XML configuration file). This technique allows you to extend a class hierarchy without modifying the mapping file of the superclass.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

CHAPTER 5 Inheritance and custom types name = (Web server application)

Friday, December 28th, 2007

CHAPTER 5 Inheritance and custom types name = “CREDIT_CARD”, pkJoinColumns = @PrimaryKeyJoinColumn(name = “CREDIT_CARD_ID”) ) public class CreditCard extends BillingDetails { @Column(table = “CREDIT_CARD”, name = “CC_NUMBER”, nullable = false) private String number; … } If you don t specify a primary key join column for the secondary table, the name of the primary key of the single inheritance table is used in this case, BILLING_DETAILS_ID. Also note that you need to map all properties that are moved into the secondary table with the name of that secondary table. You also want more tips about how to choose an appropriate combination of mapping strategies for your application s class hierarchies. 5.1.6 Choosing a strategy You can apply all mapping strategies to abstract classes and interfaces. Interfaces may have no state but may contain accessor method declarations, so they can be treated like abstract classes. You can map an interface with , , , or , and you can map any declared or inherited property with
. Hibernate won t try to instantiate an abstract class, even if you query or load it. NOTE Note that the JPA specification doesn t support any mapping annotation on an interface! This will be resolved in a future version of the specification; when you read this book, it will probably be possible with Hibernate Annotations. Here are some rules of thumb: If you don t require polymorphic associations or queries, lean toward table- per-concrete-class in other words, if you never or rarely query for BillingDetails and you have no class that has an association to BillingDetails (our model has). An explicit UNION-based mapping should be preferred, because (optimized) polymorphic queries and associations will then be possible later. Implicit polymorphism is mostly useful for queries utilizing non-persistence-related interfaces. If you do require polymorphic associations (an association to a superclass, hence to all classes in the hierarchy with dynamic resolution of the concrete
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Mapping class inheritance Figure 5.4 Breaking out a (Web design templates)

Thursday, December 27th, 2007

Mapping class inheritance Figure 5.4 Breaking out a subclass to its own secondary table You can also use the trick for other subclasses in your class hierarchy. However, if you have an exceptionally wide class hierarchy, the outer join can become a problem. Some database systems (Oracle, for example) limit the number of tables in an outer join operation. For a wide hierarchy, you may want to switch to a different fetching strategy that executes an immediate second select instead of an outer join: Java Persistence also supports this mixed inheritance mapping strategy with annotations. Map the superclass BillingDetails with InheritanceType.SINGLE_ TABLE, as you did before. Now map the subclass you want to break out of the single table to a secondary table. @Entity @DiscriminatorValue(”CC”) @SecondaryTable(
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.