【发布时间】:2019-12-30 13:50:12
【问题描述】:
我正在将 Querydsl 与 spring data jpa 一起使用。我已经实现了自定义存储库来找出人名。 person 实体如下所示:
@Entity
@Table(name = "persons")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Long id;
@Column(name = "initial")
private String initial;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "archived")
private Boolean archived;
//Other columns here.. && Getter setter goes here
}
实现的自定义存储库:
@Repository
public class PersonRepositoryImpl extends QueryDslRepositorySupport
implements PersonRepositoryCustom {
public PersonRepositoryImpl() {
super(Person.class);
}
@Override
public List<String> getPersonNames() {
return getQuerydsl()
.createQuery()
.from(QPerson.person)
.where(QPerson.person.archived.eq(Boolean.FALSE))
.select(/*
want to select as a full name like
initial + firstName + lastName
*/)
.fetch();
}
}
在选择表达式中,我想加入三列来构造全名。如果数据库有如下记录 -
id | initial | firstName | lastName | archived | ...
----------------------------------------------------
1 | "Mr" | "John" | "Snow" | 0 | ...
那么我想选择“Mr John Snow”。 有什么办法吗?
我不想选择整条记录来连接值,因为实体有其他列和很多关联,加载整条记录不好。
【问题讨论】:
-
搜索JPA投影。
标签: java jpa spring-data-jpa querydsl