双向休眠关系

时间:2009-06-24 14:14:49

标签: java hibernate jpa

我有一个使用hibernate和以下实体的Web应用程序。 我做错了什么?

...
@Entity
public class Registration implements BaseEntity {
...
@OneToMany(cascade = {CascadeType.PERSIST}, mappedBy = "registration", fetch = FetchType.EAGER)
private List<OrderedProduct> orderedProducts = new ArrayList<OrderedProduct>();
...
public List<OrderedProduct> getOrderedProducts() {
    return orderedProducts;
}

public void setOrderedProducts(List<OrderedProduct> orderedProducts) {
    this.orderedProducts = orderedProducts;
}
...

...
@Entity
public class OrderedProduct implements BaseEntity {
...
@ManyToOne
@JoinColumn(name = "registration_id",nullable = true)
private Registration registration;
...
public Registration getRegistration() {
    return registration;
}
public void setRegistration(Registration registration) {
    this.registration = registration;
}
...

当我使用它时,我得到以下Tomcat错误:

Caused by: org.hibernate.AnnotationException: Illegal use of mappedBy on both sides of the relationship: at.irian.conference.domain.Registration.orderedProducts
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:193)
    at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1325)
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1164)
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:602)
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:543)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1163)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:329)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1148)
    at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1226)
    at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:173)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:425)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:131)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:221)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:251)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1257)
    ... 86 more

4 个答案:

答案 0 :(得分:2)

这段代码是对的,我忘了删除其他关系

答案 1 :(得分:1)

使用List进行映射集合需要在数据库中映射“序列”列。您打算用作Set

吗?

答案 2 :(得分:0)

我不确定使用Annotations的语法。我们过去常常定义这样的关系。

<强> Registration.hbm.xml

<bag
    name="orderedProducts"
    inverse="true"
    cascade="appropriate-one"
>
    <key>
        <column name="REGISTRATION_ID" />
    </key>
    <one-to-many 
        class="orderedProducts"
    />
</bag>

OrderedProducts.hbm.xml

<many-to-one
    name="registration"
    class="Registration"
>
    <column name="REGISTRATION_ID"/>
</many-to-one>

答案 3 :(得分:0)

尝试如下所示的注释。它将解决您的问题。

@ManyToOne(fetch = FetchType.EAGER)

@JoinColumn(name = "registration_id")

public Registration getRegistration() {
    return registration;
}

public void setRegistration(Registration registration) {
    this.registration = registration;
}

@OneToMany(fetch = FetchType.EAGER, targetEntity = Registration.class, mappedBy = "registration", cascade = { CascadeType.ALL })
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@Fetch(FetchMode.SELECT)
@JoinColumn(name = "registration_id")