简体   繁体   English

Spring Boot应用程序中的Hibernate / JPA错误连接

[英]Hibernate/JPA incorrect join in Spring Boot Application

See code below for my 2 entity classes - when I call the findAll() method from my OrigRepository class, it joins these two tables using both primary keys. 请参阅下面的代码,了解我的2个实体类-当我从OrigRepository类调用findAll()方法时,它使用两个主键将这两个表连接在一起。 I want the join to be between the primary key of the Orig table and the foreign key entry in the MsgResponse table ("OrigID") - any sugggestions? 我希望联接位于Orig表的主键和MsgResponse表中的外键条目(“ OrigID”)之间-有任何建议吗?

Orig Entity 原始实体

@Entity
@Table(name = "originator")
public class Orig {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "OrigID")
    private int OrigID;

    @OneToOne(cascade = CascadeType.ALL) 
    @JoinColumn(name = "OrigID")  
    private MsgResponse responseInfo;
}

MsgResponse Entity MsgResponse实体

@Entity
@Table(name = "message_response")
public class MsgResponse {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private int responseId;

    @Column(name = "OrigID")
    private int OrigId;

    @OneToOne(mappedBy="responseInfo")
    private Orig OrigInfo; 
}

I suggest you to see the jpa documentation here . 我建议您在这里查看jpa文档。

Example 1 should be your case 示例1应该是您的情况

Try to swap relation ownership, that is: 尝试交换关系所有权,即:

@Entity
@Table(name = "originator")
public class Orig {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "OrigID")
    private int OrigID;

    @OneToOne(mappedBy="origInfo") 
    private MsgResponse responseInfo;
}

@Entity
@Table(name = "message_response")
public class MsgResponse {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private int responseId;

    // @Column(name = "OrigID")
    // private int origId;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "OrigID")  
    private Orig origInfo; 
}

Note that the @JoinColum annotation is in now in the MsgResponse entity. 请注意, @JoinColum批注现在位于MsgResponse实体中。 This is because in a @OneToOne the join column refers to the source entity (see here ). 这是因为在@OneToOne ,连接列引用了实体(请参见此处 )。

Hope this could help. 希望这会有所帮助。

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

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