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; }
}
|