且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HQL Hibernate 内部连接

更新时间:2022-11-05 21:37:24

Joins 只能在实体之间存在关联时使用.您的 Employee 实体不应具有映射到列的名为 id_team、类型为 int 的字段.它应该与 Team 实体有一个 ManyToOne 关联,映射为一个 JoinColumn:

Joins can only be used when there is an association between entities. Your Employee entity should not have a field named id_team, of type int, mapped to a column. It should have a ManyToOne association with the Team entity, mapped as a JoinColumn:

@ManyToOne
@JoinColumn(name="ID_TEAM")
private Team team;

然后,以下查询将完美运行:

Then, the following query will work flawlessly:

select e from Employee e inner join e.team

这将加载所有员工,除了那些与任何团队都没有关联的员工.

Which will load all the employees, except those that aren't associated to any team.

同样适用于所有其他字段,这些字段是映射为实体的其他表的外键,当然(id_bossid_profession).

The same goes for all the other fields which are a foreign key to some other table mapped as an entity, of course (id_boss, id_profession).

是时候阅读 Hibernate 文档了,因为您错过了它是什么以及它是如何工作的一个极其重要的部分.

It's time for you to read the Hibernate documentation, because you missed an extremely important part of what it is and how it works.