Inheritance: Table-Per-Class Hierarchy in Java

Creating PDF-417 2d barcode in Java Inheritance: Table-Per-Class Hierarchy

Inheritance: Table-Per-Class Hierarchy
PDF-417 2d Barcode Generator In Java
Using Barcode drawer for Java Control to generate, create PDF417 image in Java applications.
www.OnBarcode.com
PDF-417 2d Barcode Scanner In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Now it s time to put some of the concepts about mapping inheritance into practice. In the first example from the TechConf system, we will map the User, Attendee, and Presenter class hierarchy. As you can deduce from Figure 4-14, an Attendee and a Presenter are types of User.
Make UCC - 12 In Java
Using Barcode generator for Java Control to generate, create UCC.EAN - 128 image in Java applications.
www.OnBarcode.com
Generate Barcode In Java
Using Barcode creation for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
CHAPTER 4 OBJECT RELATIONAL MAPPING WITH HIBERNATE
Painting Code 39 Extended In Java
Using Barcode maker for Java Control to generate, create USS Code 39 image in Java applications.
www.OnBarcode.com
GS1 RSS Creation In Java
Using Barcode generation for Java Control to generate, create GS1 DataBar Expanded image in Java applications.
www.OnBarcode.com
Figure 4-14. User, Attendee, and Presenter class hierarchy Using the Table-Per-Class-Hierarchy strategy, we will use a single table named USER to hold the data for Users, Attendees, and Presenters. The SQL required to create this table is shown in Listing 4-24. Listing 4-24. SQL to Create the USER Table CREATE TABLE USER ( PK_ID INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, -- DISCRIMINATOR COLUMN USERTYPE VARCHAR(3) NOT NULL, PASSWORD VARCHAR(16) NOT NULL, FIRSTNAME VARCHAR(32) NOT NULL, LASTNAME VARCHAR(32) NOT NULL, EMAIL VARCHAR(64) NOT NULL, HOMEPHONE VARCHAR(12), WORKPHONE VARCHAR(12), FAX VARCHAR(12), FK_ADDRESS_ID INTEGER NOT NULL,
Creating UPC - 13 In Java
Using Barcode creation for Java Control to generate, create GS1 - 13 image in Java applications.
www.OnBarcode.com
Postnet Creator In Java
Using Barcode creator for Java Control to generate, create Postnet image in Java applications.
www.OnBarcode.com
CHAPTER 4 OBJECT RELATIONAL MAPPING WITH HIBERNATE
PDF 417 Reader In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
PDF417 Drawer In Java
Using Barcode generation for Android Control to generate, create PDF 417 image in Android applications.
www.OnBarcode.com
-- ATTENDEE FK_ATTENDEEGROUP_ID INTEGER, ALUMNUS BOOLEAN, -- PRESENTER BIO VARCHAR(255), COMPANY VARCHAR(64), COMPANYURL VARCHAR(64), FK_BLOGLINK_ID VARCHAR(64), CONSTRAINT USER_ADDRESS_FK FOREIGN KEY(FK_ADDRESS_ID) REFERENCES ADDRESS(PK_ID), CONSTRAINT UNIQUE_USER_EMAIL UNIQUE(EMAIL) ); Notice that the table has all of the fields required to store every element of the User-AttendeePresenter class hierarchy. Also notice that all of the fields for the subclasses are nullable. The column USERTYPE a VARCHAR(3) will serve as the discriminator column that Hibernate will use to determine the type of object that s stored in a given row. Now let s take a look at the HBM mapping file for the user class hierarchy shown in Listing 4-25. Listing 4-25. Table-Per-Class Hierarchy Mapping for User-Attendee-Presenter < xml version="1.0" > <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.integrallis.techconf.domain"> <class name="User" table="user" discriminator-value="USR"> <id name="Id" column="PK_ID" type="integer"> <generator class="identity" /> </id> <discriminator column="USERTYPE" length="3"/> <!-- User fields --> <property name="HomePhone" not-null="false" /> ... <many-to-one class="Address" name="Address" not-null="true" cascade="all" lazy="false" > <column name="FK_ADDRESS_ID" /> </many-to-one> ... <!-- Attendee Subclass --> <subclass name="Attendee" discriminator-value="ATD"> <property name="Alumnus" type="boolean" /> <many-to-one name="Group" class="AttendeeGroup">
UPC Code Decoder In Visual Basic .NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Encode GS1-128 In Java
Using Barcode generation for BIRT Control to generate, create USS-128 image in BIRT reports applications.
www.OnBarcode.com
CHAPTER 4 OBJECT RELATIONAL MAPPING WITH HIBERNATE
UCC-128 Printer In VS .NET
Using Barcode creation for VS .NET Control to generate, create EAN / UCC - 13 image in VS .NET applications.
www.OnBarcode.com
Encoding Code39 In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create Code 39 image in ASP.NET applications.
www.OnBarcode.com
<column name="FK_ATTENDEEGROUP_ID" /> </many-to-one> ... </subclass> <!-- Presenter Subclass --> <subclass name="Presenter" discriminator-value="PST"> <property name="Bio" /> <property name="Company" /> <property name="CompanyURL" /> ... </subclass> </class> </hibernate-mapping> As you can see, the subclass element is used to map the subclasses Attendee and Presenter. For User objects the discriminator column value is USR , for Attendee objects it is ATD , and for Presenter objects it is PST . Also, from this mapping we can see that Users can have an Address. I have omitted some sections of the mapping for clarity s sake. Now we can write a simple test to create some User, Attendee, and Presenter objects and persist them to the database. Listing 4-26 shows some example code to create the objects. Notice that I am creating the required Address objects for each of the User, Attendee, and Presenter objects. Listing 4-26. Creating and Persisting Some User, Attendee, and Presenter Objects File configFile = new File("hibernate.cfg.xml"); Configuration configuration = new Configuration().configure(configFile); SessionFactory factory = configuration.buildSessionFactory(); // create some addresses Address address1 = new Address("123 Main Street", "N/A", "Columbus", "OH", "43081"); Address address2 = new Address("456 Vine Street", "N/A", "Cincinnati", "OH", "45202"); Address address3 = new Address("1 Easy Street", "N/A", "Columbus", "GA", "31901"); // create a user User user = new User("Jim", "Smith", "jim@smith.com", "jimspassword"); user.setAddress(address1); // create an attendee Attendee attendee = new Attendee("Bob", "Smithers", "bsmithers@acme.com", "bobspassword"); attendee.setAlumnus(true); attendee.setAddress(address2);
EAN / UCC - 14 Drawer In None
Using Barcode creation for Office Word Control to generate, create GS1-128 image in Office Word applications.
www.OnBarcode.com
UPCA Creator In .NET
Using Barcode encoder for Reporting Service Control to generate, create UPC A image in Reporting Service applications.
www.OnBarcode.com
CHAPTER 4 OBJECT RELATIONAL MAPPING WITH HIBERNATE
Make QR Code 2d Barcode In .NET Framework
Using Barcode printer for Reporting Service Control to generate, create QR image in Reporting Service applications.
www.OnBarcode.com
QR Generator In None
Using Barcode generation for Software Control to generate, create QR-Code image in Software applications.
www.OnBarcode.com
// create a presenter Presenter presenter = new Presenter("Jackie", "Roberts", "jroberts@acme.com", "jackiespassword"); presenter.setBio("Jackie loves Java"); presenter.setCompany("Acme Java Corporation"); presenter.setCompanyURL("www.acme.com"); presenter.setAddress(address3); Session session = null; Transaction tx = null; try { session = factory.openSession(); tx = session.beginTransaction(); // persist the objects session.persist(user); session.persist(attendee); session.persist(presenter); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } } finally { session.close(); } Running the example shown in Listing 4-26 would produce the following SQL statements: Hibernate: insert into Address (StreetAddress, State, ZipCode, City, AptNumber) values ( , , , , ) Hibernate: insert into user (HOMEPHONE, PASSWORD, FAX, WORKPHONE, LASTNAME, FIRSTNAME, EMAIL, FK_ADDRESS_ID, USERTYPE) values ( , , , , , , , , 'USR') Hibernate: insert into Address (StreetAddress, State, ZipCode, City, AptNumber) values ( , , , , ) Hibernate: insert into user (HOMEPHONE, PASSWORD, FAX, WORKPHONE, LASTNAME, FIRSTNAME, EMAIL, FK_ADDRESS_ID, ALUMNUS, FK_ATTENDEEGROUP_ID, USERTYPE) values ( , , , , , , , , , , 'ATD') Hibernate: insert into Address (StreetAddress, State, ZipCode, City, AptNumber) values ( , , , , ) Hibernate: insert into user (HOMEPHONE, PASSWORD, FAX, WORKPHONE, LASTNAME, FIRSTNAME, EMAIL, FK_ADDRESS_ID, BIO, COMPANY, COMPANYURL, FK_BLOGLINK_ID, USERTYPE) values ( , , , , , , , , , , , , 'PST')
Create Barcode In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Drawing Code 3/9 In Java
Using Barcode printer for Android Control to generate, create ANSI/AIM Code 39 image in Android applications.
www.OnBarcode.com
Copyright © OnBarcode.com . All rights reserved.