0

以下注释在将其应用于字段时有效:

@OneToMany(targetEntity=TestMany.class,
            cascade=CascadeType.ALL, fetch=FetchType.EAGER)
            @JoinColumn(name="TESTID")

private Set<TestMany> testManys = new HashSet<TestMany>();

但是当我将它放在下面的吸气剂上方时失败了:

public Set<TestMany> getTestManys() {
    return testManys;
}

出现以下错误:

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: TEST, for columns: [org.hibernate.mapping.Column(testManys)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:291)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:275)
    at org.hibernate.mapping.Property.isValid(Property.java:217)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:464)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:236)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1193)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1378)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    ... 47 more

关系的多方面:

@SuppressWarnings("serial")
@Entity
@Table(name="TESTMANY")
public class TestMany  extends BaseEntityImpl{

@ManyToOne
@JoinColumn(name="TESTID", insertable=false, updatable=false)
private Test test;

我不知道为什么它在属性上有效,但在吸气剂上无效,这让我发疯。表格很好,注释对我来说似乎很好。我错过了一些非常明显的东西吗?会不会是版本问题?

有一个基本的基类,但我认为这与它没有任何关系:

@MappedSuperclass
public abstract class BaseEntityImpl implements BaseEntity, Serializable {

    private static final long serialVersionUID = 7887314289537012320L;

    @Id @GeneratedValue(strategy = AUTO)
    @Column(name = "ID")
    private Long id;

    public Long getId() {
        return id;
    }

    @SuppressWarnings("unused")
    private void setId(Long id) {
        this.id = id;
    }
}

我使用版本 3.5.3-Final 和 3.2.0.Final 作为 hibernate-coomons-annotations。

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.5.3-Final version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.3-Final <version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.2.0.Final</version>
    </dependency>

有没有人有任何想法或经历过类似的事情?我已经花了 2.5 个小时试图修复这个问题,我预计还会有 12 个。

4

1 回答 1

0

I'm unable to test the solution right now (I'm unfortunately stuck with a computer with less than a quadrillion gigabytes of RAM, so no Hibernate launching here), but as the doc demonstrates, your set needs a type, so instead of:

@OneToMany(targetEntity=TestMany.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="TESTID")
private Set testManys = new HashSet();

you use:

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="TESTID")
private Set<TestMany> testManys = new HashSet();

and it should do the trick. In case you don't like the Hibernate documentation, you might enjoy reading this.

于 2011-07-12T22:54:07.543 回答