日期:2014-05-17  浏览次数:20690 次

hibernate 中的 Entity 类中不能有 对象数组 字段 吗?
代码一:
package com.octopus.hibernate.Inheritance;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity
public class Tree2 {
private int id;
private String name;
private Tree perent;
private Tree[] suns;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
public Tree getPerent() {
return perent;
}
public void setPerent(Tree perent) {
this.perent = perent;
}
@OneToMany(mappedBy="perent")
@JoinColumn(name="p_id")
public Tree[] getSuns() {
return suns;
}
public void setSuns(Tree[] suns) {
this.suns = suns;
}

}
代码2:
package com.octopus.hibernate.Inheritance;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;

@Entity
public class Tree {
private int id;
private String name;
private Tree perent;
private Map<Integer,Tree> suns;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
public Tree getPerent() {
return perent;
}
public void setPerent(Tree perent) {
this.perent = perent;
}
@OneToMany(mappedBy="perent")
@JoinColumn(name="p_id")
@MapKey(name="id")
public Map<Integer,Tree> getSuns() {
return suns;
}
public void setSuns(Map<Integer,Tree> suns) {
this.suns = suns;
}


}
代码1使用的 对象数组,代码2 中使用的是Map
代码一生成表的时候报错:

Exception in thread "main" org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.octopus.hibernate.Inheritance.Tree2.perent references an unknown entity: com.octopus.hibernate.Inheritance.Tree
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:81)
at org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:456)
at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:438)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:309)
at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:803)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaE