Hibernate 3.5 注释

发布于 2024-11-23 20:17:05 字数 3515 浏览 1 评论 0原文

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

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

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

但是当我将其放在下面的 getter 上方时失败:

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;

我不知道为什么它适用于属性但不适用于 getter这让我抓狂。表格很好,注释对我来说也很好。我错过了一些非常明显的东西吗?会不会是版本问题?

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

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

The below annotation works when applying it to the field:

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

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

But fails when I place it above the getter below:

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

With the following error:

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

The Many side of the relationship:

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

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

I don't know why it works on the property but not on the getter and it's driving me nuts. The tables are fine and the annotation seems fine to me. Am I missing something very obvious? Could it be a version problem?

There is a basic base class but i dont think this has anything to do with it:

@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;
    }
}

I am using version 3.5.3-Final and 3.2.0.Final for the 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>

Does anyone have any ideas or experienced something similar? I've spent 2.5 hours of my life trying to fix this one and I predict another 12.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

棒棒糖 2024-11-30 20:17:05

我现在无法测试该解决方案(不幸的是,我的计算机内存不足四万亿千兆字节,因此此处没有启动 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 文档,您可能会喜欢阅读 这个

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文