如何选择弹簧数据中的字段?(How to select fields in spring data?)

我的实体如下。

@Entity @Table(name = "BankProduct") public class Product { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @ManyToOne private ProductUseType type; public Long getId() { return id; } public void setId(Long id) { this.id = id; } @ManyToOne private ProductSerial serial; public String getName() { return name; } public void setName(String name) { this.name = name; } public ProductUseType getType() { return type; } public void setType(ProductUseType type) { this.type = type; } public ProductSerial getSerial() { return serial; } public void setSerial(ProductSerial serial) { this.serial = serial; } }

我的控制器是:

@RestController public class DEmoController { @Autowired private ProductRepository productRepository; @GetMapping("/products") public Returns products() { return new Returns(ReturnStatus.SUCCESS.getStatus(), productRepository.findAll(), null); } }

它将加载产品的类型和系列。 我只能加载类型但不加载序列? 我不想将fetch=FetchType.LAZY添加到串行中,因为如果下一次我要加载串行但不加载类型,它将会很糟糕。

I have entity as follow.

@Entity @Table(name = "BankProduct") public class Product { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @ManyToOne private ProductUseType type; public Long getId() { return id; } public void setId(Long id) { this.id = id; } @ManyToOne private ProductSerial serial; public String getName() { return name; } public void setName(String name) { this.name = name; } public ProductUseType getType() { return type; } public void setType(ProductUseType type) { this.type = type; } public ProductSerial getSerial() { return serial; } public void setSerial(ProductSerial serial) { this.serial = serial; } }

My controller is :

@RestController public class DEmoController { @Autowired private ProductRepository productRepository; @GetMapping("/products") public Returns products() { return new Returns(ReturnStatus.SUCCESS.getStatus(), productRepository.findAll(), null); } }

It will load both of type and serial of product. Can I only load type but not to load serial? I don't want to add fetch=FetchType.LAZY to serial, because if next time I want to load serial but not to load type, it will be terrible.

最满意答案

检查Projection 界面

创建一个接口ProductProjection

interface ProductProjection { String getName(); String getType(); }

并在您的Repository添加一个方法

List<ProductProjection> findAllProjection()

Check the Projection interface

Create a interface ProductProjection

interface ProductProjection { String getName(); String getType(); }

and add a method in you Repository

List<ProductProjection> findAllProjection()

如何选择弹簧数据中的字段?(How to select fields in spring data?)

我的实体如下。

@Entity @Table(name = "BankProduct") public class Product { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @ManyToOne private ProductUseType type; public Long getId() { return id; } public void setId(Long id) { this.id = id; } @ManyToOne private ProductSerial serial; public String getName() { return name; } public void setName(String name) { this.name = name; } public ProductUseType getType() { return type; } public void setType(ProductUseType type) { this.type = type; } public ProductSerial getSerial() { return serial; } public void setSerial(ProductSerial serial) { this.serial = serial; } }

我的控制器是:

@RestController public class DEmoController { @Autowired private ProductRepository productRepository; @GetMapping("/products") public Returns products() { return new Returns(ReturnStatus.SUCCESS.getStatus(), productRepository.findAll(), null); } }

它将加载产品的类型和系列。 我只能加载类型但不加载序列? 我不想将fetch=FetchType.LAZY添加到串行中,因为如果下一次我要加载串行但不加载类型,它将会很糟糕。

I have entity as follow.

@Entity @Table(name = "BankProduct") public class Product { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @ManyToOne private ProductUseType type; public Long getId() { return id; } public void setId(Long id) { this.id = id; } @ManyToOne private ProductSerial serial; public String getName() { return name; } public void setName(String name) { this.name = name; } public ProductUseType getType() { return type; } public void setType(ProductUseType type) { this.type = type; } public ProductSerial getSerial() { return serial; } public void setSerial(ProductSerial serial) { this.serial = serial; } }

My controller is :

@RestController public class DEmoController { @Autowired private ProductRepository productRepository; @GetMapping("/products") public Returns products() { return new Returns(ReturnStatus.SUCCESS.getStatus(), productRepository.findAll(), null); } }

It will load both of type and serial of product. Can I only load type but not to load serial? I don't want to add fetch=FetchType.LAZY to serial, because if next time I want to load serial but not to load type, it will be terrible.

最满意答案

检查Projection 界面

创建一个接口ProductProjection

interface ProductProjection { String getName(); String getType(); }

并在您的Repository添加一个方法

List<ProductProjection> findAllProjection()

Check the Projection interface

Create a interface ProductProjection

interface ProductProjection { String getName(); String getType(); }

and add a method in you Repository

List<ProductProjection> findAllProjection()