代码一:
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>(SchemaExport.java:128)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:91)
at com.octopus.hibernate.yiduiyi.TestClass.testCreateORMappingAn(TestClass.java:28)
at com.octopus.hibernate.yiduiyi.TestClass.main(TestClass.java:15)
求高人指点?Hibernateentity对象数组

解决方案 »

  1.   

    自己写的类的数组????  没有试过,不过在数据库中这种关系直接就是用Set来定义的吧。这样声明好像没办法放到数据库中
      

  2.   

    一般使用set,来表示一对多,多对多的情况
      

  3.   

    references an unknown entity: com.octopus.hibernate.Inheritance.Tree只有hibernate映射类才能让你配到关系里。
      

  4.   

    我试了很多次,集合映射好像真的不使用 数组, Set Map List 都可以,至于2 楼说的 自己写的集合不能放关系类里面?我肯定是可以放的.不然拿来的hibernate一对多的关系映射呢?