英文:
Hibernate one to many mapping for multiple tables
问题
@Entity
@Table(name="Visit")
public class Visit {
@Id
@XmlTransient
@JsonIgnore
@SequenceGenerator(name = "v_id_seq", sequenceName = "v_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "v_id_seq")
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "Visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<directions> directions;
@OneToMany(mappedBy = "Visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<Test> Test;
}
@Entity
@Table(name="test")
public class Test {
@Id
@XmlTransient
@JsonIgnore
@SequenceGenerator(name = "t_id_seq", sequenceName = "t_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "t_id_seq")
@Column(name = "id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JoinColumn(name = "vid", updatable = false, insertable = true,referencedColumnName = "id")
private Visit visit;
}
@Entity
@Table(name="direction")
public class directions {
@Id
@XmlTransient
@JsonIgnore
@SequenceGenerator(name = "d_id_seq", sequenceName = "d_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "d_id_seq")
@Column(name = "id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JoinColumn(name = "vid", updatable = false, insertable = true,referencedColumnName = "id")
private Visit Visit;
}
你好,我是你的中文翻译。上面是你提供的代码的翻译部分。如果你对于在Hibernate中实现One-to-Many关系有疑问,欢迎继续提问。
英文:
@Entity
@Table(name="Visit")
public class Visit {
@Id
@XmlTransient
@JsonIgnore
@SequenceGenerator(name = "v_id_seq", sequenceName = "v_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "v_id_seq")
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "Visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<directions> directions;
@OneToMany(mappedBy = "Visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<Test> Test;
@Entity
@Table(name="test")
public class Test {
@Id
@XmlTransient
@JsonIgnore
@SequenceGenerator(name = "t_id_seq", sequenceName = "t_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "t_id_seq")
@Column(name = "id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JoinColumn(name = "vid", updatable = false, insertable = true,referencedColumnName = "id")
private Visit visit;
@Entity
@Table(name="direction")
public class directions {
@Id
@XmlTransient
@JsonIgnore
@SequenceGenerator(name = "d_id_seq", sequenceName = "d_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "d_id_seq")
@Column(name = "id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JoinColumn(name = "vid", updatable = false, insertable = true,referencedColumnName = "id")
private Visit Visit;
Hello i am new to hibernate
I am trying to map OneToMany Visit-->Test and Visit-->direction but getting error
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property:
one visit can have multiple direction and test
how can i implement this?
plz help me!
答案1
得分: 1
mappedBy
字段在 @OneToMany
注解上的值引用了 Java 实例变量名,它是区分大小写的。你将其设置为 Visit
,但在 directions
和 test
类中,变量名分别是 visit
。
解决方法是将属性 mappedBy
从 Visit
更改为 visit
(小写的 V):
@OneToMany(mappedBy = "visit", cascade = CascadeType.PERSIST, orphanRemoval = true, fetch = FetchType.LAZY)
private List<directions> directions;
@OneToMany(mappedBy = "visit", cascade = CascadeType.PERSIST, orphanRemoval = true, fetch = FetchType.LAZY)
private List<Test> Test;
英文:
The value of the mappedBy
field on the @OneToMany
annotation references java instance variable names, and it is case sensitive. You are setting it to Visit
, but in the directions
and test
classes the variable names are visit
.
The solution is to change property mappedBy
from Visit
to visit
(lower case V):
@OneToMany(mappedBy = "visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<directions> directions;
@OneToMany(mappedBy = "visit",cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List<Test> Test;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论