Hibernate version:
3.2.0GA
I've found a funny issue while using hibernate3 with ejb3.
I have a wierd table structure:
LegacyTable - old table in our DB with a composite key
LegacyExtendedTable - new table that is a child of the Legacy Table with an extra field
NewTable - a new, and simple table (one Id field that is generated by hibernate)
ManyToManyTable - between LegacyExtendedTable and NewTable with an extra field, so I have to write it out in hibernate as a new class.
I was able to get everything working without issue until working on the many-to-many table. Its using a key of two many-to-ones to two tables. One being a simple Id, the other a composite key.
Initially I used the following (I had to genericise the code, so my apologizes for minor syntax issues)
Code:
@Table(name = "manyToManyTable")
public class ManyToManyTable
{
private int extraField;
private ManyToManyTablePk pk;
@Column(name="whatever") public int getExtraField(){return extraField;}
@Id ManyToManyTablePk pk;
}
@Embeddable
public class ManyToManyTablePk
{
private LegacyExtendedTable legacy;
private NewTable new;
@ManyToOne
@JoinColumns({
@JoinColumn(name = "PK_NO"), @JoinColumn(name = "PK_NO_2"),
@JoinColumn(name = "PK_INT")})
public LegacyExtendedTable getLegacy() { return legacy; }
@ManyToOne
@JoinColumn(name = "NEW_ID")
public NewTable getNew() { return new; }
}
On deploy, Hibernate complained that the NewTable FK was bad because the NewTable code didn't have all four FK constraints. I figured this was because hibernate mapped the annotation like a many-to-one instead of a key-many-to-one. I replaced the code with the following
Code:
@Table(name = "manyToManyTable")
@IdClass(ManyToManyTablePk.class)
public class ManyToManyTable
{
private int extraField;
private LegacyExtendedTable legacy;
private NewTable new;
@Column(name="whatever") public int getExtraField(){return extraField;}
@Id
@ManyToOne
@JoinColumns({
@JoinColumn(name = "PK_NO"), @JoinColumn(name = "PK_NO_2"),
@JoinColumn(name = "PK_INT")})
public LegacyExtendedTable getLegacy() { return legacy; }
@Id
@ManyToOne
@JoinColumn(name = "NEW_ID")
public NewTable getNew() { return new; }
}
@Embeddable
public class ManyToManyTablePk
{
private LegacyExtendedTable legacy;
private NewTable new;
public LegacyExtendedTable getLegacy() { return legacy; }
public NewTable getNew() { return new; }
}
And the code worked, fine.
It looks like key-many-to-one only works when you put an @Id tag before the @ManyToOne. If the @ManyToOne is embedded in a PK class, and you don't use the @IdClass method in your object, it doesn't recognize it as a key-many-to-one.
Is anyone else noticing this? I don't want to mark it up as a bug unless someone else sees a similar situation.
Thanks!