java @OnetoMany 类调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5664714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:10:40  来源:igfitidea点击:

@OnetoMany class call

javahibernatejpaone-to-many

提问by stefanuzz

i need some help for my class...

我的课需要一些帮助...

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+"]";
    }
}

But i get thi error...

但我得到这个错误...

Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.it.ese.orbit.entity.OrbitObject.OrbitObject

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

I wish i create an OrbitObject field as like an object of the same class... Help please!

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

回答by Edwin Dalorzo

You either do

你要么做

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

Or

或者

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

The first case implies this entity will be the owning side, namely, it will have the foreign key.

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

回答by Riccardo Cossu

OneToMany means that OrbitObject has many OrbitObject children, which is not true because the OrbitObject property is not a collection. You must convert it to a ManyToOne

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

回答by Vladimir Ivanov

you can use @OneToMany referring to a collection of elements, for example

例如,您可以使用 @OneToMany 来指代元素集合

@OneToMany
List<OrbitObject> orbitList;