Typed relation (XML only)

A tutorial reader has two addresses, a billing and a delivery address. Only these address types are allowed. Full source code is provided in the package: de.laliluna.relation.typed

Classes

Tables

images/typed_classes.jpg

images/typed_tables.jpg

  <class name="TutorialReader" table="ttutorialreader">
......... snip .........
    <many-to-one name="billingAddress" entity-name="BillingReaderAddress" cascade="all" lazy="false">
      <column name="billingaddress_fk"></column>
      <formula>'billing'</formula>
    </many-to-one>
    <many-to-one  name="deliveryAddress" entity-name="DeliveryReaderAddress"  cascade="all"  lazy="false">
      <column name="deliveryaddress_fk"></column>
      <formula>'delivery'</formula>
    </many-to-one>
  </class>

  <class entity-name="BillingReaderAddress" name="ReaderAddress"
   table="treaderaddress" where="type='billing'"
    check="type in('billing','delivery')">
    <composite-id name="id" class="ReaderAddressId">
      <key-property name="readerId" column="reader_id"/>
      <key-property name="type"/>
    </composite-id>
    <property name="address" type="string"></property>
    <property name="city" type="string"></property>
  </class>
  <class entity-name="DeliveryReaderAddress" name="ReaderAddress"
   table="treaderaddress" where="type='delivery'"
    check="type in('billing','delivery')">
    <composite-id name="id" class="ReaderAddressId">
      <key-property name="readerId" column="reader_id"/>
      <key-property name="type"/>
    </composite-id>
    <property name="address" type="string"></property>
    <property name="city" type="string"></property>
  </class>

The following tables are generated:

CREATE TABLE ttutorialreader
(
  id int4 NOT NULL,
  name varchar(255),
  billingaddress_fk int4,
  deliveryaddress_fk int4,
  CONSTRAINT ttutorialreader_pkey PRIMARY KEY (id)
) ;
CREATE TABLE treaderaddress
(
  reader_id int4 NOT NULL,
  "type" varchar(255) NOT NULL,
  address varchar(255),
  city varchar(255),
  CONSTRAINT treaderaddress_pkey PRIMARY KEY (reader_id, "type"),
  CONSTRAINT treaderaddress_type_check CHECK ("type"::text = 'billing'::text OR "type"::text = 'delivery'::text),
  CONSTRAINT treaderaddress_type_check1 CHECK ("type"::text = 'billing'::text OR "type"::text = 'delivery'::text)
)

Samples of use:

/* create and set relation */
TutorialReader reader = new TutorialReader();
reader.setName("Sebastian");
ReaderAddress billing = new ReaderAddress(new ReaderAddressId(reader
      .getId(), ReaderAddressId.BILLING), "Alte Landstrasse",
      "Frankfurt");
ReaderAddress delivery = new ReaderAddress(new ReaderAddressId(reader
      .getId(), ReaderAddressId.DELIVERY), "Neue Landstrasse",
      "Frankfurt");
reader.setBillingAddress(billing);
reader.setDeliveryAddress(delivery);
session.save(reader);

/* select all billingReaderAddresses */
      List list = session.createQuery("from BillingReaderAddress").list();

/* select tutorial reader with billing address in Bad Vilbel */
List list = session.createQuery("from TutorialReader r where r.billingAddress.city='Bad Vilbel' ").list();