【发布时间】:2012-05-20 16:29:41
【问题描述】:
我有 2 张桌子 第一桌 - 活动 第二个表 - 类别
@Entity
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long eventId;
private String name;
@ManyToOne
@JoinColumn(name = "category_id")
private EventCategory category;
//getters and setters
}
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "CATEGORY" }))
public class EventCategory {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long categoryId;
@NotNull
private String category;
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
private List<Event> events;
}
现在我想要所有类别具有某种价值的事件。我对 jpa 很陌生。很难为此编写查询。如果你能帮助我,那将是有帮助的。
编辑:我将 categoryId 存储在事件表中。我希望能够按类别名称搜索。类别名称仅保存在类别表中。所以我想我需要加入类别表。
【问题讨论】:
-
是的,以某种方式找到类别(EntityManager.find(Object Id)?)并获取类别。 ?
-
正如我在之前的评论中提到的,我将 categoryId 存储在事件表中。我希望能够按类别名称搜索。类别名称仅保存在类别表中。所以我想我需要将事件表与类别表连接起来。
-
这是您第一次提到要按类别搜索名称。
-
很抱歉。我已将其添加到以下回复之一的评论中。我已经相应地编辑了我的主要帖子。
标签: java sql hibernate jpa jpql