我对jpa中的Subquery类有疑问。 我需要创建带有两个自定义字段的子查询,但子查询没有多选方法,而select方法有表达式输入参数(在查询中这是选择)和constact方法不合适。
另外我对加入子查询结果有疑问,有可能吗?怎么样?
我有:
Chain Enitity
public class Chain {
@Id
@Column(name = "chain_id")
@GeneratedValue(generator = "seq_cha_id", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "seq_cha_id", sequenceName = "SEQ_CHA_ID", allocationSize = 1)
private Long id;
@Column(name = "user_id")
private Long userId;
@Column(name = "operator_id")
private Long operatorId;
@Column(name = "subject")
private String subject;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "chain")
private List<Message> messages;
@Column(name = "status")
private Status status;
public Long getOperatorId() {
return operatorId;
}
public void setOperatorId(Long operatorId) {
this.operatorId = operatorId;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String theme) {
this.subject = theme;
}
public List<Message> getMessages() {
return messages;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
}
消息提供
public class Message {
@Id
@Column(name = "message_id")
@GeneratedValue(generator = "seq_mess_id", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "seq_mess_id", sequenceName = "SEQ_MESS_ID", allocationSize = 1)
private Long id;
@Column(name = "user_id")
private Long userId;
@Column(name = "message", nullable = true, length = 4000)
private String message;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "chain_id")
private Chain chain;
@Column(name = "creation_date")
private Date date;
@Column(name = "status")
private Status status;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Chain getChain() {
return chain;
}
public void setChain(Chain chain) {
this.chain = chain;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
查询包装
public class MessageWrapper {
private final Long chainId;
private final Long messageId;
public MessageWrapper(Long chainId, Long messageId) {
this.chainId = chainId;
this.messageId = messageId;
}
}
我需要创建这个查询(这是查询的一部分,我从谓词中得到的另一部分。JPQL不合适)
select ch.*
from hl_chain ch,
(select mes.CHAIN_ID,
max(message_id) message_id
from HL_MESSAGE mes
group by chain_id) mes
where mes.chain_id = ch.CHAIN_ID
order by message_id;
在子查询中我做
Subquery<MessageWrapper> subquery = criteriaQuery.subquery(MessageWrapper.class);
Root<Message> subRoot = subquery.from(Message.class);
subquery.select(cb.construct(MessageWrapper.class, subRoot.get(Message_.chain), cb.max(subRoot.get(Message_.id))));
但是,子查询没有在params中使用CompoundSelection进行选择,我不能使用CriteriaBuilder构造方法。
答案 0 :(得分:1)
映射为实体的数据库视图将完成您需要的工作。 它被映射为一个普通的表,只用标签@View 代替。
我对我的项目也做了同样的事情。
答案 1 :(得分:0)
您可以从JPA调用本地查询,例如:
Query q = em.createNativeQuery("SELECT p.firstname, p.lastname FROM Person p");
List<Object[]> persons= q.getResultList();
for (Object[] p : persons) {
System.out.println("Person "
+ p[0]
+ " "
+ p[1]);
}
答案 2 :(得分:-1)
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8080:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data: {}