两个实体类:
@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;
}
第二:
@Entity
public class FilesShare {
@Id
@GeneratedValue
private Integer id;
@Column(name="fileId")
private Integer fileId;
private int userId;
private int owner;
@ManyToOne
@JoinColumn(name="fileId", updatable=false,insertable=false)
private FilesInfo filesInfo;
public FilesInfo getFilesInfo() {
return filesInfo;
}
public void setFilesInfo(FilesInfo filesInfo) {
this.filesInfo = filesInfo;
}
daoimpl 方法:
public List<FilesInfo> reqSharedFiles(FilesShare fs) {
session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<FilesInfo> filesInfo = null;
filesInfo=(List<FilesInfo>)session.createQuery("from FilesInfo a, FilesShare b where a.id=b.fileId and b.userId=5 and b.owner=1").list();
现在我不明白hibernate何时映射并返回对象,因为我在操作中遇到了无法强制转换的错误:
Ljava.lang.Object;无法转换为 app.domain.FilesInfo
那个行动
private List<FilesInfo> furls= new ArrayList<FilesInfo>();
furls=upload.reqSharedFiles(fs);
for(FilesInfo s: furls) // ERROR IS SHOWING HERE
{
System.out.println(s.getUrl());
}