英文:
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 = "hotelCon") @Column(name = "HOTELID")
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<Entity name> 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 = "HOTELID")
private String HotelId;
public class ContractEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CONTRACTID")
private String ContractId;
}
I decided to keep the ID as auto-generated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论