Spring boot @具有自联接类映射的Constructorresult

Spring boot @具有自联接类映射的Constructorresult,spring-boot,jpa,spring-data-jpa,Spring Boot,Jpa,Spring Data Jpa,有人能解释一下如何为使用自联接类的场景编写注释吗 在我的场景中,我使用本机查询,我需要为其传递一些输入 对于SQL结果映射,我使用@constructorresult。但不确定如何基于特定列配置自类联接 假设我们有一个非常复杂的实体ComplexObject,它与其他对象有很多关系 @Entity @Table(name = "complex_object") public class ComplexObject { @Id @GeneratedValue(strategy =

有人能解释一下如何为使用自联接类的场景编写注释吗

在我的场景中,我使用本机查询,我需要为其传递一些输入

对于SQL结果映射,我使用@constructorresult。但不确定如何基于特定列配置自类联接 假设我们有一个非常复杂的实体
ComplexObject
,它与其他对象有很多关系

@Entity
@Table(name = "complex_object")
public class ComplexObject {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id_complex_object")
    private Integer id;

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

    // More relations...

}
我希望查询仅检索此实体的id和标签

ComplexObject
中,我定义了一个新的
namedNativeRequesty
,如下所示

@NamedNativeQueries({
        @NamedNativeQuery(name = "ComplexObject.getIdAndLabel", query = "SELECT co.id_complex_object, co.label FROM complex_object", resultSetMapping = "SimpleObject")
})
命名活动请求的重要部分是
结果映射=“SimpleObject”

然后,我可以定义一个非实体的
SimpleObject
,并按如下方式匹配我的查询:

public class SimpleObject {

    private Integer id;
    private String label;

    /**
     * This constructor is very important !
     * Its signature has to match the SqlResultSetMapping defined in the entity class.
     * Otherwise, an exception will occur.
     */
    public SimpleObject(Integer id, String label) {
        this.id = id;
        this.label = label;
    }

    // Getters and setters...
}
@SqlResultSetMappings({
    @SqlResultSetMapping(name = "SimpleObject", classes = {
        @ConstructorResult(targetClass = SimpleObject.class, columns = {
            @ColumnResult(name = "id_complex_object", type = Integer.class),
            @ColumnResult(name = "label", type = String.class)
        })
    })
})
然后我可以在
ComplexObject
中定义
sqlresultsetmap
,如下所示:

public class SimpleObject {

    private Integer id;
    private String label;

    /**
     * This constructor is very important !
     * Its signature has to match the SqlResultSetMapping defined in the entity class.
     * Otherwise, an exception will occur.
     */
    public SimpleObject(Integer id, String label) {
        this.id = id;
        this.label = label;
    }

    // Getters and setters...
}
@SqlResultSetMappings({
    @SqlResultSetMapping(name = "SimpleObject", classes = {
        @ConstructorResult(targetClass = SimpleObject.class, columns = {
            @ColumnResult(name = "id_complex_object", type = Integer.class),
            @ColumnResult(name = "label", type = String.class)
        })
    })
})
完成了


namedNativeRequesty
将使用
SimpleObject
SqlResultSetMapping
来构造
SimpleObject
(通过构造函数),因此您的查询将返回一个
SimpleObject
而不是
ComplexObject
,感谢您的示例,但是我正在寻找一个场景,其中ComplexObject包含一个ComplexObject类型的属性,只要它在数据库中有一个表示,它就不会对您拥有的对象类型产生任何影响。如果您的
ComplexObject
包含一个
ComplexObject
,您将只能检索它的ID,因为您要使用
ConstructorResult
NamedNativeRequesty