【发布时间】:2016-03-27 19:36:21
【问题描述】:
假设我想要一个方法,获取超级主客户,有id=0。
我有客户类:
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
protected Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%d, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
According to dox,我应该创建两个额外的类/接口。
所以我有CustomerCustom接口:
public interface CustomerCustom {
Customer getVeryMainCustomer();
}
在哪里声明了一个方法getVeryMainCustomer。
然后我的曝光仓库界面变成如下:
public interface CustomerRepository extends CrudRepository<Customer, Long>, CustomerCustom {
List<Customer> findByLastName(String lastName);
}
它同时扩展了CrudRepository 和我的CustomerCustom。
但接下来我应该实现CustomerCustom。但是该怎么做呢?
我写的
public class CustomerCustomImpl implements CustomerCustom {
@Override
public Customer getVeryMainCustomer() {
return null;
}
}
bot 不知道,在实现中要写什么。如何接触客户?
【问题讨论】:
-
该方法应该做什么?您是否考虑过使用
@Query?什么是表结构或Customer? -
Customer的表结构是JPA在
Customer类定义的基础上自动定义的。该方法应该选择ID = 0的客户。是的,如果可能的话,我想使用@Query
标签: java spring spring-data-jpa