【问题标题】:Annotations Hibernate 3.5注释 Hibernate 3.5
【发布时间】:2011-10-04 01:37:46
【问题描述】:

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

@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 个小时。

【问题讨论】:

    标签: jpa one-to-many hibernate3


    【解决方案1】:

    我现在无法测试该解决方案(不幸的是,我遇到了一台内存不足 100 亿 GB 的计算机,因此这里没有启动 Hibernate),但正如 doc 所展示的,您的设备需要一个类型,所以不是:

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

    你使用:

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

    它应该可以解决问题。如果您不喜欢 Hibernate 文档,您可能会喜欢阅读 this

    【讨论】:

    • 嗨,谢谢,对不起,当我粘贴代码时,括号消失了。我实际上正在这样做:
    • 私有集 testManys = new HashSet(); @OneToMany(targetEntity=TestMany.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn(name="TESTID") public Set getTestManys() { return testManys; }
    • 私有集 testManys = new HashSet(); @OneToMany(targetEntity=TestMany.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn(name="TESTID") public Set getTestManys() { return testManys; }
    • erm,忽略格式,注释确实在 getter 上方但在字段下方。这让我很困惑。
    • 呃。省略 targetEntity 部分。
    猜你喜欢
    • 2011-02-26
    • 2011-05-15
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多