简体   繁体   English

JPA投影得到结果但与界面视图中的方法不匹配

[英]JPA projection getting results but not matching the columns with the methods in the interface view

I am using jpa projection in spring boot project to get some values.我在 spring boot 项目中使用 jpa 投影来获取一些值。 I have the following TABLE:我有以下表格:

create table `rental_contract`
(
`rental_contract_id` bigint(20) not null,
`object_id`          bigint(20) default null,
`property_id`        bigint(20) default null,
primary key (rental_contract_id),
foreign key (property_id, object_id) references property_object (property_id, object_id)
);

The following entity:以下实体:

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@NamedEntityGraph(
    name = "RentalContract.WithPropertyObjects",
    attributeNodes = {
            @NamedAttributeNode(value = "propertyObject", subgraph = 
"RentalContract.WithPropertyObjects.Properties")
    },
    subgraphs = {
            @NamedSubgraph(name = "RentalContract.WithPropertyObjects.Properties",
                    attributeNodes = {
                            @NamedAttributeNode(value = "property")
                    })
    }
)
public class RentalContract {

@Id
private Long rentalContractId;


@JoinColumns({
        @JoinColumn(name = "object_id"),
        @JoinColumn(name = "property_id"),
})
@ManyToOne(fetch = FetchType.LAZY)
private PropertyObject propertyObject;
}

The repository:存储库:

public interface RentalContractRepository extends JpaRepository<RentalContract, Long> {

Optional<RentalContract> 
findByPropertyObject_ObjectIdAndPropertyObject_Property_PropertyId(Long objectId, Long 
propertyId);

@Override
@EntityGraph(value = "RentalContract.WithPropertyObjects")
List<RentalContract> findAll();

@Query(value = "SELECT * from rental_contract", nativeQuery = true)
List<RentalContractView> getAllWithObjectsAndProperties();
}

And the view is:观点是:

public interface RentalContractView {

Long getRentalContractId();

Long getObjectId();

Long getPropertyId();

}

When I am calling the repository method I can see that I am getting the results and the exact number, but when calling the method getRentalContractId() I am getting null even though there should be a value (checked in the database) and the two other fields are not being mapped.当我调用存储库方法时,我可以看到我正在获取结果和确切数字,但是在调用方法getRentalContractId()时,即使应该有一个值(在数据库中检查)和另外两个值,我也得到了空值未映射字段。

I tried also with creating class and map them but still not working.我也尝试过创建类并映射它们,但仍然无法正常工作。

You have to provide aliases for the columns to match the names in the interface:您必须为列提供别名以匹配界面中的名称:

@Query(value = "SELECT " +
               "rental_contract_id as rentalContractId " +
               "object_id          as objectId " +
               "property_id        as propertyId" +
               "from rental_contract", nativeQuery = true)

There is no naming strategy because you are not using entities.没有命名策略,因为您没有使用实体。

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

相关问题 Spring Data JPA和Projection获得TupleConverter的ConverterNotFoundException - Spring Data JPA and Projection getting ConverterNotFoundException for TupleConverter Spring JPA:在同一查询界面上使用多个投影 - Spring JPA: Using multiple projection on same query interface Spring JPA 基于接口的投影在连接实体上返回 null - Spring JPA Interface based projection returns null on a joined entity spring 数据 jpa - 基于接口的投影中的自定义类型转换 - spring data jpa - Custom type conversion in interface-based projection 字符串集在 Spring JPA 投影接口中不正常 - Set of String is not acting normal in Spring JPA Projection Interface Spring 数据 JPA 投影 - 获取投影列的所有行 - Spring data JPA projection - get all rows for the projected columns Spring Data JPA从具有查询的实体获取投影 - Spring Data JPA getting a Projection from an Entity with a Query Spring JPA 投影内侧投影 - Spring JPA Projection inside of Projection 为什么在通过连接 JPA 中的两个不相关实体来使用投影获取数据时未设置投影字段? - Why projection fields are not getting set while fetching data using projection by joining two unrelated entities in JPA? 在JPA和存储库接口中获取实体类 - Getting Entity Class in JPA and Repository Interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM