繁体   English   中英

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

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

我在 spring boot 项目中使用 jpa 投影来获取一些值。 我有以下表格:

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)
);

以下实体:

@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;
}

存储库:

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();
}

观点是:

public interface RentalContractView {

Long getRentalContractId();

Long getObjectId();

Long getPropertyId();

}

当我调用存储库方法时,我可以看到我正在获取结果和确切数字,但是在调用方法getRentalContractId()时,即使应该有一个值(在数据库中检查)和另外两个值,我也得到了空值未映射字段。

我也尝试过创建类并映射它们,但仍然无法正常工作。

您必须为列提供别名以匹配界面中的名称:

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

没有命名策略,因为您没有使用实体。

暂无
暂无

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

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