Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA2:加入(自加入)问题_Java_Mysql_Multithreading_Eclipselink_Jpa 2.0 - Fatal编程技术网

Java JPA2:加入(自加入)问题

Java JPA2:加入(自加入)问题,java,mysql,multithreading,eclipselink,jpa-2.0,Java,Mysql,Multithreading,Eclipselink,Jpa 2.0,我正在尝试使用Join-self-Join从表中获取记录。我正在使用EclipseLink JPA2和MySQL。这是我使用的本机查询 select * from mytable as detail join (select max(timestamp) as maxtimestamp from mytable where id=" + theUser.getId() + " group by hnumber order by maxtimestamp limit " + <off

我正在尝试使用Join-self-Join从表中获取记录。我正在使用EclipseLink JPA2和MySQL。这是我使用的本机查询

    select * from mytable as detail join (select max(timestamp) as maxtimestamp from mytable where id=" + theUser.getId() + " group by hnumber order by maxtimestamp limit " + <offset> + "," + <iTotalRecords> + ") as topconv on detail.timestamp=topconv.maxtimestamp order by detail.timestamp
下面是获取记录的方法

    public List<MyTable> fetchRecords(User theUser, int iTotalRecords, long offset) throws Exception {
        if(null == theUser) {
            throw new Exception("Invalid input.");
        }

        EntityManager entityManager = getEntityManager();
        if(false == entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().begin();
        }

        try {
            Query query = entityManager.createQuery("select detail from MyTable as detail join (select topconv, max(topconv.timestamp) as maxtimestamp from MyTable where topconv.User= :User group by topconv.hnumber order by topconv.maxtimestamp) as topconv on detail.timestamp=topconv.maxtimestamp order by detail.timestamp");
            query.setParameter("User", theUser.getUser());
            @SuppressWarnings("unchecked")
            List<SMS> resultList = query.getResultList();
            return resultList;
        } catch(Throwable th) {
            throw new Exception(th.getMessage());
        } finally {
            closeEntityManager();
        }
    }
我的应用程序支持多线程。我发现以下错误

    java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
    Exception Description: Syntax error parsing [select detail from MyTable as detail join (select topconv, max(topconv.timestamp) as maxtimestamp from MyTable where topconv.User= :User group by topconv.hnumber order by topconv.maxtimestamp) as topconv on detail.timestamp=topconv.maxtimestamp order by detail.timestamp]. 
    The join association path is not a valid expression.
    An identification variable must be defined for a JOIN expression.
    The query contains a malformed ending.

在JPA2查询中似乎不支持JOIN,请建议其他替代方法。非常感谢您的帮助。

看起来您正在将SQL连接语法与JPA连接语法混合使用。JPA支持加入,但您尝试的不是通常的自加入。您右侧的表不是MyTable,而是包含聚合数据的select。您可能需要编写SQL连接查询,然后使用实体管理器的createNativeQuery方法。有关JPA连接的更多信息,请参考@Shailendra。您能给我介绍一个使用CriteriaBuilder实现自连接的示例吗?
    private EntityManagerFactory _entityManagerFactory = Persistence.createEntityManagerFactory("JPATest");
    protected EntityManager getEntityManager() {
        _entityManager = _entityManagerFactory.createEntityManager();
        return _entityManager;
    }

    protected void closeEntityManager() {
        try {
            if(null != _entityManager) {
                _entityManager.close();
            }
        } catch(Throwable th){}
    }
    java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
    Exception Description: Syntax error parsing [select detail from MyTable as detail join (select topconv, max(topconv.timestamp) as maxtimestamp from MyTable where topconv.User= :User group by topconv.hnumber order by topconv.maxtimestamp) as topconv on detail.timestamp=topconv.maxtimestamp order by detail.timestamp]. 
    The join association path is not a valid expression.
    An identification variable must be defined for a JOIN expression.
    The query contains a malformed ending.