- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
vb.net generate 2d barcode OBJECT RELATIONAL MAPPING WITH HIBERNATE in Java
CHAPTER 4 OBJECT RELATIONAL MAPPING WITH HIBERNATE Making PDF-417 2d Barcode In Java Using Barcode creation for Java Control to generate, create PDF417 image in Java applications. www.OnBarcode.comReading PDF-417 2d Barcode In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comTable 4-1. Hibernate J2SE Application Dependencies
Barcode Creator In Java Using Barcode creator for Java Control to generate, create Barcode image in Java applications. www.OnBarcode.comQR Code JIS X 0510 Encoder In Java Using Barcode drawer for Java Control to generate, create QR Code ISO/IEC18004 image in Java applications. www.OnBarcode.comFile Name
Barcode Generation In Java Using Barcode printer for Java Control to generate, create Barcode image in Java applications. www.OnBarcode.comMake Code 39 In Java Using Barcode creator for Java Control to generate, create Code 39 image in Java applications. www.OnBarcode.comcommons-logging-1.0.4.jar commons-lang-2.0.jar mysql-connector-java-3.0.14production-bin.jar
UPC Symbol Generator In Java Using Barcode encoder for Java Control to generate, create Universal Product Code version A image in Java applications. www.OnBarcode.comCode 9/3 Maker In Java Using Barcode creator for Java Control to generate, create USS Code 93, USS 93 image in Java applications. www.OnBarcode.comDescription
Painting PDF 417 In .NET Using Barcode creation for .NET Control to generate, create PDF 417 image in .NET applications. www.OnBarcode.comPrint PDF-417 2d Barcode In Java Using Barcode generator for Java Control to generate, create PDF 417 image in Java applications. www.OnBarcode.comJakarta commons used as a thin logging wrapper Used to simplify the POJO s implementation The JDBC driver used with MySQL Barcode Recognizer In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comMaking Barcode In .NET Using Barcode encoder for VS .NET Control to generate, create Barcode image in VS .NET applications. www.OnBarcode.com* Jar names and versions will vary from distribution to distribution.
Barcode Drawer In None Using Barcode creation for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comRecognize ANSI/AIM Code 39 In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comEclipse Setup
Encoding Code 128C In Java Using Barcode creation for BIRT Control to generate, create Code 128A image in Eclipse BIRT applications. www.OnBarcode.comPDF 417 Recognizer In .NET Framework Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comIf you are using Eclipse and you ve followed the steps in 2, in order for the examples in this chapter to work you ll have to add the Hibernate JARs to the TechConf project build path. To do so, go to the Project Properties page by selecting the project node in the Eclipse Package Explorer or Navigator, select Properties (by right-clicking on the node or via the main menu under Project Properties) and select Java Build Path. Click the Add Jars button to navigate to the lib directory and select the JARs shown in Figure 4-11. Reading Barcode In Java Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications. www.OnBarcode.comCode128 Drawer In Visual Studio .NET Using Barcode encoder for Visual Studio .NET Control to generate, create Code 128 Code Set B image in VS .NET applications. www.OnBarcode.comFigure 4-11. Adding Hibernate JARs to the Eclipse project
Recognize UPC Symbol In Visual Basic .NET Using Barcode recognizer for .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comRecognize Barcode In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comCHAPTER 4 OBJECT RELATIONAL MAPPING WITH HIBERNATE
Database Setup
For the simple examples in this chapter you ll need to set up a MySQL instance. I used version 4.1.7-nt on a Windows XP machine. There are several tutorials online and plenty of books that can help you setup MySQL, including The Definitive Guide to MySQL 5 by Michael Kofler (Apress, 2005) and Pro MySQL by Michael Kruckenberg and Jay Pipes (Apress, 2005). For JDBC connectivity I used the MySQL Connector/J 3.X JDBC Driver that is available from the MySQL website at http://dev.mysql.com. Quick Start
We will create our first POJO, Hibernate mapping, and database table completely by hand so that you get an idea of what it takes to map an object to a table. In subsequent examples you might be able to use tools suited for your development style to streamline the process. POJO Sample Code
The simple Address POJO follows the basic well-known JavaBean conventions, private fields exposed via getters and setters. That s the easy part; of paramount importance to how Hibernate manipulates persistent objects is the implementation of the equals and hashCode methods. The implementation of these methods is not a Hibernate-specific requirement, but it should be part of any Java business object. In their book Hibernate in Action (Manning, 2005), Bauer and King go the distance explaining the difference between object equality, object identity, and database identity. They promote the use of business key equality when implementing the equals method by using a business property or set of properties that are unique to a table row. In the case of the Address POJO the combination of StreetAddress, State, ZipCode, City and AptNumber fields identify a unique address as shown in Listing 4-1. Listing 4-1. Address POJO package com.integrallis.TechConf.domain; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; public class Address implements Serializable { // primary key private Integer id; // fields private String private String private String private String private String streetAddress; state; zipCode; city; aptNumber; // constructors public Address () {} CHAPTER 4 OBJECT RELATIONAL MAPPING WITH HIBERNATE
// getters and setters /* Implementation of equals using Business Key Equality * * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals (Object object) { // short circuits if (object == null) return false; if (this == object) return true; if (!(object instanceof Address)) return false; final Address address = (Address) object; //NOTE always use getters on the passed object since it // might be a Hibernate Proxy return new EqualsBuilder(). append(streetAddress, address.getStreetAddress()). append(aptNumber, address.getAptNumber()). append(city, address.getCity()). append(state, address.getState()). append(zipCode, address.getZipCode()). isEquals(); } public int hashCode () { // pick a hard-coded, randomly chosen, non-zero, odd number // ideally different for each class return new HashCodeBuilder(17, 37). append(streetAddress). append(aptNumber). append(city). append(state). append(zipCode). toHashCode(); } public String toString () { return new ToStringBuilder(this). append("streetAddress", streetAddress). append("aptNumber", aptNumber). append("city", city). append("state", state). append("zipCode", zipCode). toString(); } } CHAPTER 4 OBJECT RELATIONAL MAPPING WITH HIBERNATE
Tip I use the Jakarta Commons Lang utility to implement the equals, hashCode and toString methods.
Besides making a lot of the mundane coding simpler, they provide a clean and (most of the time) efficient way to deal with these three important methods of the Object class. The builders are EqualsBuilder, HashCodeBuilder and ToStringBuilder respectively, and they are located in the org.apache.commons. lang.builder package. The Jakarta Commons libraries can be found at http://jakarta.apache.org/ commons/. For a complete coverage of the available libraries I recommend Harshad Oak s Pro Jakarta Commons (Apress, 2004). Technically you need to implement only equals and hashCode for your POJOs if they are going to be used as part of a composite primary key class or if an instance of that class loaded in a different session needs to be compared (either directly or by being in an ordered collection).
|
|