休眠多个表的一对多映射

时间:2020-09-18 06:29:58

标签: java spring-boot hibernate maven hibernate-mapping

val conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
val streamingContext = new StreamingContext(conf, Seconds(1))
@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;

你好,我是新来的冬眠 我正在尝试映射OneToMany Visit-> Test and Visit-> direction但出现错误 @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;

一次访问可以有多个方向和测试 我该如何实现呢?

请帮助我!

1 个答案:

答案 0 :(得分: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;