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.
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.
精彩评论