-->
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.  [ 5 posts ] 
Author Message
 Post subject: Humor Me
PostPosted: Thu Aug 04, 2005 7:55 pm 
Newbie

Joined: Thu Aug 04, 2005 7:43 pm
Posts: 3
Location: Los Angeles, CA
Here's a class called Poc.hbm.xml (which means Point of Contact):

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="com.apvio.sequim.hibernate">

<!-- Point of Contact (Super class of Email, Phone, Fax, etc.). -->
<class name="Poc" table="POC">

  <id name="id" column="ID" type="int">
   <generator class="native" />
  </id>

  <many-to-one name="company" class="com.apvio.sequim.hibernate.Company" column="COMPANY_ID" not-null="true" />

  <property name="description" column="DESCRIPTION" type="string" not-null="true">
   <meta attribute="use-in-tostring">true</meta>
  </property>

  <joined-subclass name="Email" table="EMAIL">
   <key column="POC_ID" />
   <property name="email" column="EMAIL" type="string" not-null="false" />
   <property name="permitMarketing" column="PERMIT_MARKETING" type="boolean" not-null="true" />
   <joined-subclass name="PrimaryEmail" table="PRIMARY_EMAIL">
    <key column="POC_ID" />
    <many-to-one name="uniqueCompany" class="com.apvio.sequim.hibernate.Company" column="COMPANY_ID" unique="true" not-null="true" />
   </joined-subclass>
  </joined-subclass>

  <joined-subclass name="Phone" table="PHONE">
   <key column="POC_ID" />
   <property name="phone" column="PHONE" type="string" not-null="false" />
   <property name="permitMarketing" column="PERMIT_MARKETING" type="boolean" not-null="true" />
   <joined-subclass name="PrimaryPhone" table="PRIMARY_PHONE">
    <key column="POC_ID" />
    <many-to-one name="uniqueCompany" class="com.apvio.sequim.hibernate.Company" column="COMPANY_ID" unique="true" not-null="true" />
   </joined-subclass>
  </joined-subclass>

  <joined-subclass name="Fax" table="FAX">
   <key column="POC_ID" />
   <property name="fax" column="FAX" type="string" not-null="false" />
   <property name="permitMarketing" column="PERMIT_MARKETING" type="boolean" not-null="true" />
   <joined-subclass name="PrimaryFax" table="PRIMARY_FAX">
    <key column="POC_ID" />
    <many-to-one name="uniqueCompany" class="com.apvio.sequim.hibernate.Company" column="COMPANY_ID" unique="true" not-null="true" />
   </joined-subclass>
  </joined-subclass>

  <joined-subclass name="Address" table="ADDRESS">
   <key column="POC_ID" />
   <property name="address1" column="ADDRESS1" type="string" not-null="false" />
   <property name="address2" column="ADDRESS2" type="string" not-null="false" />
   <property name="city" column="CITY" type="string" not-null="false" />
   <property name="state" column="STATE" type="string" not-null="false" />
   <property name="zip" column="ZIP" type="string" not-null="false" />
   <property name="country" column="COUNTRY" type="string" not-null="false" />
   <property name="permitMarketing" column="PERMIT_MARKETING" type="boolean" not-null="true" />
   <joined-subclass name="PrimaryAddress" table="PRIMARY_ADDRESS">
    <key column="POC_ID" />
    <many-to-one name="uniqueCompany" class="com.apvio.sequim.hibernate.Company" column="COMPANY_ID" unique="true" not-null="true" />
   </joined-subclass>
  </joined-subclass>

  <joined-subclass name="Url" table="URL">
   <key column="POC_ID" />
   <property name="url" column="URL" type="string" not-null="false" />
   <joined-subclass name="PrimaryUrl" table="PRIMARY_URL">
    <key column="POC_ID" />
    <many-to-one name="uniqueCompany" class="com.apvio.sequim.hibernate.Company" column="COMPANY_ID" unique="true" not-null="true" />
   </joined-subclass>
  </joined-subclass>

</class>

<query name="com.apvio.sequim.hibernate.pocByDescription">
  <![CDATA[
  from com.apvio.sequim.hibernate.Poc as poc
  where upper(poc.description) = upper(:description)
  ]]>
</query>

</hibernate-mapping>


And here's how I use it in code:

Code:
      if ( isEmailPrimary() ) {
        email = new PrimaryEmail(getCompanyName() + "'s Primary E-mail Address", company, getEmail(), isEmailSpamable(), company) ;
      } else {
        email = new Email(getCompanyName() + "'s E-mail Address", company, getEmail(), isEmailSpamable()) ;       
      }


This works great, except I have to mention "company" twice in the constructor.

Am I just an idiot for trying something as overly complicated as this? Or is there actually a way to let Hibernate know what it is I'm attempting to do here?

_________________
%s


Top
 Profile  
 
 Post subject: Re: Humor Me
PostPosted: Thu Aug 04, 2005 8:04 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
inertia186 wrote:

Code:
      if ( isEmailPrimary() ) {
        email = new PrimaryEmail(getCompanyName() + "'s Primary E-mail Address", company, getEmail(), isEmailSpamable(), company) ;
      } else {
        email = new Email(getCompanyName() + "'s E-mail Address", company, getEmail(), isEmailSpamable()) ;       
      }


This works great, except I have to mention "company" twice in the constructor.

Am I just an idiot for trying something as overly complicated as this? Or is there actually a way to let Hibernate know what it is I'm attempting to do here?


What does Hibernate have to do with how your constructor functions ?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: Humor Me
PostPosted: Thu Aug 04, 2005 8:06 pm 
Newbie

Joined: Thu Aug 04, 2005 7:43 pm
Posts: 3
Location: Los Angeles, CA
Quote:
What does Hibernate have to do with how your constructor functions ?


What indeed. Hibernate hbm2java created these constructors.

_________________
%s


Top
 Profile  
 
 Post subject: Re: Humor Me
PostPosted: Thu Aug 04, 2005 8:10 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
inertia186 wrote:
Quote:
What does Hibernate have to do with how your constructor functions ?


What indeed. Hibernate hbm2java created these constructors.


It's just like any other code generator. It might not know exactly what you're trying to do and usually only get's you 95% of where you want to be.

Here's a question for you. Why do you have two many-to-one properties mapped to the same column/Object ?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: Humor Me
PostPosted: Thu Aug 04, 2005 8:18 pm 
Newbie

Joined: Thu Aug 04, 2005 7:43 pm
Posts: 3
Location: Los Angeles, CA
pksiv wrote:
Here's a question for you. Why do you have two many-to-one properties mapped to the same column/Object ?


The reason why there are two many-to-one properties is because it's ok to have multiple Email instances for one company, but it's not ok to have multiple PrimaryEmail instances for one company.

If there's a better way to enforce this constraint, I'd love to use it. Like I said, there's probably a more sane way to do this.

_________________
%s


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

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.