-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: how to map a inherited classes as components
PostPosted: Sun Apr 01, 2007 1:04 pm 
Regular
Regular

Joined: Mon Oct 02, 2006 12:03 pm
Posts: 62
Hello,

I don't know how I map this situation:


I'm a customer that contains login, password, ... properties, and a Bank address component (BankAdress class) and a set of contact adress (ContactAdress). So, contactAdress and BankAdress inherits from Adress class.

I have chosse table per subClass strategy, so I have Adress Table that contains ID, State, Country, Street, ... BankAdress Table that contains ID (foreign key to ID field Adress Table), accountBank, ... and ContactAdress Table, that contains ID (foreign key to ID field Adress Table), contact Name, ...

I want to map properties as components or collection of components. But, if components are inhrited classes (table per subclass), I don't know how I can to map this class.

How I can map customer class? Thanks for all.

Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Model.Customer, ComponentsBroker" table="CUSTOMER">
      <id name="Id" column="ID" type="String">
         <generator class="assigned" />
      </id>
      <component class="Model.Name, ComponentsBroker" name="Name">
         <property name="First_name" column="FIRST_NAME" type="String"/>
         <property name="Second_name" column="SECOND_NAME" type="String"/>
      </component>
      <component class="Model.BankAddress, ComponentsBroker" name="Bank_address">
         ...
      </component>
      <set name="Contacts" .../>
         <component class="Model.BankAddress, ComponentsAddress">
            ...
         </component>
   </class>
</hibernate-mapping>



C# classes -->

Code:
using System;

namespace ComponentsBroker
{
   public class Address
   {
      
      private string id;
      private string number_street;
      private string street;
      private string city;
      private string state;

      #region Constructors

      protected Address()
      {
         this.id = null;
         this.number_street = "";
         this.street = "";
         this.city = "";
         this.state = "";
      }

      public Address(string number_street, string street, string city, string state)
      {
         this.number_street = number_street;
         this.street = street;
         this.city = city;
         this.state = state;
      }

      #endregion

      #region Properties

      public string Id
      {
         get { return this.id; }
      }

      public string State
      {
         get { return this.state; }
         set { this.state = value; }
      }

      public string Street
      {
         get { return this.street; }
         set { this.street = value; }
      }

      public string Number_street
      {
         get { return this.number_street; }
         set { this.number_street = value; }
      }

      public string City
      {
         get { return this.city; }
         set { this.city = value; }
      }

      #endregion
   }

   public class BankAddress : Address
   {

      private string bankName;
      private string account;

      #region Constructors

      protected BankAddress () : base()
      {
         this.bankName = "";
         this.account = "";
      }

      public BankAddress (string number_street, string street, string city, string state, string bankName, string account) : base(number_street, street, city, state)
      {
         this.bankName = bankName;
         this.account = account;
      }

      #endregion

      #region Properties

      public string Account
      {
         get { return this.account; }
         set { this.account = value; }
      }

      public string BankName
      {
         get { return this.bankName; }
         set { this.bankName = value; }
      }

      #endregion
   }

   public class ContactAddress : Address
   {

      private string contactName;
      private string phone;

      #region Constructors

      protected ContactAddress () : base()
      {
         this.contactName = "";
         this.phone = "";
      }

      public ContactAddress (string number_street, string street, string city, string state, string constactName, string phone) : base(number_street, street, city, state)
      {
         this.contactName = contactName;
         this.phone = phone;
      }

      #endregion

      #region Properties

      public string ContactName
      {
         get { return this.contactName; }
         set { this.contactName = value; }
      }

      public string Phone
      {
         get { return this.phone; }
         set { this.phone = value; }
      }

      #endregion
   }

public class Customer
   {

      private Model.Name name;
      private Model.BankAddress bank_address;
      private Iesi.Collections.HashedSet constacts;

      #region Constructors

      private Customer()
      {
         this.name = null;
         this.bank_address = null;
         this.constacts = null;
      }

      public Customer (Model.Name name)
      {
         this.name = name;
         this.bank_address = null;
         this.constacts = null;
      }

      #endregion

      #region Propietats

      public Model.Name Name
      {
         get { return this.name; }
         set { this.name = value; }
      }

      public Model.BankAddress Bank_address
      {
         get { return this.bank_address; }
         set { this.bank_address = value; }
      }

      public Iesi.Collections.HashedSet Constacts
      {
         get { return this.constacts; }
         set { this.constacts = value; }
      }

      public string Id
      {
         get { return this.id; }
      }

      #endregion
   }

public class Name
   {
      private string first_name;
      private string second_name;

      #region Constructors

      private Name()
      {
         this.first_name = "";
         this.second_name = "";
      }

      public Name(string first_name, string second_name)
      {
         this.first_name = first_name;
         this.second_name = second_name;
      }

      #endregion

      #region Properties

      public string Second_name
      {
         get { return this.second_name; }
         set { this.second_name = value; }
      }

      public string First_name
      {
         get { return this.first_name; }
         set { this.first_name = value; }
      }

      #endregion
   }
}


SQL DATABASE Script -->

Code:
CREATE TABLE ADDRESS (
  ID VARCHAR(8) NOT NULL,
  STREET VARCHAR(16) NULL,
  NUMBER_STREET INTEGER UNSIGNED NULL,
  CITY VARCHAR(32) NULL,
  STATE VARCHAR(32) NULL,
  PRIMARY KEY(ID)
);

CREATE TABLE BANK_ADDRESS (
  ADDRESS_ID VARCHAR(8) NOT NULL,
  BANK_NAME VARCHAR(16) NULL,
  ACCOUNT VARCHAR(64) NULL,
  PRIMARY KEY(ADDRESS_ID),
  FOREIGN KEY(ADDRESS_ID)
    REFERENCES ADDRESS(ID)
);

CREATE TABLE CUSTOMER (
  ID VARCHAR(8) NOT NULL,
  BANK_ADDRESS VARCHAR(8) NOT NULL,
  FIRST_NAME VARCHAR(16) NULL,
  SECOND_NAME VARCHAR(32) NULL,
  PRIMARY KEY(ID),
  FOREIGN KEY(BANK_ADDRESS)
    REFERENCES BANK_ADDRESS(ADDRESS_ID)
);

CREATE TABLE CONTACT_ADDRESS (
  ADDRESS_ID VARCHAR(8) NOT NULL,
  CUSTOMER_ID VARCHAR(8) NOT NULL,
  FIRST_NAME VARCHAR(16) NULL,
  SECOND_NAME VARCHAR(32) NULL,
  PHONE VARCHAR(32) NULL,
  PRIMARY KEY(ADDRESS_ID),
  FOREIGN KEY(ADDRESS_ID)
    REFERENCES ADDRESS(ID),
  FOREIGN KEY(CUSTOMER_ID)
    REFERENCES CUSTOMER(ID)
);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.