Hibernate一对多映射多个表

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

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=&quot;Visit&quot;)
public class Visit {
@Id
@XmlTransient
@JsonIgnore
@SequenceGenerator(name = &quot;v_id_seq&quot;, sequenceName = &quot;v_id_seq&quot;, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;v_id_seq&quot;)
@Column(name = &quot;id&quot;)
private Long id;
@OneToMany(mappedBy = &quot;Visit&quot;,cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List&lt;directions&gt; directions;
@OneToMany(mappedBy = &quot;Visit&quot;,cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List&lt;Test&gt; Test;
@Entity
@Table(name=&quot;test&quot;)
public class Test {
@Id
@XmlTransient
@JsonIgnore
@SequenceGenerator(name = &quot;t_id_seq&quot;, sequenceName = &quot;t_id_seq&quot;, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;t_id_seq&quot;)
@Column(name = &quot;id&quot;)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JoinColumn(name = &quot;vid&quot;, updatable = false, insertable = true,referencedColumnName = &quot;id&quot;)
private Visit visit;
@Entity
@Table(name=&quot;direction&quot;)
public class directions {
@Id
@XmlTransient
@JsonIgnore
@SequenceGenerator(name = &quot;d_id_seq&quot;, sequenceName = &quot;d_id_seq&quot;, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;d_id_seq&quot;)
@Column(name = &quot;id&quot;)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JoinColumn(name = &quot;vid&quot;, updatable = false, insertable = true,referencedColumnName = &quot;id&quot;)
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,但在 directionstest 类中,变量名分别是 visit

解决方法是将属性 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;
英文:

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 = &quot;visit&quot;,cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List&lt;directions&gt; directions;
@OneToMany(mappedBy = &quot;visit&quot;,cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
private List&lt;Test&gt; Test;

huangapple
  • 本文由 发表于 2020年9月18日 14:29:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63950409.html
匿名

发表评论

匿名网友

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

确定