Getting error "Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements"

huangapple go评论74阅读模式
英文:

Getting error "Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements"

问题

我一直在开发一个REST API,但似乎无法摆脱这个错误。

> 嵌套异常是 org.hibernate.AnnotationException:
非法尝试将非集合映射为 @OneToMany、@ManyToMany 或 @CollectionOfElements:com.example.TNDservice.Entity.HotelEntity.HotelId

代码:

public class HotelEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int Id;

    @OneToMany(mappedBy = "hotelCon")
    @Column(name = "HOTELID")
    private String HotelId;

   
    public String getHotelId() {
        return HotelId;
    }

    public void setHotelId(String hotelId) {
        HotelId = hotelId;
    }
}

 public class ContractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int Id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "T522_HOTEL_HOTELID")
    private HotelEntity hotelCon;
}
英文:

I have been developing a REST API and I just can't seem to get rid of this error.

> nested exception is org.hibernate.AnnotationException:
Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or
@CollectionOfElements: com.example.TNDservice.Entity.HotelEntity.HotelId

Code:

public class HotelEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int Id;

    @OneToMany(mappedBy = "hotelCon")
    @Column(name = "HOTELID")
    private String HotelId;

   
    public String getHotelId() {
        return HotelId;
    }

    public void setHotelId(String hotelId) {
        HotelId = hotelId;
    }
}

 public class ContractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int Id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "T522_HOTEL_HOTELID")
    private HotelEntity hotelCon;
}

答案1

得分: 0

@OneToMany(mappedBy = "hotelCon")
@Column(name = "HOTELID")
private String HotelId;

如其名称所示,@OneToMany表示一对多的关系,将一个对象映射到多个对象,因此您不应该有这个

private String HotelId;

而是应该使用

private Collection<Entity name> listOfEntity;

变量名应该以小写字母开头。请查阅基本概念和文档。
英文:
@OneToMany(mappedBy = &quot;hotelCon&quot;) @Column(name = &quot;HOTELID&quot;) 
private String HotelId; 

As name suggests one to many relationship map one object to multiple objects, hence you are not supposed to have this

private String HotelId ;

Instead of

private Collection&lt;Entity name&gt; listOfEntity ;

Variable name should start with lowercase. Please go through the basic concepts and documentation.

答案2

得分: 0

所以最终我意识到问题出在我有两个ID,一个是自动生成的,一个是手动分配的。最终我将其中一个移除,就像这样:

public class HotelEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "HOTELID")
    private String HotelId;

public class ContractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CONTRACTID")
    private String ContractId;
}

我决定保持ID的自动生成。

英文:

So I ended up realizing that the fault was that I have two Id's, one generated and one assigned. I ended up taking one of them off like so,

public class HotelEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &quot;HOTELID&quot;)
    private String HotelId;

public class ContractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &quot;CONTRACTID&quot;)
    private String ContractId;
}

I decided to keep the ID as auto-generated.

huangapple
  • 本文由 发表于 2020年4月6日 11:28:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61052529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定