I'm using Hibernate 3.2.0.GA.
I need to establish a @ManyToOne join between two tables using a composite join on two non-pk columns which are actually @ManyToOne themselves (foreign keys) within their respective classes.
Unless I have something wrong, it appears that the mapping code can not handle a referencedColumnName of a property which is an association in the target table of a @ManyToOne:
Code:
org.hibernate.MappingException: Unable to find column with logical name: VENDOR_ID in org.hibernate.mapping.Table(ItemCost) and its related supertables and secondary tables.
at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:364)
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:88)
at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:63)
at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1039)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1207)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:844)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:382)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:126)
(many more...)
I've tried to simplify this code to illustrate the objective:
Code:
public class Item {
int Id;
Vendor defaultVendor;
Cost defaultCost;
@Id getId {
return id;
}
@ManyToOne
@JoinColumn(name="VENDOR_ID")
Vendor defaultVendor getDefaultVendor {
return defaultVendor;
}
@ManyToOne
@JoinColumns({
@JoinColumn(name="VENDOR_ID", referencedColumnName="VENDOR_ID", insertable=false, updatable=false),
@JoinColumn(name="id", referencedColumnName="ITEM_ID", insertable=false, updatable=false)
})
public Cost getDefaultCost() {
return defaultCost;
}
}
public class ItemCost {
int Id;
Item item;
Vendor vendor;
BigDecimal cost;
@Id getId {
return id;
}
@ManyToOne
@JoinColumn(name="ITEM_ID")
Item item getItem{
return item
}
@ManyToOne
@JoinColumn(name="VENDOR_ID")
Vendor vendor getVendor {
return vendor
}
BigDecimal cost getCost {
return cost;
}
}
There will be a unique constraint on the item/vendor combination so there can only be one match in the database at a time, and an index for performance.
I could use a named query within the Item class, however this would not result in a JOIN but a subselect. I believe a JOIN will perform much better during retrieval of Item records.
Using a composite key in the Cost table is not an option for various reasons having to do with our application framework.
I've tried all of the logical names I can think of within the @JoinColumn annotation like vendor.id, VENDOR_ID, vendor_id, vendor, etc. All throw the exception above.
Any advice on how to get this to work would be appreciated.