@OnetoMany类调用

时间:2011-04-14 14:24:55

标签: java hibernate jpa one-to-many

我的班级需要一些帮助...

package com.it.ese.orbit.entity;
import javax.persistence.*;
import java.util.List;



/**
 * Created by IntelliJ IDEA.
 * User: Shahriar Newaz
 * Date: 07/03/11
 * Time: 10.07
 */
@Entity
@Inheritance(strategy =InheritanceType.JOINED)
public class OrbitObject {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue


    @Column(name="id",nullable = false)
    private Long id;

    @Column(name="Scenario",nullable = false)
    private String scenario;  // not sure about how to map scenario

    @Column(name="code",nullable = true)
    private String code;

    @Column(name="name",nullable = true)
    private String name;

    @OneToOne(cascade=CascadeType.ALL)
    private Bia bia;

    @OneToOne(cascade=CascadeType.ALL)
    public Impatti impatti;


    @Column(name="parent",nullable = true)
    @OneToMany(cascade=CascadeType.ALL)
    private OrbitObject OrbitObject;


    public Long getId() {
     return id;
    }

    protected void setId(Long id) {
     this.id = id;
    }

    public String getCode() {
     return code;
    }

    public void setCode(String code) {
     this.code = code;
    }



    public String getScenario() {
        return scenario;
    }
    public void setScenario(String scenario) {
        this.scenario = scenario;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        name = name;
    }

     // LOG
    @Override
    public String toString(){
        return "com.it.ese.orbit.models.OrbitObject["
        + " - name="+name + " - scenario="+scenario +" - id= "+id+"]";
    }
}

但是我得到了错误...

  

引起:org.hibernate.AnnotationException:非法尝试将非集合映射为@OneToMany,@ ManyToMany或@CollectionOfElements:com.it.ese.orbit.entity.OrbitObject.OrbitObject

我希望我创建一个OrbitObject字段,就像同一个类的对象一样...... 求救!

3 个答案:

答案 0 :(得分:5)

你要么

@Column(name="parent",nullable = true)
@ManyToOne(cascade=CascadeType.ALL)
private OrbitObject OrbitObject;

或者

@Column(name="parent",nullable = true)
@OneToMany(cascade=CascadeType.ALL)
private Set<OrbitObject> OrbitObject;

第一种情况意味着该实体将是拥有方,即它将拥有外键。

答案 1 :(得分:1)

OneToMany意味着OrbitObject有许多OrbitObject子节点,这是不正确的,因为OrbitObject属性不是集合。 您必须将其转换为ManyToOne

答案 2 :(得分:0)

您可以使用@OneToMany引用元素集合,例如

@OneToMany
List<OrbitObject> orbitList;