Hibernate version:3.1.1
Hibernate annotations version:3.1beta8
Hi,
I'm having real trouble converting a mapping file to annotations for what is conceptually a fairly simple case. What I have is a PropertyDefinition class that can have 0 or more PropertyParams. The legacy database I am working with defines a composite primary key on the child table, consisting of the propertyId of the parent, and an index.
In my mapping document, I specify this as follows:
Mapping documents:
Code:
<class name="PropertyDefinition" table="PROPERTY_DEFINITION" >
<id name="propertyId" column="I_PROPERTY_ID" >
<generator class="native" >
<param name="sequence">PROPERTY_SEQ</param>
</generator>
</id>
...
<list name="propertyParams" lazy="false" cascade="all" >
<key>
<column name="I_PROPERTY_ID" />
</key>
<list-index column="I_PARAM_ID" base="1" />
<one-to-many class="PropertyParams" />
</list>
</class>
<class name="PropertyParams" table="PROPERTY_PARAMS" >
<composite-id>
<key-many-to-one name="propertyDefinition" column="I_PROPERTY_ID" class="PropertyDefinition" />
<key-property name="paramId" column="I_PARAM_ID" />
</composite-id>
<property name="label" column="C_LABEL" />
<property name="value" column="C_VALUE" />
</class>
This works fine, but I'd really like to be able to use annotations in place of mapping files, since there are a large number of classes to map.
What I am struggling with is how to convert this mapping file into the equivalent annotations. My class definitions look something like the following (edited for brevity):
Code:
@Entity
@Table(name = "PROPERTY_DEFINITION")
@SequenceGenerator(name = "PropertyIdGenerator", sequenceName = "PROPERTY_SEQ")
public class PropertyDefinition
implements java.io.Serializable
{
// Fields
private long propertyId;
private List<PropertyParams> propertyParams = new ArrayList<PropertyParams>( 0 );
// Property accessors
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator="PropertyIdGenerator")
@Column(name = "I_PROPERTY_ID", unique = true, nullable = false, insertable = true, updatable = true, precision = 22, scale = 0)
public long getPropertyId() {
return this.propertyId;
}
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "propertyDefinition")
@IndexColumn(name="I_PARAM_ID", base=1)
public List<PropertyParams> getPropertyParams() {
return this.propertyParams;
}
}
@Entity
@Table(name = "PROPERTY_PARAMS", uniqueConstraints = { @UniqueConstraint(columnNames = {
"I_PROPERTY_ID", "C_LABEL" }) })
@IdClass(PropertyParamsId.class)
public class PropertyParams
implements java.io.Serializable
{
// Fields
// private PropertyParamsId id;
//private long propertyId;
private PropertyDefinition propertyDefinition;
private long paramId;
@Id
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name = "I_PROPERTY_ID", unique = false, nullable = false, insertable = false, updatable = false)
public PropertyDefinition getPropertyDefinition() {
return this.propertyDefinition;
}
@Id
public long getParamId() {
return paramId;
}
}
@Embeddable
public class PropertyParamsId
implements java.io.Serializable
{
// Fields
//private long propertyId;
private PropertyDefinition propertyDefinition;
private long paramId;
public PropertyDefinition getPropertyDefinition() {
return this.propertyDefinition;
}
public void setPropertyDefinition(PropertyDefinition propertyDefinition) {
this.propertyDefinition = propertyDefinition;
}
public long getParamId() {
return this.paramId;
}
}
The mixture of composite keys, embeddable's, and list index columns is proving too much to grasp... I've read the documentation and looked for examples, but just haven't been able to work out the right combination of field types and annotations.
Any assistance would therefore be very much appreciated!
Thanks,[/code]