-->
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.  [ 15 posts ] 
Author Message
 Post subject: The trouble with <component>
PostPosted: Thu Mar 18, 2004 12:15 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
i need to define two attributes of a class to point to the same <component>.

Code:
<class name="Employee" table="employees">
        <id name="id">
            <generator class="sequence">
                <param name="sequence">employee_id_seq</param>
            </generator>
        </id>
        <property name="taxfileNumber"/>

        <component name="name" class="Name">
            <property name="firstName"/>
            <property name="initial"/>
            <property name="lastName"/>
        </component>

        <component name="motherName" class="Name">
            <property name="firstName"/>
            <property name="initial"/>
            <property name="lastName"/>
        </component>
</class>



not surprisingly this causes causes :
[STDERR] net.sf.hibernate.MappingException: Repeated column in mapping for class com.alterpoint.aof.objects.platform.impl.ManagedElementImpl should be mapped with insert="false" update="false": firstName

any solutions for this (or is it a usage issue..)

thanks
ravi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 1:03 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
tell us what do exactly want to do ?
is there an association between name and mother?
you want to do Employee.getName().getFirstName()

AND Employee.getMotherName().getFirstName()

both representing exactly the data? (row/column)
or is there a link on the same table?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 1:19 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
this should make it more obvious. the employee class looks like this
Code:
Employee {

     Name motherName;
     Name name;

     public Name getName() {
          return name;
     }

     public Name getMotherName() {
          return motherName;
     }
}

essentially there will be a set of fields (3) in the database for each of the name attributes. so both attribute DONOT represent the same columns in the database.

yes, i do want to be able to do
Code:
Employee.getName().getFirstName()
Employee.getMotherName().getFirstName()

which returns two different name objects.

thnx


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 3:34 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
can you give employees table details?

i think you need two many to one (one for name, one for mother), not two components...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 4:19 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
it's not that complicated.

if you look at the manual under section 18.1 my example is just an extension to it.
Code:
http://www.hibernate.org/hib_docs/reference/html_single/#examples-s0


all i'm trying to do is add another field to the employee class as described above. so there's no scope for a many to one association.

thnx


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 4:45 pm 
Newbie

Joined: Wed Aug 27, 2003 11:23 am
Posts: 4
The error is telling you that you have two properties mapped to the same column. This is occurring because the default mapping of a Property to a column is to use the proprty name as the column name (http://www.hibernate.org/hib_docs/reference/html_single/#or-mapping-s1-7). So you need to specify a column name for the Properties in either your component mapping for name="name" or name="motherName".

Your table "employees" has to have seperate fields for the Employee's firstname and the Mother's firstname to use a component.

HTH

Mike

ravi wrote:
it's not that complicated.

if you look at the manual under section 18.1 my example is just an extension to it.
Code:
http://www.hibernate.org/hib_docs/reference/html_single/#examples-s0


all i'm trying to do is add another field to the employee class as described above. so there's no scope for a many to one association.

thnx


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 5:00 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
yes, i agree that the source of the problem is obviously the conflicting property names (which are also the column names).

i was looking for a more hands-off solution, since i generate the xmls from tags as below:

Code:
Employee {

     Name motherName;
     Name name;

     /**
      * @hibernate.component
      *
     public Name getName() {
          return name;
     }

     /**
      * @hibernate.component
      *
     public Name getMotherName() {
          return motherName;
     }
}


and the Name

Code:
Name {
     String firstName;
     String midName;
     String lastName;

     /**
      * @hibernate.property
      *
      public String getFirstName()
      {
            return firstName;
      }

     /**
      * @hibernate.property
      *
      public String getMiddleName()
      {
            return midName;
      }

     /**
      * @hibernate.property
      *
      public String getLastName()
      {
            return lastName;
      }
}


the generated xml is what i had posted earlier. there is no way to tell the generator to change the property name="firstName" if it is referred to twice in the same class. a workaround for this is what i'm looking for, the last resort being hand-editing the generated mapping xml

i guess i steered the question partly into the XDoclet lane.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 5:18 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
in fact the columns are not the same aren't they?

but the problem may be generation from xdoclet as you said.

I had another limitation about xdoclet, we can try to add patchs... but in your case it looks it's going to be hard :-/


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 03, 2004 1:20 pm 
Newbie

Joined: Thu Oct 09, 2003 3:44 pm
Posts: 18
The solution is to add a new tag option
Code:
@hibernate.component prefix="xxx"


so your example could then become
[code]
Employee {



Top
 Profile  
 
 Post subject: Re: The trouble with <component>
PostPosted: Sat Apr 03, 2004 4:44 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
ravi wrote:
i need to define two attributes of a class to point to the same <component>.


To different instances of the same component class - that's what you mean ?

By default Hibernate will use the property name for the column - causing the clash in your example... Why don't you just override this default behavior by specifing the column name yourself ?

Code:
<class name="Employee" table="employees">
        <component name="name" class="Name">
            <property name="firstName">
                 <column name="firstName" />
            </property>
         ...
        </component>

        <component name="motherName" class="Name">
            <property name="firstName">
                 <column name="motherFirstName" />
            </property>
         ...
        </component>
</class>

[/quote]


Top
 Profile  
 
 Post subject: fine but how to do it with xdoclet?
PostPosted: Sun Apr 04, 2004 3:11 pm 
Newbie

Joined: Thu Oct 09, 2003 3:44 pm
Posts: 18
that's all well and good, and in fact exactly how i was doing it, but xdoclet does not allow such flexibility

my suggestion was an enhancement to the xdoclet tags, not hibernate per se


Top
 Profile  
 
 Post subject: Re: fine but how to do it with xdoclet?
PostPosted: Sun Apr 04, 2004 3:17 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
davesag wrote:
my suggestion was an enhancement to the xdoclet tags, not hibernate per se


Misunderstood your question then - sorry about that.


Top
 Profile  
 
 Post subject: The full solution
PostPosted: Thu Apr 15, 2004 8:18 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
just an update for the code-gen enthusiasts.

i attempted to find a solution to this problem today afternoon and i'm very close to completing the .xdt to create unique fieldnames based on an extra attribute specified with the component tag.

the challenge so far has been that we want to specify the tag on the @hibernate.compoent tag BUT want it to manifest itself in the .property tag. i had to look at the programmatic power of the xdoclet template language so it took a little longer than i thought.

stay tuned


Top
 Profile  
 
 Post subject: Solved
PostPosted: Mon May 03, 2004 2:57 am 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
having done some tweaks to the .xdt i got my solution working but then also found an existing fix that i decided to stick to.

you can implement the fix at the the Xdoclet JIRA for this same issue
http://opensource.atlassian.com/projects/xdoclet/browse/XDT-733

there's an improvement to the solution suggested which you can easily implement yourself.

enjoy
ravi


Top
 Profile  
 
 Post subject: Re: Solved
PostPosted: Wed Aug 11, 2004 2:34 pm 
Newbie

Joined: Wed Oct 15, 2003 11:16 am
Posts: 6
ravi wrote:
having done some tweaks to the .xdt i got my solution working but then also found an existing fix that i decided to stick to.

you can implement the fix at the the Xdoclet JIRA for this same issue
http://opensource.atlassian.com/projects/xdoclet/browse/XDT-733

there's an improvement to the solution suggested which you can easily implement yourself.

enjoy
ravi


Thanks for the fix, Ravi. I'm using Xdoclet 1.2.1, but I am not getting the behavior descrbed with 'prefix'. I examined the .xdt and java source and see that the fix as described in Jira has been implemented in this version.

I've boiled it down to a simple example of a Person entity with two Address components, one for 'home' and one for 'billing' to illustrate.

Adding a 'prefix' attribute to the @hibernate.component tag does not result in a prefixed column attribute for the component's properties.

Can anyone confirm that XDoclet 1.2.1 accurately implements the 'prefix' additon to @hibernate.component ?

Here are the relevant snippets of the xdoclet tags and resulting mapping:

Code:
public class Person {
    private Address billingAddress;
    private Address homeAddress;

    /** hibernate.component prefix="billing_" */
    public getBillingAddress() {
        return billingAddress;
    }

    /** hibernate.component prefix="home_" */
    public getHomeAddress() {
        return homeAddress;
    }
}



Code:
<component
            name="billingAddress"
            class="Address"
        >
        <property
            name="address1"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="address1"   <!-- Expecting billing_address -->
        />

        <property
            name="address2"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="address2"
        />

        <property
            name="city"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="city"
        />
  ...
[/img]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 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.