hibernate如何使用hql创建内连接

时间:2013-04-27 05:53:06

标签: mysql hibernate

我有两个由onetomany映射的实体类:

@Entity
public class FilesInfo {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private String url;

    @OneToMany(cascade= CascadeType.ALL)
    @JoinColumn(name="fileId")
    private Collection<FilesShare> filesShared = new ArrayList<FilesShare>();

    public Collection<FilesShare> getFilesShared() {
        return filesShared;
    }

    public void setFilesShared(Collection<FilesShare> filesShared) {
        this.filesShared = filesShared;
    }
 //getters & setters   
}

另一个

@Entity
public class FilesShare {
    private Integer id;
    private int userId;
    private int owner;
}

结果表是:

mysql> desc filesshare;
+--------+---------+------+-----+---------+----------------+
| Field  | Type    | Null | Key | Default | Extra          |
+--------+---------+------+-----+---------+----------------+
| id     | int(11) | NO   | PRI | NULL    | auto_increment |
| userId | int(11) | NO   |     | NULL    |                |
| owner  | int(11) | NO   |     | NULL    |                |
| fileId | int(11) | YES  | MUL | NULL    |                |
+--------+---------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

mysql> desc filesinfo;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | YES  |     | NULL    |                |
| url   | varchar(255) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

现在我正在尝试执行内部联接并获取类型为FilesInfo的列表:

    @Override
        public List<FilesInfo> reqSharedFiles(FilesShare fs) {
            session=HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            List<FilesInfo> filesInfo = null;
            filesInfo=session.createQuery("select a.id, a.name, a.url from FilesInfo as a 
            inner join FilesShare as b on "+
            " a.id=b.fileId where b.userId= :userId and b.owner= :owner")
           .setInteger("userId",fs.getUserId()).setInteger("owner",fs.getOwner()).list();
            session.getTransaction().commit();
            return filesInfo;
        }

我收到此错误

  
    

Struts检测到未处理的异常:消息:
    org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr / RecognitionException; Lantlr /集合/ IMPL /位集合;)V     文件:org / hibernate / hql / antlr / HqlBaseParser.java行号:1,802 app.dao.UploadDAOImpl.reqSharedFiles(UploadDAOImpl.java:79)     &LT; ------这一行指向filesInfo = session.createQuery(“select

  
请告诉我哪里出错了?

感谢

1 个答案:

答案 0 :(得分:2)

这是您正在使用的hibernate和ANTLR jar版本的问题。在版本2.7.6之前,ANTLR Parser类中没有恢复方法?如果您使用的是早期版本的ANTLR,例如2.7.2,那么您将遇到问题。

SEE HERE