Sure, here’s the translation: 使用接口从JAVA JPA返回带有名称的几个实体变量

huangapple go评论150阅读模式
英文:

JAVA JPA returning few Entity variables with their names using interface

问题

以下是您要求的翻译内容:

假设我有一个简单的类:

public class Human{
    private String id;
    private String name;
    private String surname;
    private String something;
    private String something2;
    
    //getters setters
}

并且我声明了一个接口:

public interface IHuman{
    String getId();
    String getName();
}

在我的仓库中,我调用了:

@Query("select r from Human r")
Page<IHuman> getIdAndName();

这将返回一个包含 id 和 name 属性的 IHuman 数组,但在转换为 JSON 时,它没有变量名称,只返回值:

"content":[["id1","jeff"],["id2","Jones"],...]

所以我的问题是:是否可能使用接口将变量连接到它们对应的值,以获得类似以下的结果:

"content":[{"id":"id1","name":"jeff"},{"id":"id2","name":"Jones"},...]
英文:

Lets say I have a simple class:

public class Human{

private String id;
private String name;
priavte String surname;
priavte String something;
priavte String something2;

//geters setters

}

and I have declared an Interface

public interface IHuman{
String getId;
String getName;
}

in my repository I call:

@Query(&quot;select r from Human r&quot;)
Page&lt;IHuman&gt; getIdAndName();

Which in turn returns me an array of IHuman atributes with id and name, but when parsing to JSON it has no variable names and returns only the values:

&quot;content&quot;:[[&quot;id1&quot;,&quot;jeff&quot;],[&quot;id2&quot;,&quot;Jones&quot;],...]

so my question would be: is it possible to connect variables to their corresponding values using interface, to get something like:

&quot;content&quot;:[[&quot;id&quot;:&quot;id1&quot;,&quot;name&quot;:&quot;jeff&quot;],[&quot;id&quot;:&quot;id2&quot;,&quot;name&quot;:&quot;Jones&quot;],...]

答案1

得分: 1

以下是您要翻译的内容:

您正在编写以下代码:

@Query("select r from Human r")
Page<IHuman> getIdAndName();

这将返回整个 `Human` 对象

但是您只需要 id 和 name因此在查询中只检索这些内容就像这样

```java
@Query("select r.id, r.name from Human r")
Page<IHuman> getIdAndName();

这样应该可以将您的接口与值进行映射
英文:

You are writing

@Query(&quot;select r from Human r&quot;)
Page&lt;IHuman&gt; getIdAndName();

Which would return entire Human object.

But you only need id and name, so only retrieve that in the Query, like

@Query(&quot;select r.id, r.name from Human r&quot;)
Page&lt;IHuman&gt; getIdAndName();

And That should map your interface with your values.

答案2

得分: 0

我真的不知道这是怎么/为什么起作用的,但是没问题:
将查询更改为:

 @Query("SELECT r.id as Id, r.name as Name from human r")
 Page<IHuman> getIdAndName();

然后在我的 IHuman 类中添加:

@Value("#{target.Id}")
String getId();
@Value("#{target.Name}")
String getName();
英文:

I legit don't know how/why this worked, but ok:
switched Query to:

 @Query(&quot;SELECT r.id as Id, r.name as Name from human r)
 Page&lt;IHuman&gt; getIdAndName();

and in my IHuman class I added:

@Value(&quot;#{target.Id}&quot;)
String getId();
@Value(&quot;#{target.Name}&quot;)
String getName();

huangapple
  • 本文由 发表于 2020年3月16日 17:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/60703083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定