简体   繁体   English

休眠多个表的一对多映射

[英]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;

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:您好,我是 hibernate 新手,我正在尝试映射 OneToMany 访问--> 测试和访问--> 方向,但出现错误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!请帮帮我!

The value of the mappedBy field on the @OneToMany annotation references java instance variable names, and it is case sensitive. @OneToMany注解上的mappedBy字段的值引用了java 实例变量名,并且区分大小写。 You are setting it to Visit , but in the directions and test classes the variable names are visit .您将其设置为Visit ,但在directionstest类中,变量名称是visit

The solution is to change property mappedBy from Visit to visit (lower case V):解决方案是将属性mappedByVisit改为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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM