问题描述
我遵循并试图实现 9.4部分的示例。组件作为复合标识符但面临着如何实现它的问题。
以下是我所做的:
我的实体类:
Order.java
public class Order {
private int id;
私人套餐< OrderLine> lines = new HashSet< OrderLine>();
// Setters& Getters
OrderLine.java
public class OrderLine {
private OrderLineId id;
私人字符串名称;
私人订单;
// Setters& Getters
OrderLineId.java
public class OrderLineId implements Serializable {
private int lineId;
private int orderId;
private int customerId;
// Setters& Getters
}
我的映射文件存在问题:
< hibernate-mapping>
< class name =Ordertable =TEST_Order>
< id name =idtype =intcolumn =id>
< generator class =native/>
< / id>
< set name =linescascade =all>
< key column =orderId/>
<一对多等级=OrderLine/>
< / set>
< / class>
< class name =OrderLinetable =TEST_OrderLine>
< composite-id name =idclass =OrderLineId>
< key-property name =lineId/>
< key-property name =orderId/>
< key-property name =customerId/>
< / composite-id>
< property name =name/>
<多对一名称=orderclass =Order
insert =falseupdate =false>
< column name =orderId/>
< column name =customerId/>
< /多对一>
< / class>
< / hibernate-mapping>
当我试图创建解析这个映射文件的会话工厂时,我得到一个异常:
引起:org.hibernate.MappingException:外键(FK_89b4nqt5l2n6tfd1d5tq0ill0:TEST_OrderLine [orderId,customerId]))必须相同作为引用主键的列数(TEST_Order [id])
在org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:110)
在org.hibernate.mapping.ForeignKey.alignColumns( ForeignKey.java:93)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1816)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1739)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
有人可以帮助我如何实现给出的示例
OrderLine需要引用Order PK,它不是一个复合键。
$ b
这意味着多对一必须是:
< many-to- one name =orderclass =Order
insert =falseupdate =false>
< column name =orderId/>
< /多对一>
orderId是Order.id的FK。
然后一对多一方将变成:
< set name =linescascade =所有>
< key>
< column name =orderId/>
< / key>
<一对多等级=OrderLine/>
< / set>
所以即使OrderLine有一个组合键,引用也是在Order.id之后进行的,是一个简单的键。
如果要将其他关联映射到OrderLine,如OrderLineProduct,则需要使用复合关键字来映射父关联(OrderLine)和子级(OrderLineProduct),以便OrderLineProduct将组合外键返回给OrderLine。
I am following Hibernate Documentation and trying to implement the example given for section 9.4. Components as composite identifiers but facing issues on how to implement it.
Here is what I have done:
My entity classes:
Order.java
public class Order { private int id; private Set<OrderLine> lines = new HashSet<OrderLine>(); // Setters & Getters }
OrderLine.java
public class OrderLine { private OrderLineId id; private String name; private Order order; // Setters & Getters }
OrderLineId.java
public class OrderLineId implements Serializable{ private int lineId; private int orderId; private int customerId; // Setters & Getters }
My mapping file which is having issues:
<hibernate-mapping> <class name="Order" table="TEST_Order"> <id name="id" type="int" column="id"> <generator class="native"/> </id> <set name="lines" cascade="all"> <key column="orderId"/> <one-to-many class="OrderLine"/> </set> </class> <class name="OrderLine" table="TEST_OrderLine"> <composite-id name="id" class="OrderLineId"> <key-property name="lineId"/> <key-property name="orderId"/> <key-property name="customerId"/> </composite-id> <property name="name"/> <many-to-one name="order" class="Order" insert="false" update="false"> <column name="orderId"/> <column name="customerId"/> </many-to-one> </class> </hibernate-mapping>
When I am trying to create a session factory which parses this mapping file, I am getting an exception saying:
Caused by: org.hibernate.MappingException: Foreign key (FK_89b4nqt5l2n6tfd1d5tq0ill0:TEST_OrderLine [orderId,customerId])) must have same number of columns as the referenced primary key (TEST_Order [id]) at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:110) at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:93) at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1816) at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1739) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
Can someone please help me how to implement the example given in the documentation.
The OrderLine needs to refernece the Order PK, which is not a composite key.
It means the many-to-one must be:
<many-to-one name="order" class="Order" insert="false" update="false"> <column name="orderId"/> </many-to-one>
The orderId is the FK to Order.id.
Then the one-to-many side will become:
<set name="lines" cascade="all"> <key> <column name="orderId"/> </key> <one-to-many class="OrderLine"/> </set>
So even if the OrderLine has a composite-key, the reference is made after Order.id, which is a simple key.
If you want to map other association to OrderLine, like OrderLineProduct then you'll need to use the composite-key to map the association between the parent (OrderLine) and the child (OrderLineProduct), so that OrderLineProduct has a composite-foreign-key back to OrderLine.
这篇关于组件在Hibernate中作为复合标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!