Hibernate _ note 18 (annotation-based Metadata)

Source: Internet
Author: User

The basic idea of annotation-based Metadata is to put metadata together with the information it describes, rather than separating it into a different file. Java did not provide this function before JDK5.0, so it developed an alternative solution. The XDoclet project uses a special Javadoc tag that supports key/value pairs and introduces Java source code annotations that contain metadata. Through tag nesting, a very complex structure is also supported, but only some ides allow custom Javadoc templates for automatic ground-breaking verification.
Java standard request (JSR) 175 introduces the annotation concept in the Java language, and uses the type security and declarative interfaces for the annotation definition. Checking for Automatic completion and compilation is no longer a problem.
Now we will introduce the ing annotation and use JDK5.0. If you must use JDK1.4 but prefer annotation-based Metadata, consider the XDoclet we will discuss later.
1. Define and use annotations

import javax.persistence.*;@Entity@Table(name="TBL_ITEM")public class Item {//...}
This public Item class has been declared as a persistent object. All its attributes are now automatically persisted using the Default policy. Another Annotation is displayed, which declares the table name in the database Schema mapped to this persistence class. If this information is omitted, the JPA provider defaults to the unqualified class name, just as what Hibernate does when you omit the table name in an XML ing file.
All of these are type-safe. Therefore, when Hibernate is started, the declared annotation is read through Java Reflection. You do not need to write any XML ing files. Hibernate does not need to parse any XML and starts faster. IDE can also easily verify and emphasize annotations-after all, they are common Java types.
One of the obvious advantages of annotation is their flexibility for agile development, If you often refactor code, rename, delete or move classes and attributes. Most development tools and editors cannot reconstruct XML elements and attribute values, but annotations are part of the Java language, including in all refactoring operations.
What annotations should be applied? There are several standard vendor-specific packages for you to choose from.
2. Considering standard annotation-based Metadata has a significant impact on how you write Java applications. Other programming environments, such as C # And. NET, have long had such support, and developers have quickly adopted metadata attributes. In the Java World, annotations are widely used in Java EE 5.0. All specifications considered to be part of JavaEE, such as EJB, JMS (Java Message Service), JMX, and even servlet specifications will be updated, and JDK5.0 annotation will be used as required by metadata. Sun introduced a standard result (JSR250) to process annotations of different specifications and defined general annotations for the entire Java platform. However, the most important specification for your work on the persistence layer is EJB3.0 and JPA.
Once you have included the JAP interface in classpath, annotations from the Java Persistence package can be used in javax. persistence. You can use these annotations to declare Persistent object classes, embedded classes, attributes, fields, and keys. The JPA specification covers basic and most relevant advanced mappings-everything required to write a portable application, including a pluggable, standard persistence layer, it is applicable to the internal and external containers at any runtime.
Which annotations and ing features are not specified in Java Persistence? Specific JPA engines and products generally offer these advantages-so-called vendor expansion.
3. Use vendor extensions. Even if you map most models of an application using the JPA-compatible annotation in the javax. persistence package, sometimes you must use vendor extensions. For example, you want to use almost all of the Performance Tuning options available in high-quality persistent software, such as crawling and high-speed cache settings, as only Hibernate-specific annotations.
Let's look at what this will look like in an example. Re-annotate the source code of the Item object:
import javax.persistence.*;@Entity@Table(name="TBL_ITEM")public class Item {//...}
This example contains two Hibernate annotations. The first annotation @ BatchSize is a capture option, which can improve performance in several cases to be verified later in this book. The second annotation @ DiscriminatorFormula is a Hibernate ing annotation. When the class inheritance cannot use a simple text value (literal value) when deciding (here it maps a legacy column ITEM_IS_SPECIAL -- maybe some mark -- to a text value), it is particularly useful for legacy modes. Both annotations use org. hibernate. annotations as the prefix of the package name. Take this as a good practice, because now you can easily see what metadata of this entity comes from the JPA specification, and which labels are vendor-specific. You can also easily search for "org. hibernate. annotations" in the source code and get a comprehensive overview of all non-standard annotations in your application in a single search result.
The annotations in the class only cover the metadata that applies to the specific class. However, at a higher level, metadata is often required for the entire package or even the entire application. Before discussing these methods, we will introduce another format for ing metadata.
4. XML descriptor EJB3.0 and Java Persistence standards in JPA and EJB3.0 both compete for annotations. However, the Expert Group has noticed the advantages of XML deployment descriptors in some situations, especially for configuration metadata that changes with each deployment. As a result, every annotation in EJB3.0 and JPA can be replaced with an XML descriptor element. In other words, if you do not want to use it, you do not need to use annotations (but it is strongly recommended ).
Let's take a look at the jpa xml descriptor example of a specific persistence unit:
Note: xsi: schemaLocation = "http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd" is written in this book, but the program always reports an error and does not see another way of writing on a foreign website.
Xsi: schemaLocation = "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" this is OK, giggle, good luck, just find it, but XML is not very familiar with this, I hope you can share it.
 
 
  
   
   
   
    
    
     
System
    
    
    FIELD/PROPERTY 
    
    
    
   
  
  
   
Cn. jbit. entity
  
  
   
    
   
   
    
   
   
    
   
  
 
I have tried the Catalog Task label for a long time. It turns out that it is not supported by Oracle databases, such as tables:
Supplier Catalog support Schema support
Oracle Not Supported Oracle User ID
MySQL Not Supported Database Name
Ms SQL Server Database Name The object owner name, which has changed since version 2005.
DB2 The Catalog part is omitted when the database object is specified. Catalog owner name
Sybase Sybase Database owner name
Informix Not Supported Not Supported
PointBase PointBase PointBase
This XML is automatically selected by the JPA provider if it is placed in a file named orm. xml under the META-INF directory of the persistent unit in classpath. You will see that you only need to name an identifier attribute for a class. Just like in the annotation, all other attributes of the object class are automatically considered persistent through an obvious default ing.
You can also set default mappings for the entire persistence unit, such as Schema names and default cascade options. If The JPA provider ignores all annotations of the entity class in this persistence unit, and only relies on mappings defined in the orm. xml file.
If you do not want to ignore the annotation metadata, you must first remove the global Element. You also need to remove the metadata-complete = "true" attribute from any entity ing that should overwrite (rather than replace) the annotation.
An obvious problem with using XML deployment descriptors in Java Persistence is their compatibility with native Hibernate XML ing files. These two formats are not compatible at all. You should choose one or another. The jpa xml descriptor syntax is closer to the actual JPA annotation than the native Hibernate XML ing file.
Vendor extensions are also required when determining an XML Metadata format. The Hibernate XML format supports all possible Hibernate mappings. Therefore, if some things cannot be mapped in the JPA/Hibernate annotations, you can use the native Hibernate XML file ing. The same thing is not true for jpa xml descriptors-they only provide convenient and specific metadata that covers specifications.
On the other hand, you cannot overwrite the Annotation with the Hibernate XML ing file; you must define a complete object class ing in XML format.
If JDK5.0 is being used, the JPA/Hibernate annotation is the first choice. If you want to embody a specific class ing, or use a Hibernate extension that cannot be used as an annotation, you can return to the native Hibernate XML ing file. If you do not intend to use any vendor extension (in fact, this is not possible), or if you only want to override a few annotations, or if you want to even include the complete portability of the deployment descriptor, consider using the jpa xml descriptor.
But what if you are used to using JDK1.4 (or even 1.3) and still want to enjoy better refactoring capabilities and reduce the benefits of code lines?
The following content is an introduction to XDoclet. I feel familiar with all the knowledge points, such as JDK 1.4 and 1.3... It is always good to learn more, haha.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.