HQL Hibernate INNER JOIN

后端 未结 3 1129
情话喂你
情话喂你 2020-11-30 08:23

How can I write this SQL query in Hibernate? I would like to use Hibernate to create queries, not create the database.

SELECT * FROM Employee e INNER JOIN Te         


        
3条回答
  •  猫巷女王i
    2020-11-30 09:15

    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.

    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).

    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.

提交回复
热议问题