如何使用PagingAndSortingRepository不返回特定列 [英] How to not return an specific column using PagingAndSortingRepository

查看:161
本文介绍了如何使用PagingAndSortingRepository不返回特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个标题可能听起来有点令人困惑,但我不知道如何在标题上总结我的问题。

I know the title may sound a bit confusing, but I did not know how to summarize my problem on the title.

我的主要问题是我没有想要使用PagingAndSortingRepository返回特定列。想象一下以下场景:

My main problem is that I do not want to return an specific column using PagingAndSortingRepository. Imagine the following scenario:

我有两个实体,一个叫做用户:

I have two entitys, one called Users:

@Entity
@Table(name = "users")
public class User implements Serializable {

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

  @NotBlank()
  @Column(name = "name")
  private String name;

  @Column(name = "password")
  private String password;

}

还有一个叫汽车:

@Entity
@Table(name = "car")
public class Car implements Serializable {

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

  @NotBlank()
  @Column
  private String name;

  @ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
  @JoinColumn(name = "owner_id", referencedColumnName = "id")
  private User owner;

}

我想使用用户名返回汽车列表PagingAndSortingRepository。使用以下接口我可以实现:

I want to return a list of car by user's name using PagingAndSortingRepository. Using the following interface I can achieve that:

public interface CarRepository extends PagingAndSortingRepository<Car, Long> {

  List<Car> findByOwner_name(String name);

}

然而,通过所述界面,结果也将返回用户的密码就可以了。我可以在列表的每个元素上使用for并将密码设置为null,但在存在大量汽车的用户的情况下会很麻烦。我怎么能这样做,以便列表的结果带有特定的列,密码,对用户没有任何价值?

However, with said interface the result will also return the user's password on it. I could use a for on each element of the list and set the password as null, but it would be troublesome in a scenario where there is a user with lots of cars. How could I make it so that the result of the list come with the specific column, password, without any value to the user?

提前谢谢=)。

ps:我想要返回投影列表而不是列表Car,但是我不知道怎么说它只是来自类的一个属性应为null。

ps: I thought about returning a list of projection instead of a list of Car, but I do not know how to say it to the projection that just one attribute from the class should be null.

推荐答案

为此需要使用预测。详细记录了此处

You need to use Projections for this purpose. It is documented in detail here

创建一个接口,其中包含要包含在要返回的实体中的字段。

例如,在您的情况下,它将是这样的

Create an interface with fields you want to include in the entity to be returned.
For example, in your case it would be like this

interface CarSummary {

  String getName();
  Long getId();
  UserSummary getUser();

  interface UserSummary {
    String getName();
    Long getId();
  }
}

并像这样修改你的资料库

and modify your repository like this

public interface CarRepository extends PagingAndSortingRepository<Car, Long> {
    Collection<CarSummary> findByOwner_name(String name);  
}

这篇关于如何使用PagingAndSortingRepository不返回特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆