[英]spring data repository with native named query on unMapped POJO
I have a POJO named company which is mapped to a database table named company I have a native query with only two fields (company from legacy reasons has lots of eager mapping which I would like to remove)我有一个名为 company 的 POJO,它映射到一个名为 company 的数据库表我有一个只有两个字段的本机查询(来自遗留原因的公司有很多我想删除的急切映射)
the system is working with spring data I would like to map as a native query and only fill these two scalars to my list of companies how can I do it using the CompanyRepository?系统正在处理 spring 数据我想映射为本地查询并且只将这两个标量填充到我的公司列表中我如何使用 CompanyRepository 来完成它?
@Entity
@Audited
@Table(name = "company")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@NamedNativeQuery(
name = "getDefaultBuyers",
query =
"SELECT c.id as id ,c.name as name"
+ "FROM complex query with non mapped tables "
resultSetMapping = "getDefaultBuyersMapping")
@SqlResultSetMapping(
name = "getDefaultBuyers",
columns = {@ColumnResult(name = "name"),@ColumnResult(name = "id")})
public class Company {
@Column(name = "id", nullable = false, length = 32)
protected String id;
@Column(name = "name", nullable = false)
protected String name;
//diffrent properties
}
@Repository
public interface CompanyRepository
extends JpaRepository<Company, Long>, JpaSpecificationExecutor<Company>{}
EDIT I dont think it's elegant, but this one works, can someone help me do it more elegant编辑我不认为它很优雅,但是这个有效,有人可以帮我做得更优雅吗
@Repository
public interface CompanyRepository
extends JpaRepository<Company, Long>, JpaSpecificationExecutor<Company>{
@Query(
value =
"SELECT c.id as id ,c.name as name FROM bidder_config b, native_version n, company c WHERE ...",
nativeQuery = true)
Object[][] getDefaultBuyers(); ```
apparently there is an issue in spring data which resolved like this :显然弹簧数据中存在一个问题,解决方法如下:
@Repository
public interface CompanyRepository
extends JpaRepository<Company, Long>, JpaSpecificationExecutor<Company>{
@Query(nativeQuery = true)
List<DefaultBuyerDTO> getDefaultBuyers();
}
see https://github.com/spring-projects/spring-data-examples/tree/master/jpa/jpa21#support-for-custom-sqlresultsetmapping-with-constructorresult for more info有关更多信息,请参阅https://github.com/spring-projects/spring-data-examples/tree/master/jpa/jpa21#support-for-custom-sqlresultsetmapping-with-constructorresult
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.