JPQL 内连接

Posted

技术标签:

【中文标题】JPQL 内连接【英文标题】:JPQL Inner join 【发布时间】:2016-01-22 11:09:32 【问题描述】:

如何连接两个表并在 JPA 中显示?

表格:

用户

+----+----------+----------+-----------+
| id | username | password | userlevel |
+----+----------+----------+-----------+
|  1 | you      | you      |         1 |
|  2 | me       | me       |         2 |
|  3 | we       | we       |         3 |
|  4 | us       | us       |         1 |
+----+----------+----------+-----------+

用户级别

+------+--------+
| id   | level  |
+------+--------+
|    1 | Admin  |
|    2 | Leader |
|    3 | Member |
+------+--------+

类:

用户.java

package com.taskmanagement.entities;

import javax.persistence.Entity;

import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Table(name = "USER")
public class User 

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    private String username;
    private String password;
    private int userlevel;

    public User() 

    

    public User(int id, String username, String password, int userlevel) 
        this.username = username;
        this.password = password;
        this.userlevel = userlevel;
    

    public int getId() 
        return id;
     

    public String getUserName() 
        return username;
    

    public void setUserName(String username) 
        this.username = username;
    

    public String getPassword() 
        return password;
    

    public void setPassword(String password) 
        this.password = password;
    

    public int getUserLevel() 
        return userlevel;
    

    public void setUserLevel(int userlevel) 
        this.userlevel = userlevel;
    

    @Override
    public String toString() 
        return "User" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", userlevel='" + userlevel + '\'';
    

用户服务.java

package com.taskmanagement.service;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import com.taskmanagement.entities.User;
import java.util.List;

public class UserService  

    private EntityManager em;

    public UserService(EntityManager em) 
        this.em = em;
    
    public User addUser(int id, String username, String password, UserLevel userlevel) 
        User user = new User(id, username, password, userlevel);
        em.persist(user);
        em.flush(); 
        return user;
    

    public List<User> findAllUser() 
        TypedQuery<User> query = em.createQuery("SELECT u.username,u.password,ul.level FROM User u JOIN u.user_level ul", User.class);
        return query.getResultList();
    

Client.java

package com.taskmanagement.client;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import com.taskmanagement.entities.User;
import com.taskmanagement.service.UserService;
import java.util.List;

public class Client 

    public static void main(String[] args) 
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("TaskManagement");
        EntityManager em = emf.createEntityManager();

        UserService service = new UserService(em);
        User user = new User();

        EntityTransaction transaction = em.getTransaction();

        transaction.begin();
        User userservice = service.addUser(user.getId(),"Username","Password","xxx"); 
        //System.out.println("Latest id: "+userservice.getId());
        transaction.commit();
        System.out.println(String.format("Persisted: %s\n", userservice));

        System.out.println("--- Find all Users---");
        List<User> userservices = service.findAllUser();
        for (User foundUser : userservices) 
            System.out.println(String.format("Found: %s\n", foundUser));
        
    

我想显示加入 user_level 表的所有用户,以便显示用户以及他的用户级别。

更新

我发现了这个错误,但我不知道为什么它不起作用:

Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: user_level of: com.taskmanagement.entities.User [SELECT u.username,u.password,ul.level FROM com.taskmanagement.entities.User u JOIN u.user_level ul]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:350)
at com.taskmanagement.service.UserService.findAllUser(UserService.java:48)
at com.taskmanagement.client.Client.main(Client.java:35)
Caused by: org.hibernate.QueryException: could not resolve property: user_level of: com.taskmanagement.entities.User [SELECT u.username,u.password,ul.level FROM com.taskmanagement.entities.User u JOIN u.user_level ul]
at org.hibernate.QueryException.generateQueryException(QueryException.java:137)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:342)
... 2 more
Caused by: org.hibernate.QueryException: could not resolve property: user_level of: com.taskmanagement.entities.User
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1978)
at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:367)
at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:500)
at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:671)
at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:275)
at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:219)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:126)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:393)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3903)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3689)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
... 10 more

【问题讨论】:

您希望用户级别作为单独的实体存在,还是像现在一样成为User 类中的一个字段? @TimBiegeleisen 我的查询有什么问题吗?谢谢 @TimBiegeleisen 不,我不需要为用户级别设置单独的实体,因为它的值是静态的。我想要的只是获取用户级别(用文字表示)而不是那个 userlevel_id。 您有一个设计 问题,因为从您向我们展示的内容来看,User_level 是一个单独的表,但您没有对应的实体类。您应该添加一个实体类,或者只是将级别字段移动到User 表/类中。 【参考方案1】:

我相信好的设计会要求您确实应该为User_level 表创建一个单独的实体类:

@Entity
@Table(name = "USER_LEVEL")
public class UserLevel 

    @OneToMany (mappedBy="userLevel")
    private Set<User> users;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    private String level;

    public UserLevel() 

    

    public UserLevel(int id, String level) 
        this.id = id;
        this.level = level;
    

    public Set<User> getUsers() 
        return users;
    

    public int getId() 
        return id;
     

    public String getLevel() 
        return level;
    

    public void setUsers(Set<User> users) 
        this.users = users;
    

    public void setId(int id) 
        this.id = id;
    

    public void setLevel(String level) 
        this.level = level;
    

接下来应该更新您的User 实体类以引用上面为用户级别创建的实体,而不是原始字段。

@Entity
@Table(name = "USER")
public class User 

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    private String username;
    private String password;

    @ManyToOne
    @JoinColumn(name="id")        // 'id' is the foreign key in the UserLevel table
    private UserLevel userLevel;  // reference the entity class

    public User() 

    

    // getters and setters omitted for brevity

    public UserLevel getUserLevel() 
        return userLevel;
     

    public void setUserLevel(UserLevel userLevel) 
        this.userLevel = userLevel;
    

一旦你做了这些改变,你就可以很容易地做你想要得到结果的join查询:

String theQuery = "SELECT u, ul FROM User u JOIN u.userLevel ul";

TypedQuery<Object[]> query = entityManager.createQuery(theQuery, Object[].class);
List<Object[]> list = query.getResultList();

for (Object[] o : list) 
    User u = o[0];
    UserLevel ul = o[1];

    System.out.println("User " + u.getUserName() + " has level " + ul.getLevel());

【讨论】:

如果您遇到错误,我会与您一起修复它们。如果不运行代码,我无法确定我是否正确,但这是正确的想法。 我在添加新用户时遇到问题。我将更新我的 UserService.java 我没有在那里包含 addUser 方法。

以上是关于JPQL 内连接的主要内容,如果未能解决你的问题,请参考以下文章

直接在 JPA / JPQL 中将行插入连接表?

JPQL - 连接 - MultipleTables 和空 ResultList

JPQL查询多对多连接表

JPQL:多重连接。如何做我的命名查询?

JPQL在多对多关系上左外连接

JPQL 到 SQL 使用 Hibernate 更新查询交叉连接问题