Hibernate 3.5 注释
下面的注释在将其应用于字段时有效:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在无法测试该解决方案(不幸的是,我的计算机内存不足四万亿千兆字节,因此此处没有启动 Hibernate),但作为 doc 演示,您的集合需要一个类型,因此,不要使用:,而是
使用:
,它应该可以解决问题。如果您不喜欢 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:
you use:
and it should do the trick. In case you don't like the Hibernate documentation, you might enjoy reading this.