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

时间:2014-07-17 05:08:21

标签: java mysql multithreading eclipselink jpa-2.0

我正在尝试使用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

我有以下pojo类

    @Entity
    @Table(name="mytable")
    public class MyTable {

        @Id
        @GeneratedValue(strategy=GenerationType.SEQUENCE)
        @Column(name="mytableid")
        private long lMyTableId;

        @ManyToOne
        @JoinColumn(name="id", referencedColumnName="id")
        private User user; //this object has id, name and so on fields.

        @Column(name="timestamp", length=15, nullable=false)
        private String strTimestamp;

        @Column(name="hnumber", nullable=true)
        private String hNumber;

        //Getters and Setters.

    }

以下是获取记录的方法

    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();
        }
    }

获取并关闭EntityManager

    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.

在JPA2查询中看起来不支持JOIN,请建议其他任何替代方案。任何帮助表示赞赏。

0 个答案:

没有答案