簡體   English   中英

在JHipster中使用Spring JPA Projection

[英]Using Spring JPA Projection with JHipster

在通過JHipster版本4.14.5生成的項目中使用Spring Data JPA Projections遇到了一些困難。

我遵循Spring關於如何使用JPA儲存庫進行投影的方向,但是我沒有取得任何成功。 當我嘗試使用投影時,倉庫給我null值。

由於我不是JHipster煙斗的大鑒賞家 ,所以我希望那里的人可以幫助我。 我的實體

@Entity
@Table(name = "research")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Research implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Column(name = "answer", nullable = false)
private String answer;

@OneToOne(optional = false)
@NotNull
@JoinColumn()
private Question question;
// getters and setters

實體投影:

public interface ResearchSimple {
    Long getId();
    String getAnswer();
}

實體的回購:

@Repository
public interface ResearchRepository
extends JpaRepository<Research, Long> {
    @Query("SELECT r FROM Research r)
    List<ResearchSimple> findAllAsSimple();
}

檢測結果

List<ResearchSimple> result = repo.findAllAsSimple();
assertEquals(result.size, dbSize); // OK
ResearchSimple simple = result.get(0);
assertNotNull(simple); // OK
assertNotNull(simple.getId); // FAIL!
assertNotNull(simple.getAnswer); // FAIL!

調試通過simple獲得的值我已經注意到進行了投影,但是我無法訪問它的值。 請注意,在simpleadvised br.com.pixinside.projection.ResearchSimple類。

org.springframework.aop.framework.ProxyFactory: 2 interfaces    [br.com.pixinside.projection.ResearchSimple, org.springframework.data.projection.TargetAware]; 3 advisors [org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [Pointcut.TRUE]; advice [org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor@1d62a0b], org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [Pointcut.TRUE]; advice [org.springframework.data.projection.ProxyProjectionFactory$TargetAwareMethodInterceptor@3b89f41a], org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [Pointcut.TRUE]; advice [org.springframework.data.projection.ProjectingMethodInterceptor@198453c9]]; targetSource [SingletonTargetSource for target object [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@319267ad]]; proxyTargetClass=false; optimize=false; opaque=true; exposeProxy=false; frozen=false

如果要在手動查詢中使用投影,則應使用與投影界面中的字段名稱匹配的別名。 @Query("SELECT r.id as id, r.answer as answer FROM Research r)或者只是跳過@Query,並使用List<ResearchSimple> findAllSimplifiedBy();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM