简体   繁体   English

如何使用本机查询在 JPA 中执行嵌套连接

[英]How to perform nested Join in JPA with native query

I have existing table student, extras & address with this structure我有这个结构的现有表学生、演员和地址

Table student
------------------------------------------
id  |   name    
------------------------------------------
1   | John          
------------------------------------------

Table extras
-----------------------------------------------------------------
id  |   student_id  |  extras_key               | extras_value
-----------------------------------------------------------------
1   | 1             | class                     | X3
2   | 1             | address_id                | addr-2
-----------------------------------------------------------------

Table address
--------------------------------------------
addr_id     |   name            | city
--------------------------------------------
addr-2      | Office            | San Jose
--------------------------------------------

how to join those tables on JPA Hibernate?如何在 JPA Hibernate 上加入这些表? My current code is我目前的代码是

Student Entity class学生实体类

@Entity
@Table(name = "student")
@Data
public class Student implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "name", nullable = false, columnDefinition = "TEXT")
    private String name;

//    @OneToOne
//    @JoinColumn(name = "id", referencedColumnName = "student_id")
//    private Extras extras;

    @OneToMany(mappedBy = "studentId")
    private Set<Extras> extras;
}

Extras Entity class:附加实体类:

@Data
@Entity
@Table(name="extras")
public class Extras implements Serializable {
    @Id
    @Column(name="id")
    private int id;

    @Column(name="student_id", nullable = false)
    private int studentId;

    @Column(name="extras_key", nullable = false)
    private String extrasKey;

    @Column(name="extras_value", nullable = false)
    private String extrasValue;

    @OneToOne(optional = false)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "extras_value", referencedColumnName = "addr_id", insertable = false, updatable = false)
    private Address address;
}

and Address entity class和地址实体类

@Entity
@Table(name="address")
@Data
public class Address implements Serializable {
    @Id
    @Column(name="addr_id")
    private String addrId;

    @Column(name="name")
    private String name;

    @Column(name="city")
    private String city;

}

and here is my repository class这是我的存储库类

@Repository
public interface StudentRepository extends CrudRepository<Student, Integer> {
    @Query(value = "select * from student " +
            "JOIN extras ON student.id=extras.student_id " +
            "JOIN address ON address.addr_id = extras.extras_value", nativeQuery = true)
    List<Student> findAllData();
}

but when I use OneToMany in Student Entity, I got Exception "org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jpa.belajarjpa.enitities.Student.extras, could not initialize proxy - no Session" .但是当我在 Student Entity 中使用 OneToMany 时,我得到了 Exception "org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jpa.belajarjpa.enitities.Student.extras, could not initialize proxy - no Session" But when I use OneToOne in Student Entity, I don't face exception but got false result, Student(id=1, name=john, extras=null), and when I execute this query, It show correct result SELECT * FROM student JOIN extras ON student.id=extras.student_id JOIN address ON address.addr_id = extras.extras_value但是当我在 Student Entity 中使用 OneToOne 时,我没有遇到异常但得到了错误的结果,Student(id=1, name=john, extras=null),当我执行这个查询时,它显示了正确的结果SELECT * FROM student JOIN extras ON student.id=extras.student_id JOIN address ON address.addr_id = extras.extras_value

You need to use join fetching with a HQL query like this:您需要使用带有 HQL 查询的连接获取,如下所示:

@Repository
public interface StudentRepository extends CrudRepository<Student, Integer> {
    @Query("FROM Student s " +
            "JOIN FETCH s.extras e " +
            "JOIN FETCH e.address")
    List<Student> findAllData();
}

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true is an anti-pattern. spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true是一种反模式。 I wouldn't use it if I were you.如果我是你,我不会使用它。

通过在属性中添加 spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true 解决

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

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