我有一對多映射兩個實體類:冬眠如何創建內部聯接使用HQL
@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;
}
的resultand表是:
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/collections/impl/BitSet;)V File:org/hibernate/hql/antlr/HqlBaseParser.java行號:1,802 app.dao.UploadDAOImpl.reqSharedFiles(UploadDAOImpl.java: 79) < ------這一行指向filesInfo = session.createQuery(「選擇
請請告訴我,這是錯誤的?
感謝
一兩件事我想知道的是,映射在實體類必須進行連接操作?我們不能加入單個表使用hql? – Aadam 2013-04-27 06:24:43
你可以加入。沒問題 – samba 2013-04-27 06:37:57
嘿,我現在得到這個錯誤:\t 意外的標記:在第1行,第86列[從app.domain.FilesInfo中選擇a.id,a.name,a.url作爲內部加入FilesShare爲b上a.id = b.fileId其中b.userId =:userId和b.owner =:所有者] – Aadam 2013-04-27 06:41:14