比如说我有一个SQL表
create table CUSTOMER
(
first_name VARCHAR2(30) not null,
last_name VARCHAR2(30) not null,
age VARCHAR2(30) not null,
credit_card_num VARCHAR2(30) not null UNIQUE
);
我想得到一个Java Map,其中map键是
CUSTOMER.credit_card_num
的值并且地图值是
CUSTOMER
的所有属性因此,例如:
1234134cardnumber -> Customer(first_name: Jack, age: 23, credit_card_num: 1234134cardnumber),
123faef -> Customer(first_name: Mike, age: 43, credit_card_num: 123faef)
等等...
我会接受在JPA或本机查询中执行此操作的任何方法
更新资料
如https://stackoverflow.com/users/1025118/vlad-mihalcea所指,可以由https://vladmihalcea.com/why-you-should-use-the-hibernate-resulttransformer-to-customize-result-set-mappings/完成
最佳答案
由于这是一个非常常见的要求,因此我决定将此答案转换为a full-blown article on my blog。
有两个解决方案。
假设您使用以下Post
实体:
@Entity(name = "Post")
@Table(name = "post")
public class Post {
@Id
private Long id;
private String title;
@Column(name = "created_on")
private LocalDate createdOn;
public Long getId() {
return id;
}
public Post setId(Long id) {
this.id = id;
return this;
}
public String getTitle() {
return title;
}
public Post setTitle(String title) {
this.title = title;
return this;
}
public LocalDate getCreatedOn() {
return createdOn;
}
public Post setCreatedOn(LocalDate createdOn) {
this.createdOn = createdOn;
return this;
}
}
并且您的数据库中保留了以下实体:
entityManager.persist(
new Post()
.setId(1L)
.setTitle("High-Performance Java Persistence eBook has been released!")
.setCreatedOn(LocalDate.of(2016, 8, 30))
);
entityManager.persist(
new Post()
.setId(2L)
.setTitle("High-Performance Java Persistence paperback has been released!")
.setCreatedOn(LocalDate.of(2016, 10, 12))
);
entityManager.persist(
new Post()
.setId(3L)
.setTitle("High-Performance Java Persistence Mach 1 video course has been released!")
.setCreatedOn(LocalDate.of(2018, 1, 30))
);
entityManager.persist(
new Post()
.setId(4L)
.setTitle("High-Performance Java Persistence Mach 2 video course has been released!")
.setCreatedOn(LocalDate.of(2018, 5, 8))
);
entityManager.persist(
new Post()
.setId(5L)
.setTitle("Hypersistence Optimizer has been released!")
.setCreatedOn(LocalDate.of(2019, 3, 19))
);
使用Java 8流
以下是您按照问题要求返回
Map
的方法:Map<Long, Post> postByIdMap = entityManager
.createQuery(
"select p " +
"from Post p ", Post.class)
.getResultStream()
.collect(
Collectors.toMap(
Post::getId,
Function.identity()
)
);
assertEquals(
"High-Performance Java Persistence eBook has been released!",
postByIdMap.get(1L).getTitle()
);
assertEquals(
"Hypersistence Optimizer has been released!",
postByIdMap.get(5L).getTitle()
);
使用休眠
ResultTransformer
使用
ResultTransformer
可以实现相同的目标:Map<Long, Post> postByIdMap = (Map<Long, Post>) entityManager
.createQuery(
"select p " +
"from Post p ")
.unwrap(org.hibernate.query.Query.class)
.setResultTransformer(
new ResultTransformer() {
Map<Long, Post> result = new HashMap<>();
@Override
public Object transformTuple(Object[] tuple, String[] aliases) {
Post post = (Post) tuple[0];
result.put(
post.getId(),
post
);
return tuple;
}
@Override
public List transformList(List collection) {
return Collections.singletonList(result);
}
}
)
.getSingleResult();
assertEquals(
"High-Performance Java Persistence eBook has been released!",
postByIdMap.get(1L).getTitle()
);
assertEquals(
"Hypersistence Optimizer has been released!",
postByIdMap.get(5L).getTitle()
);
有关
ResultTransformer
的更多详细信息,请签出this article。