gpt4 book ai didi

java - 我可以在 JPA 中查询对象吗

转载 作者:行者123 更新时间:2023-11-29 05:01:51 27 4
gpt4 key购买 nike

我正在使用 JPA 和 Spring Data,

有一个具有字段 UserDetail createdBy 的类事件,我想查询由指定用户创建的事件。这是我的查询

@Query("SELECT e FROM  event WHERE e.created_by = :userdetail")
Collection<Event> getEventsCreatedByUser(@Param("userdetail")UserDetail user);

当我在 Dao 类中使用此查询启动测试时,出现错误 org.hibernate.hql.internal.ast.QuerySyntaxException: event is not mapped [SELECT e FROM event WHERE e.created_by = :userdetail]

这是实体类,created_by 在 BaseEntityAudit 中,我测试了也扩展了 BaseEntityAudit 的 User 类,它工作得很好,pgadmin 也显示 createdBy 列存在于 Event 表中

@Entity
@Table(name = "Event")
@AttributeOverride(name = "id", column = @Column(name = "event_id",
nullable = false, columnDefinition = "bigserial"))
public class Event extends BaseEntityAudit {
@Embedded
private Point point;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "event_comments")
private Collection<Comment> comments = new ArrayList<Comment>();
@OneToMany
@JoinTable(name = "\"users_tagged_at_event\"")
private Collection<UserDetail> usersTagged = new ArrayList<UserDetail>();
public Event(UserDetail createdBy, Point point) {
super();
this.point = point;
setCreatedBy(createdBy);
}

还有 BaseEntityAudit

public abstract class BaseEntityAudit extends BaseEntity {
@Column(name = "created_at")
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
@Column(name = "created_by", length = 20)
private UserDetail createdBy;
@Column(name = "updated_at")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedAt;
@Column(name = "updated_by", length = 20)
private UserDetail updatedBy;
//geters/setters ...
/**
* Sets createdAt before insert
*/
@PrePersist
public void setCreationDate() {
this.createdAt = new Date();
}
/**
* Sets updatedAt before update
*/
@PreUpdate
public void setChangeDate() {
this.updatedAt = new Date();
}
}

更新

Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: created_by of: home.map.events.entity.Event [SELECT e FROM  home.map.events.entity.Event e WHERE e.created_by = :userdetail]

最佳答案

您忘记在 from 子句中提及 event e。我猜你有事件表和事件类(由 @pL4Gu33 指出)。所以你需要使用Event而不是event

@Query("SELECT e FROM Event e WHERE e.createdBy = :userdetail")
use---^ ^- added ^---- updated

关于java - 我可以在 JPA 中查询对象吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31876072/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com