- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Spring MVC 为我的 Web 应用程序构建一个消息传递系统,并使用 Spring Data JPA 和 Hibernate 作为我的 JPA 提供程序。
我有五个实体:Thread
、ThreadParticipant
、Participant
、Account
和 Company
.每个消息线程至少有两个参与者,其中一个与用户(Account
实体)关联,另一个与Company
关联。该约束由应用程序强制执行。数据库这样设计是为了支持 future 的功能。数据库中给定线程的两个参与者的示例如下所示:
id account_id company_id
1 44 NULL
2 NULL 123
id=1 的行是用户,id=2 的行是公司。我想要做的是编写一个 HQL 查询,提取给定帐户的所有 Thread 对象,其中包含用户/帐户参与者以及公司参与者。我尝试为我的连接使用不同的别名,如下所示:
select distinct t
from Thread t
inner join fetch t.threadParticipants user_tp
inner join fetch t.threadParticipants company_tp
inner join fetch user_tp.participant user_p
inner join fetch user_p.account a
inner join fetch company_tp.participant receiver_p
inner join fetch receiver_p.company
where a.id = :accountId
由于 t.threadParticipants
的两次提取,我收到异常无法同时提取多个包
。如果我在这里只执行一次联接,生成的 SQL 会忽略我的附加联接,并且仅联接参与者
一次,这要求参与者同时拥有关联的帐户和公司。使用原始 SQL,我可以这样做,而且效果很好:
select *
from thread t
inner join thread_participant user_tp on (user_tp.thread_id = t.id)
inner join thread_participant company_tp on (company_tp.thread_id = t.id)
inner join participant user_p on (user_p.id = user_tp.participant_id)
inner join account a on (a.id = user_p.account_id)
inner join participant company_p on (company_p.id = company_tp.participant_id)
inner join company c on (c.id = company_p.company_id)
where a.id = 123;
如果我不对同一个表使用不同的别名(请参阅下面的查询),查询运行正常,但我只返回一个线程参与者 - 与帐户关联的线程参与者。
select distinct t
from Thread t
inner join fetch t.threadParticipants tp
inner join fetch tp.participant p
inner join fetch p.account a
left join fetch p.company
where a.id = :accountId
有什么方法可以让我用 HQL 做我想做的事情,还是必须使用 native SQL?
我的映射如下:
线程实体
@Entity
@Table(name = "thread")
public class Thread {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "thread", cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private Collection<ThreadParticipant> threadParticipants = new HashSet<>();
// Getters and setters
}
参与者实体
@Entity
@Table(name = "participant")
public class Participant {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int id;
@ManyToOne(fetch = FetchType.LAZY, optional = true, targetEntity = Account.class, cascade = { CascadeType.PERSIST })
@JoinColumn(name = "account_id")
private Account account;
@ManyToOne(fetch = FetchType.LAZY, optional = true, targetEntity = Company.class)
@JoinColumn(name = "company_id")
private Company company;
// Getters and setters
}
ThreadParticipant 实体
@Entity
@Table(name = "thread_participant")
@IdClass(ThreadParticipantPK.class)
public class ThreadParticipant implements Serializable {
@Id
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Participant.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "participant_id")
private Participant participant;
@Id
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Thread.class)
@JoinColumn(name = "thread_id")
private Thread thread;
@Column(name = "last_viewed", nullable = true)
private Date lastViewed;
// Getters and setters
}
ThreadParticipantPK
public class ThreadParticipantPK implements Serializable {
private Thread thread;
private Participant participant;
public ThreadParticipantPK() { }
public ThreadParticipantPK(Thread thread, Participant participant) {
this.thread = thread;
this.participant = participant;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ThreadParticipantPK)) return false;
ThreadParticipantPK that = (ThreadParticipantPK) o;
if (!participant.equals(that.participant)) return false;
if (!thread.equals(that.thread)) return false;
return true;
}
@Override
public int hashCode() {
int result = thread.hashCode();
result = 31 * result + participant.hashCode();
return result;
}
// Getters and setters
}
提前谢谢您!
最佳答案
尝试将 threadParticipants
集合的类型更改为 Set
而不是 Collection
:
private Set<ThreadParticipant> threadParticipants;
关于java - 在 Hibernate 中使用不同别名多次连接到同一关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29122326/
最近几天,我们考虑使用 Solr 作为我们的首选搜索引擎。 我们需要的大多数功能都是开箱即用的,或者可以轻松配置。 然而,我们绝对需要的一项功能似乎在 Solr 中被很好地隐藏(或缺失)了。 我会试着
我是 Sequelize 的新手,并且一直在探索关联。我正在使用 mysql 5.6 并 Sequelize ^4.42.0。我正在尝试创建两个简单的表:PRJS 和 TASKS 并将一些数据插入这些
关联、聚合和组合之间有什么区别?请从实现的角度解释一下。 最佳答案 对于两个对象,Foo 和 Bar 可以定义关系 关联 - 我与一个对象有关系。 Foo 使用 Bar public class Fo
这两种 hasOne 语法有什么区别? class Project { ....... ............ static hasOne = Employee // static h
对于当前的项目,我想使用遗传算法 - 目前我查看了 jenetics 库。 如何强制某些基因相互依赖?我想将 CSS 映射到基因上,例如我有基因指示是否显示图像,以及如果它也是各自的高度和宽度。因此,
关联、聚合和组合之间有什么区别?请从实现的角度解释一下。 最佳答案 对于两个对象,Foo 和 Bar 可以定义关系 关联 - 我与一个对象有关系。 Foo 使用 Bar public class Fo
假设我有一个名为“学生”的表格,其中包含姓名、手机、电子邮件、首选类(class)、首选学校、性别、年龄、地址、资格、职称、家庭电话、工作电话等列 我想从 Students 表中选择数据并插入到 2
问题标题有点困惑。我有一级员工和一级项目。一名或多名员工正在从事一个或多个项目。在这个关联中,我只有一个从具有*多重性的员工类到具有*多重性的项目类的链接。现在有另一种实现。每个项目只有一名经理,属于
到目前为止,我有一个程序采用一组随机点、站点,并围绕这些点形成适当的 Voronoi 图,表示为角和边的图形。它还为我提供了 Delaunay 三角剖分作为另一个以所有站点为节点的图形(尽管我不知道这
实现IComMethodEvents时你得到三个事件。 OnMethodCall OnMethodException OnMethodReturn 我的目标是记录 COM+ 组件中每个方法的调用时间。
我正在处理这个问题。我正在创造数学问题,每一个都有回应。例如。 如果我的问题是关于“5x + 15 = 2 的结果?”,我将只等待一个答案(整数)。 如果我的问题是关于“给我这个形状的面积和许可”,我
我正在寻找一种数据结构来保存唯一元素的无序集合,它将支持以下操作 在集合中任意位置插入/删除元素 查询元素是否存在 访问一个随机元素 天真地,1 和 2 建议使用关联容器,例如unordered_se
是否可以在 LINQ 中使用类似 ContactAddress.Contact 的内容,而无需在 SQL Server 中在这两者之间创建外键关系(通过 Contact.Id ContactAddr
我一直在谷歌搜索,但不明白调用 javax.persistence.criteria.Subquery 和 Criteria API 的方法相关的结果是什么。 http://www.objectdb.
我正在关注 Chris McCord 的“Programming Phoenix”一书,在第 6 章中,在 User 之间创建了一个关系。和一个 Video . 尝试使用 mix phoenix.se
我在 XAML 中有一个 ItemsControl,我在其中为每个组显示一个扩展器,以便我可以展开/折叠该组。我想保持 IsExpanded 的状态属性(以及可能与组标题显示相关的其他设置)。通常你只
Oracle 11 中是否有内置方法来检查 varchar2 字段中值的相关性?例如,给定一个简单的表,如下所示: MEAL_NUM INGREDIENT --------------------
是否可以在没有 JPA 在数据库中创建外键的情况下设置多对一关联? 这些表归另一个系统所有,并以异步方式填充。因此我们不能在数据库中使用 FK。仍然,几乎总是,最终是一种关系。 @ManyToOne(
我一直在使用NHibernate,使用Fluent NHibernate进行映射。我解决了很多问题,并开始认为自己在nhibernate中经验丰富。 但是,此错误非常奇怪。 这是我的模型: p
我正在开发一个 Typescript Sequelize 项目,其中我的 /models/index.ts 文件具有以下“导入此目录中的所有模型”功能: var basename = path.bas
我是一名优秀的程序员,十分优秀!