Hibernate双向关系

时间:2014-08-23 02:18:42

标签: hibernate

在hibernate中建立双向关系@OneToMany的正确方法是什么?

我有3个表useraccount,category和item。在我的项目表中,它包含useraccount和category的外键。如果我建立单向,它在项目类中编译如下代码:

@ManyToOne
@JoinColumn(name="cat_id")
public Category getCategory() { return category;    }
public void setCategory(Category category) {this.category = category; }
private Category category;

@ManyToOne
@JoinColumn(name="user_id")
public UserAccount getItemOwner() { return itemOwner;   }
public void setItemOwner(UserAccount itemOwner) {   this.itemOwner = itemOwner; }
private UserAccount itemOwner;

但是当我尝试通过在useraccount和category中添加那些映射来定义双向时:

@OneToMany(mappedBy="USERACCOUNTS", targetEntity=Item.class, cascade=CascadeType.ALL)
public Set<Item> getOwnedItems() {  return ownedItems;  }
public void setOwnedItems(Set<Item> ownedItems) {   this.ownedItems = ownedItems;   }
private Set<Item> ownedItems;

@OneToMany(mappedBy="CATEGORIES", targetEntity=Item.class, cascade=CascadeType.ALL)
public Set<Item> getListOfItem() {  return listOfItem;  }
public void setListOfItem(Set<Item> listOfItem) {   this.listOfItem = listOfItem;   }
private Set<Item>listOfItem; 

它给了我这个错误:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.isad.model.Item.CATEGORIES in com.isad.model.Category.listOfItem
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:684)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.isad.model.Item.CATEGORIES in com.isad.model.Category.listOfItem
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:768)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:343)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:431)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:416)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 21 more

我查看了该文档,但无法找出导致问题的原因。任何人都可以给我建议解决这个问题。感谢

2 个答案:

答案 0 :(得分:1)

注释@JoinColumn表示该实体是关系的所有者(即:对应的表具有带引用表的外键的列),而属性mappedBy表示此方面的实体是关系的反转,所有者驻留在“其他”实体中。

根据documentation

由于多对一(几乎)始终是JPA规范中双向关系的所有者方,因此@OneToMany注释了一对多关联(mappedBy) = ...)

@Entity
public class Troop {
    @OneToMany(mappedBy="troop")
    public Set<Soldier> getSoldiers() {
    ...
}

@Entity
public class Soldier {
    @ManyToOne
    @JoinColumn(name="troop_fk")
    public Troop getTroop() {
    ...
} 

部队通过部队属性与士兵有一对一的双向关系。您不必(不得)在mappedBy端定义任何物理映射。

要将双向一个映射到多个,使用一对多方作为拥有方,您必须删除mappedBy元素并将多个设置为@JoinColumn为可插入和可更新为false。此解决方案未经过优化,将生成一些额外的UPDATE语句。

@Entity
public class Troop {
    @OneToMany
    @JoinColumn(name="troop_fk") //we need to duplicate the physical information
    public Set<Soldier> getSoldiers() {
    ...
}

@Entity
public class Soldier {
    @ManyToOne
    @JoinColumn(name="troop_fk", insertable=false, updatable=false)
    public Troop getTroop() {
    ...
}

答案 1 :(得分:0)

@OneToMany的所有“mappedBy”都是错误的。它的值应该是另一方的属性名称,尝试配置如下:

@OneToMany(mappedBy="itemOwner", cascade=CascadeType.ALL)
public Set<Item> getOwnedItems() {  return ownedItems;  }

@OneToMany(mappedBy="category", cascade=CascadeType.ALL)
public Set<Item> getListOfItem() {  return listOfItem;  }