1、ll describe that process in this article.)3. Copy the XML configuration files to your applications CLASSPATH. Youll notice that you dont have to modify any Java objects to support the framework. Imagine, for instance, that you needed to somehow change a database table that your Java application used
2、 - by renaming a column, for example. Once youd changed the table, all youd have to do to update your Java application would be to update the appropriate XML configuration file. You wouldnt need to recompile any Java code. Hibernate Query Language (HQL)Hibernate provides a query language called Hibe
3、rnate Query Language (HQL), which is quite similar SQL. For those of you who prefer good old-fashioned SQL queries, Hibernate still gives you the opportunity to use them. But our supporting example will use HQL exclusively. HQL is quite simple to use. You will find all the familiar keywords you know
4、 from SQL, like SELECT, FROM, and WHERE. HQL differs from SQL in that you dont write queries directly on your data model (that is, on your tables, columns, etc.), but rather on you Java objects, using their properties and relationships. Listing 1 illustrates a basic example. This HQL code retrieves
5、all Individuals whose firstName is John. Listing 1. Basic HQL QuerySELECT * FROM eg.hibernate.mapping.dataobject.Individual WHERE firstName = JohnYou can refer to the HQL reference material on the Hibernate Website if you want to learn more about HQL syntax. XML configuration filesThe core of Hibern
6、ates functionality resides inside XML configuration files. These files must reside in your applications CLASSPATH. We placed them in the config directory of our sample code package The first file that well examine is hibernate.cfg.xml. It contains information regarding your data source (DB URL, sche
7、ma name, username, password, etc.) and references to other configuration files that will contain your mapping information. The remaining XML files allow you to map Java classes against database tables. We will take a closer look at those files later, but it is important to know that their filenames
8、follow the pattern ClassName.hbm.xml. Our supporting exampleIn this article, well examine a basic example that illustrates how Hibernate works and puts to good use three different strategies under which you can use Hibernate to do your object-relational mapping. Our example application will be used
9、by an insurance company that must keep legal records of all the property rights that its customers are insured for. Weve provided the full source code with this article; this code provides basic functionality from which you could build a full-fledged application, such as a Web or Swing application.
10、Our example assumes a classic use case for this kind of application. The user would provide the search criteria for any type of customer (individual, corporation, government agency, etc.), and would then be presented with a list of all customers matching the specified criteria - even if these are di
11、fferent types of customers. The user could access a more detailed view of a specific customer from that same list. In our application, a property right is represented by the Right class. A Right can either be a Lease or a Property. A Right is owned by a customer. To represent our customer, well use
12、the generic class Person. A Person can either be an Individual or a Corporation. Of course, the insurance company must know the Estates to which those Rights are assigned. An Estate is a very generic term, youll agree. So, well use the Land and Building classes to give our developers more comprehens
13、ive objects to work with. From this abstract, we can develop the class model shown in Figure 1:Figure 1. Complete Class ModelOur database model was designed to cover the three different strategies well discuss in this article. For the Right hierarchy, well use a single table (TB_RIGHT) and map to th
14、e correct class using the DISCRIMINATOR column. For the Person hierarchy, well use what we call a super table (TB_PERSON) that will share the same IDs with two other tables (TB_CORPORATION and TB_INDIVIDUAL). The third hierarchy (Estate) uses two different tables (TB_BUILDING and TB_LAND) linked by
15、a foreign key defined by the combination of two columns (REF_ESTATE_ID and REF_ESTATE_TYPE). Figure 2. Complete data modelSetting up the databaseHibernate supports a wide variety of RDBMSs, and any of them should work with our sample. However, the sample code and the text of this article have been t
16、ailored for HSQLDB, a fully functional relational database written entirely in the Java language. In the sql directory of the sample code package, you will find a file named datamodel.sql. This SQL script will create the data model used in our example. Setting up the Java projectAlthough you can alw
17、ays build and execute the sample code using the command line, you may want to consider setting up the project in an IDE for better integration. Within the sample code package, youll find the following directories: config, which contains all the samples XML configuration files (mapping, Log4J, etc.)
18、data, which contains configuration files used by HSQLDB. You will also find a batch file named startHSQLDB.bat that you may use to launch the database. src, contains all the samples source code. Be sure to copy the required Java libraries and XML configuration files to you applications CLASSPATH. Th
19、e code needs only the Hibernate and HSQLDB libraries in order to compile and run properly.Strategy 1: One table per subclass (Persons)In our first strategy, well look at how to map our Person hierarchy. Youll notice that the data model is very close to our class model. As a result, well use a differ
20、ent table for each class in the hierarchy, but all these tables must share the same primary key (well explain that in more detail momentarily). Hibernate will then use this primary key when it inserts new records into the database. It will also make use of this same primary key to perform JOIN opera
21、tions when accessing the database.Now we need to map our object hierarchy to our table model. We have three tables (TB_PERSON, TB_INDIVIDUAL, and TB_CORPORATION). As we mentioned above, they all have a column named ID as a primary key. Its not mandatory to have a shared column name like this, but it
22、 is considered good practice - and it makes it a lot easier to read the generated SQL queries. In the XML mapping file, shown in Listing 2, youll notice that the two concrete classes are declared as inside the Person mapping definition. The XML element is mapped to the primary key for the top-level
23、table TB_PERSON, while the elements (from each subclass) are mapped to the matching primary keys of the TB_INDIVIDUAL and TB_CORPORATION tables.Listing 2. Person.hbm.xml!DOCTYPE hibernate-mapping PUBLIC -/Hibernate/Hibernate Mapping DTD 2.0/EN hibernate-mapping /idset name=rights lazy=falsekey colum
24、n=REF_PERSON_IDone-to-many class=eg.hibernate.mapping.dataobject.Right /setjoined-subclass name=eg.hibernate.mapping.dataobject.IndividualTB_INDIVIDUALproperty name=firstNameFIRST_NAME type=java.lang.StringlastNameLAST_NAME/joined-subclasseg.hibernate.mapping.dataobject.CorporationTB_CORPORATIONname
25、NAMEstringregistrationNumberREGISTRATION_NUMBER/class/hibernate-mappingSaving a new instance of Individual form our Java code with Hibernate is pretty straightforward, as shown in Listing 3:Listing 3. Saving a new instance of Individualpublic Object create(Object object) Session session = null; try session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.save(object); session.fl
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1