0

我是 Spring ORM 的新手,我正在尝试实现这个 Spring+Hibernate 教程:

http://it4beginners.wordpress.com/2012/10/05/spring-3-and-hibernate-4-for-beginners/

唯一的区别是我没有使用PostgreSQL,但我使用的是MySql数据库,所以我在 pom.xml 文件中插入了正确的 MySql 连接器,并且我在ApplicationContext.xml配置文件中更改了 mainDataSource 属性...

问题是当我运行我的App.java类(包含 main 方法的主类)时,我在堆栈跟踪中获得以下错误消息:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: org.andrea.myexample.myHibernateSpringExample.entityclasses.ChemicalStructure.occurence[java.lang.Object]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1057)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:733)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:668)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1597)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:357)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 12 more

不明白问题出在哪里!!!

这是似乎存在问题的类的代码:

package org.andrea.myexample.myHibernateSpringExample.entityclasses;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "chemical_structure")
public class ChemicalStructure implements Serializable {

private Long id;
private String structureKey;
private String structureData;
private Set<Object> occurence;

/**
 * @return the id
 */
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "chem_structure_seq_gen")
@SequenceGenerator(name = "chem_structure_seq_gen", sequenceName = "seq_chemical_structure")
@Column(name = "structure_id")
public Long getId() {
    return id;
}

/**
 * @param id
 *            the id to set
 */
public void setId(Long id) {
    this.id = id;
}

/**
 * @return the structureKey
 */
@Column(name = "structure_key", unique = true)
public String getStructureKey() {
    return structureKey;
}

/**
 * @param structureKey
 *            the structureKey to set
 */
public void setStructureKey(String structureKey) {
    this.structureKey = structureKey;
}

/**
 * @return the structureData
 */
@Column(name = "chemical_structure")
public String getStructureData() {
    return structureData;
}

/**
 * @param structureData
 *            the structureData to set
 */
public void setStructureData(String structureData) {
    this.structureData = structureData;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.chemicalStructure", cascade = CascadeType.ALL)
public Set<Object> getOccurence() {
    return occurence;
}

public void setOccurence(Set<Object> occurence) {
    this.occurence = occurence;
}
}

你有什么想法吗?

肿瘤坏死因子

4

2 回答 2

1

该错误的原因在于您尝试映射一组普通对象。Object 不是 JPA 实体,因此不能映射到关系类型。

您需要创建一个名为 Occurence 的实体,并按照您使用对象集的方式对其进行映射。

于 2013-02-25T12:36:06.287 回答
0

我认为问题出在 mappedBy = "pk.chemicalStructure" 检查您是否在此类中有 ManyToOne 关系,例如:

@ManyToOne
@JoinColumn(name = "chemicalstructure_id", nullable = false)
@ForeignKey(name = "fk_chemicalstructure_id")
private Occurence occurence;

然后 mappedBy = "发生"

该错误表明缺少这种双向关系。

于 2013-02-22T12:45:30.367 回答