I have a problem with foreign keys created for OneToMany relationship in secondary table.
I am using Hibernate Core 3.2.5.GA and Hibernate Annotations 3.3.0.GA.
Please take a look at the example classes and annotations.
Abstract superclass:
Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="category",discriminatorType=DiscriminatorType.CHAR)
@DiscriminatorValue("@")
public abstract class Product {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
// ... more attributes ...
}
Several subclasses, one particularly interesting:
Code:
@Entity
@DiscriminatorValue("W")
@SecondaryTable(name="WineProduct")
public class WineProduct extends Product {
private float alcoholicStrength;
// ... more attributes persisted in Product ...
@Column(table="WineProduct")
private String wineGrowingZone;
// ... more attributes persisted in WineProduct ...
@OneToMany
@JoinColumn(name="product_id",nullable=false)
private List<WineOperation> wineOperations;
}
And WineOperation:
Code:
@Entity
public class WineOperation {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
// ... more attributes ...
}
I am using Hibernate to generate the database schema. The problem is that the foreign key created for @OneToMany is not what I want it to be: it is WineOperation --> Product instead of WineOperation --> WineProduct.
After changing @JoinColumn annotation in WineProduct to (is this even correct?):
Code:
@JoinColumn(table="WineProduct",name="product_id",nullable=false)
I get the folowing exception: org.hibernate.cfg.NotYetImplementedException: Collections having FK in secondary table
Any ideas how to get the foreign key created correctly?