-->
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.  [ 11 posts ] 
Author Message
 Post subject: composite-id with autogenerated field
PostPosted: Wed Aug 27, 2003 6:09 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
M'kay, here's the deal...

I'd like to create a data object that has a composite id based off a many to one relationship and a generated id.

I'm generating both the java and ddl from the mapping file. Here's a mapping I've tried, but it isn't generating the code or database structure I'd like. (Well, this one won't even autogenerate code that compiles. :P )

Code:
<class name="DataObject" table="base_data_objects" mutable="true">
   
        <id name="id" type="long">
            <generator class="native"/>
        </id>
       
        <joined-subclass name="SiteDataObject" table="base_site_data_objects">
           
            <composite-id
                name="site_composite"
                unsaved-value="any">

                <key-property name="id" type="long"/>
                <key-many-to-one name="site" class="Site"/>
       
            </composite-id>
           
            <key column="site_composite"/>

        </joined-subclass>

</class>


The SiteDataObject at the moment is a subclass of DataObject, but I can change this if necessary. My main goal is to have a composite key which contains an autogenerated id and many-to-one fields. However, the generated class will have several subclasses that need to share the composite key...

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 6:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
subclasses do not have ids. the <id> is inherited. I don't understand what you are trying to do.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 6:25 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
subclasses do not have ids. the <id> is inherited.

Dur...Should have realized that. :)

What I want to do, though, is have a subclass that modifies the id behavior of the superclass. That's not necessary, though. I can get by if a SiteDataObject is at the base of the object heirarchy.

I don't understand what you are trying to do.

Why ask why? :P

At my business we have several locations that generate data, then we aggregate it to a central location. The "Site" object stores site specific info, and each site database has only one site object. The central database will have a table with all Sites. When a SiteDataObject is created at a site, I want it identifiable by it's autogenerated id and site identifier.

So, is this possible?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 6:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You should remodel this as delegation.

What you really have here is a many-to-one association between the SiteData and the Data; ie. the *same* Data row is shared b/w multiple instances of SiteDate. Hibernate does not (and probably should not) let you represent the same row in multiple instances.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 7:12 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
the *same* Data row is shared b/w multiple instances of SiteDate

No, not really. Maybe a concrete example will help.

Each site generates events. These events are stored on the site database, then periodically copied to the central DB, where they all reside in one table. That's why I need a composite id. The autogenerated id works fine when the data is at the site, but may not be unique when copied to the central database.

Now my mapping file looks like this:

Code:
<class name="SiteDataObject" table="base_site_data_objects">
        <composite-id name="id" unsaved-value="any" class="CompositeSiteID">
            <key-property name="generated_id" type="long"/>
            <key-many-to-one name="site" class="Site"/>
        </composite-id>

        <joined-subclass name="Event" table="events">
            <key ...>
        </joined-subclass>

</class>


My question now is, what should I put in the <key> tag in the joined-subclass?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 8:29 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Hmmm... I just tried to post, but it was eaten. Here we go again...

I wrote some <key> XML that fits with the DTD, but when I run SchemaExport I get an exception.

Here's the mapping:

Code:
<class name="SiteDataObject" table="base_site_data_objects">
        <composite-id name="id" unsaved-value="any" class="CompositeSiteID">
            <key-property name="generated_id" type="long"/>
            <key-many-to-one name="site" class="Site"/>
        </composite-id>

        <joined-subclass name="Event" table="events">
            <key><key column="generated_id"/><key column="site"/></key>
        </joined-subclass>

</class>


When I run SchemaExport I get this exception:

Code:
Error creating schema Foreign key must have same number of columns as referenced primary key
net.sf.hibernate.MappingException: Foreign key must have same number of columns as referenced primary key
    at net.sf.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:33)
    at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:523)
    at net.sf.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:352)
    at net.sf.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:51)
    at net.sf.hibernate.tool.hbm2ddl.SchemaExport.main(SchemaExport.java:293)


Any ideas? Is my <key> XML right? Since the exception is spawned from mapping.ForeignKey I tend to think not, but the DTD allows that XML...

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 8:44 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
OK, I know I'm replying to myself a lot, but I keep pounding at it. :)

I replaced the <key> mapping with one that still fits with the DTD but makes more sense. Same exception, though. :(

Mapping:
Code:
<key><column name="generated_id"/><column name="site"/></key>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 12:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This last one should work (the previous does NOT, actually, fit the DTD!). You should have the same number of <column>s in the <key> as are in the <id> of the superclass.

What is the mapping for Site?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 1:07 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
This last one should work (the previous does NOT, actually, fit the DTD!)

Well, it gets past the XML parser without throwing an exception. Really. Try for yourself. hibernate 2.0.3...

Anyway, I've fixed my mapping problem. I had some <many-to-one> and <set> tags in subclasses that I hadn't changed the <key> tags on. Silly me. :)

One last question remains: How do I create <generator> like functionality for my <composite-id>? Can I do that in the mapping, or do I have to create a custom type for the composite-id?

I know this isn't there now, but it would be nice if you could have a <generator> embedded in a <key-property> tag.

Anyway, here's my mapping, with some classes with the sets and many-to-one tags I was talking about above: (properties edited out for brevity...)

Code:
<class name="SiteDataObject" table="base_site_data_objects">
    <composite-id name="id" unsaved-value="any" class="CompositeSiteID">
        <key-property name="generated_id" type="long"/>
        <key-many-to-one name="site" class="Site"/>
    </composite-id>
    <joined-subclass name="Event" table="events">
        <key><key column="generated_id"/><key column="site"/></key>
    </joined-subclass> <!-- Event -->
    <joined-subclass name="Listing" table="listings">
            <key><column name="generated_id"/><column name="site"/></key>
    </joined-subclass> <!-- Listing -->
    <joined-subclass name="MenuItem" table="menu_items">
        <key><column name="generated_id"/><column name="site"/></key>
        <many-to-one name="parent" class="MenuHeirarchyItem">
            <column name="parent_id" unique-key="label-parent"/>
            <column name="parent_site" unique-key="label-parent"/>
        </many-to-one>
        <joined-subclass name="MenuActionItem" table="menu_actions">
            <key><column name="generated_id"/><column name="site"/></key>
            <many-to-one name="listing" outer-join="false" class="Listing">
                <column name="listing_id"/>
                <column name="listing_site"/>
            </many-to-one>
        </joined-subclass> <!-- MenuActionItem -->
        <joined-subclass name="MenuHeirarchyItem" table="menu_heirarchy">
            <key><column name="generated_id"/><column name="site"/></key>
            <set name="children" lazy="true" inverse="true">
                <key><column name="parent_id"/><column name="parent_site"/></key>
                <one-to-many class="MenuItem"/>
            </set>
        </joined-subclass> <!-- MenuHeirarchyItem --> 
    </joined-subclass> <!-- MenuItem --> 
</class> <!-- SiteDataObject -->
<class name="Site" mutable="true" table="sites_sites">
    <id name="id" type="long">
        <generator class="native"/>
    </id>
</class><!-- Site -->


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2003 4:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Well, it gets past the XML parser without throwing an exception. Really. Try for yourself. hibernate 2.0.3...



No it doesn't. It prints a WARN (ERROR?) to your console.

Quote:
do I have to create a custom type for the composite-id?


Yes. There is a page on the wiki about this.


Top
 Profile  
 
 Post subject: I have the same problem !!
PostPosted: Wed Mar 07, 2007 5:09 am 
Newbie

Joined: Sat Mar 03, 2007 3:14 am
Posts: 16
I need a UserType composite-id .. that generate one of the ids ..
Where can i find info about that .. i search in the wiki but i m not find nothing !!


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