简体   繁体   English

从@Query批注中获取具有nativeQuery true J​​PARepository的两列

[英]Get two columns from @Query annotation having nativeQuery true JPARepository

I have a entity user 我有一个实体用户

@Entity
@Table(name="user")
public class User{

@Id
@Column(name="id")
private int id;
@Column(name="reward")
private double reward;

@Column(name="reward_id")
private double rewardId;

//Getter & Setters
}

Now what i need is to get reward and rewardId from @Query annotation from other table in DB at once. 现在,我需要立即从数据库中其他表的@Query批注中获取reward和rewardId。

I have tried using 我尝试使用

@Query(value="SELECT reward,reward_id from Table_name")
 public List getRewards();

but when run it says no property associated with bean. 但是运行时它说没有与bean相关联的属性。 And when i Select only single column it give in my query it runs perfectly 当我只选择单个列时,它会在我的查询中给出完美运行

Please help as i am new to JPA and Hibernate Thanks in advance!! 请帮助,因为我是JPA和Hibernate的新手!

Just use Projection : 只需使用Projection即可

public interface RewardProjection {
    Double getReward();
    Double getRewardId();
}

@Query(value = "select t.reward as reward, t.reward_id as rewardId from table_name t", nativeQuery = true)
public List<RewardProjection> getRewards();

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

相关问题 Spring Data JPA @Query注释,nativeQuery = true, - Spring Data JPA @Query annotation, nativeQuery = true, 在JpaRepository中不使用@Query注释 - Use @Query annotation not in the JpaRepository 注释类型Query的属性nativeQuery未定义 - The attribute nativeQuery is undefined for the annotation type Query 在哪些情况下,JpaRepository 会自动创建查询,而无需使用 @Query 批注 - In which cases does JpaRepository automatically create the query without you having to use @Query annotation 使用nativeQuery = true在JPA Reposity中执行@Query时出现ResultSet错误 - ResultSet error executing @Query in JPA Reposity, using nativeQuery = true 使用@Query 时将“nativeQuery”设置为“true”有什么好处? - What are the advantages of setting 'nativeQuery' to 'true' when using @Query? 在JPARepository中获取汇总的查询结果 - Get aggregated query result in JPARepository 我可以在 JpaRepository nativeQuery 中使用枚举参数吗? - Can I use enum parameter into JpaRepository nativeQuery? 嵌套的FETCH JOIN如何使用JpaRepository和@Query注释在Hibernate上工作? - How nested FETCH JOIN works on Hibernate using JpaRepository and @Query annotation? 有什么方法可以在Spring Boot应用程序中使用@Query注释从DATA JPA获取特定列 - Is there any way, to get specific columns from DATA JPA , using @Query annotation in spring boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM