-->
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.  [ 2 posts ] 
Author Message
 Post subject: Association table with extra columns
PostPosted: Thu Mar 29, 2007 6:34 pm 
Newbie

Joined: Thu Mar 29, 2007 5:57 pm
Posts: 2
I have a many-to-many association between two tables, but the association table has some extra columns (apart from the foreign keys).

This is how I would do it with XML mappings,

-------------------------------------------------------------------------
-- Test.hbm.xml --

...

<set name="TestProperties">
<key column="testID"/>
<composite-element class="TestProperty">
<property name="value" type="string" not-null="true"/>
<many-to-one name="type" class="PropertyType" not-null="true"/>
</composite-element>
</set>

...

--------------------------------------------------------------------------

but, I want to do it with Hibernate annotations. I think I should be able to define the TestProperty object with the extra data that I need and then have annotations in Test and PropertyType (plus possibly some more in TestProperty?) that will cause Hibernate to generate a TestProperty table that has a composite (Test's FK and PropertyType's FK) along with my extra columns. Can someone help? The code below is, of course, not complete. I was just hoping to present the code base and have someone help me annotate it.


@Entity
class Test
{
long id;
Set<TestProperty> properties = new Set<TestProperty>();

@Id
long getID() { return id; }

@OneToMany
Set<TestProperty> getProperties() { return properties; }

}

@Entity
class PropertyType
{
long id;
String name;
String metadata;

@Id
long getID() { return id; }

String getName() { return name; }
String getMetadata() { return metadata; }
}

// This is my association table
class TestProperty
{
String value;
PropertyType type;

String getValue() { return value; } // This is the extra column

@ManyToOne
PropertyType getPropertyType() { return type; }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 11:03 am 
Newbie

Joined: Thu Mar 29, 2007 5:57 pm
Posts: 2
For anyone interested, I stumbled upon the answer to my problem

@Entity
class Test
{
long id;
Set<TestProperty> properties = new Set<TestProperty>();

@Id
long getID() { return id; }

@CollectionOfElements
@JoinTable(name="TEST_PROPERTIES",joinColumns=@JoinColumn(name="TEST_ID"))
Set<TestProperty> getProperties() { return properties; }

}

@Entity
class PropertyType
{
long id;
String name;
String metadata;

@Id
long getID() { return id; }

String getName() { return name; }
String getMetadata() { return metadata; }
}

@Embeddable
class TestProperty
{
String value;
PropertyType type;

String getValue() { return value; } // This is the extra column

@ManyToOne
@JoinColumn(name="PROPERTY_TYPE_ID",nullable=false,updatable=false)
PropertyType getPropertyType() { return type; }
}


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