pojo类(Photo)
把getComment 返回的set改成了 list
package com.demo.hibernate.beans;import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Photo implements Serializable {    /** identifier field */
    private Integer PId;    /** nullable persistent field */
    private String description;    /** nullable persistent field */
    private Boolean share;    /** persistent field */
    private byte[] photo;    /** persistent field */
    private com.demo.hibernate.beans.CommonAlbum commonAlbum;    /** persistent field */
    private com.demo.hibernate.beans.Album album;    /** persistent field */
    private com.demo.hibernate.beans.User user;    /** persistent field */
    private Set<Comment> comments;
    private List<Comment> comments2 = new ArrayList<Comment>();
    /** full constructor */
    public Photo(Integer PId, String description, Boolean share, byte[] photo, com.demo.hibernate.beans.CommonAlbum commonAlbum, com.demo.hibernate.beans.Album album, com.demo.hibernate.beans.User user, Set comments) {
        this.PId = PId;
        this.description = description;
        this.share = share;
        this.photo = photo;
        this.commonAlbum = commonAlbum;
        this.album = album;
        this.user = user;
        this.comments = (Set<Comment>) comments;
    }    /** default constructor */
    public Photo() {
    }    /** minimal constructor */
    public Photo(Integer PId, byte[] photo, com.demo.hibernate.beans.CommonAlbum commonAlbum, com.demo.hibernate.beans.Album album, com.demo.hibernate.beans.User user, List<Comment> comments) {
        this.PId = PId;
        this.photo = photo;
        this.commonAlbum = commonAlbum;
        this.album = album;
        this.user = user;
        this.comments = (Set<Comment>) comments;
    }    public Integer getPId() {
        return this.PId;
    }    public void setPId(Integer PId) {
        this.PId = PId;
    }    public String getDescription() {
        return this.description;
    }    public void setDescription(String description) {
        this.description = description;
    }    public Boolean getShare() {
        return this.share;
    }    public void setShare(Boolean share) {
        this.share = share;
    }    public byte[] getPhoto() {
        return this.photo;
    }    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }    public com.demo.hibernate.beans.CommonAlbum getCommonAlbum() {
        return this.commonAlbum;
    }    public void setCommonAlbum(com.demo.hibernate.beans.CommonAlbum commonAlbum) {
        this.commonAlbum = commonAlbum;
    }    public com.demo.hibernate.beans.Album getAlbum() {
        return this.album;
    }    public void setAlbum(com.demo.hibernate.beans.Album album) {
        this.album = album;
    }    public com.demo.hibernate.beans.User getUser() {
        return this.user;
    }    public void setUser(com.demo.hibernate.beans.User user) {
        this.user = user;
    }    public List<Comment> getComments() {
     System.out.print("csize"+this.comments.size());
     Iterator<Comment> iter = comments.iterator();
     System.out.print("csize"+iter.hasNext());
     Comment com = new Comment();
     while(iter.hasNext()){
     System.out.print("!!!!!!!!!!!!!");
     com = iter.next();
    
     //System.out.print("???????"+com.getContent());
     this.comments2.add(com);
     //System.out.print("???????"+comments2.getClass());
     }
        return this.comments2;
    }    public void setComments(Set comments) {
    
        this.comments = comments;
    }    public String toString() {
        return new ToStringBuilder(this)
            .append("PId", getPId())
            .toString();
    }}
pojo类(Comment)这个没改:package com.demo.hibernate.beans;import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Comment implements Serializable {    /** identifier field */
    private Integer commentId;    /** nullable persistent field */
    private String content;    /** nullable persistent field */
    private Boolean share;    /** persistent field */
    private com.demo.hibernate.beans.Photo photo;    /** persistent field */
    private com.demo.hibernate.beans.User user;    /** full constructor */
    public Comment(Integer commentId, String content, Boolean share, com.demo.hibernate.beans.Photo photo, com.demo.hibernate.beans.User user) {
        this.commentId = commentId;
        this.content = content;
        this.share = share;
        this.photo = photo;
        this.user = user;
    }    /** default constructor */
    public Comment() {
    }    /** minimal constructor */
    public Comment(Integer commentId, com.demo.hibernate.beans.Photo photo, com.demo.hibernate.beans.User user) {
        this.commentId = commentId;
        this.photo = photo;
        this.user = user;
    }    public Integer getCommentId() {
        return this.commentId;
    }    public void setCommentId(Integer commentId) {
        this.commentId = commentId;
    }    public String getContent() {
        return this.content;
    }    public void setContent(String content) {
        this.content = content;
    }    public Boolean getShare() {
        return this.share;
    }    public void setShare(Boolean share) {
        this.share = share;
    }    public com.demo.hibernate.beans.Photo getPhoto() {
        return this.photo;
    }    public void setPhoto(com.demo.hibernate.beans.Photo photo) {
        this.photo = photo;
    }    public com.demo.hibernate.beans.User getUser() {
        return this.user;
    }    public void setUser(com.demo.hibernate.beans.User user) {
        this.user = user;
    }    public String toString() {
        return new ToStringBuilder(this)
            .append("commentId", getCommentId())
            .toString();
    }}
然后调用 PhotoDAO 中的getCommentpublic List<Comment> getComment(int  pID) {
Session session = null; Transaction tx = null;
Photo photo = new Photo();

List<Comment> Comments = new ArrayList<Comment>();

try{
session = HibernateSessionFactory.currentSession();
//System.out.println("得到session");
tx = session.beginTransaction();
//System.out.println("得到事物");
Query query = session.createQuery("from Photo where p_id=?");
query.setInteger(0, pID);
photo = (Photo) query.uniqueResult();

Comments = photo.getComments();
System.out.println("username2"+Comments.getClass());
//

Iterator<Comment> iter = Comments.iterator();
while(iter.hasNext()){
Comment com = new Comment();
com = iter.next();
System.out.println("username3"+com.getContent());
}

query = null;
tx.commit();
}catch (HibernateException e){

if(tx!=null){
System.out.println("what!!");
tx.rollback();
}
throw e;
}finally{

 //String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
HibernateSessionFactory.closeSession();

}
return Comments;
}一下从一个main里面测试:public static void main(String[] args){




List<Comment> coms = new ArrayList<Comment>();
PhotoDAO dao = new PhotoDAO();
                 coms = dao.getComment(1);
System.out.println("coms.size"+coms.size());
//System.out.println("com"+coms.get(0).getContent());
}
debug断点测试 一直都正常每当 到了 getComment()中的return comments断点再往下就出现
Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Set
at org.hibernate.type.SetType.wrap(SetType.java:61)
at org.hibernate.event.def.WrapVisitor.processArrayOrNewCollection(WrapVisitor.java:106)
at org.hibernate.event.def.WrapVisitor.processCollection(WrapVisitor.java:73)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:122)
at org.hibernate.event.def.WrapVisitor.processValue(WrapVisitor.java:120)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:77)
at org.hibernate.event.def.DefaultFlushEntityEventListener.wrapCollections(DefaultFlushEntityEventListener.java:218)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:152)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.demo.hibernate.dao.PhotoDAO.getComment(PhotoDAO.java:191)
at com.demo.hibernate.dao.PhotoDAO.main(PhotoDAO.java:249)
他提示ArrayList不能转化成set类型 我就郁闷了 到底哪里需要ArrayList转化为set呢 而且方法体里面一直运行的没错
比如设置的参数都读出来了 如下
csize1csizetrue!!!!!!!!!!!!!username2class java.util.ArrayList
username3good!
往下就出现错误了 可能hibernate内部机制的问题?我还不太了解hibernate原理 求骨灰级玩家帮帮我!!!

解决方案 »

  1.   

    把Photo中的 comments 类型改成List<Comment>试一试
    public class Photo implements Serializable {//.......
    /** persistent field */
      private Set<Comment> comments;
      

  2.   

    额 谢谢楼上 不过 这个用set是必须的 因为hibernate从数据表中取出来的数据是放在org.hibernate.collection.persistentSet里面 而这个类 继承了Set 所以 这里setter里面必须是set类型的 而且我试过了 不然连前期代码都通不过
      

  3.   

    检查一下你的hibernate映射文件里面的属性类型和你类里面配置的属性类型是不是相同!应该是你映射文件中配置错了,和类中配置的类型不相同才出这个错的
      

  4.   

    谢谢楼上 应该是这个错误的 但我想知道期中的原理 如果这个方法不行我可能就要用最笨的方法了 。。因为C/S不同虚拟之间 不能直接传递set对象(这个我查了一下 其实是传递hibernate3.jar里面的org.hibernate.collection.persisitentSet类,该类继承了Set)因为就算双方都布置了 hibernate3.jar 也出现org.hibernate.collection.persisitentSet 版本序列号不一致。。因为这个我快疯了 但现在要赶时间做完 没办法深究了 。。哎 
      

  5.   

    hibernate的最要功能是简化Dao类,让程序员的主要注意力集中在业务逻辑上,而不是集中在数据查询上,也就是说hibernate帮助我们去做数据查询,但hibernate不是万能的也需要我们指定要查询什么,例如要查询一个用户对象信息,对象有String 类型的名称name,有int类型的年龄age,hibernate在查询的时候会根据你指定的这个对象向数据库中对应的表中查询信息(这就是为什么要配置hibernate.xml文件,这个文件的作用就是将程序中我们指定的实体类对象和数据库中的表对应起来),你应该知道String类型是无法转换成int类型的(除非String类型本身的值就是int类型的数字),hibernate帮我们获取对象信息的时候也不会自动为我们转换类型,即使可以自动转换如果类型不匹配当然也会报错,所以在使用hibernate的时候,数据库中字段的类型要和我们实体类中对应属性的类型相同,而配置文件中的类型要和实体类中的类型相匹配。
    我这样说不知道楼主能不能明白,这也是我个人的一些理解,如有不当之处欢迎各位朋友批评指正!
      

  6.   

    着什么急啊。我对hibernate也不怎么熟悉,不过既然配置的是Set类型,那你的getComments就返回Set,调用的时候转换为List即可。别着急,慢慢来
      

  7.   

    嗯 world明白您是什么意思,也知道映射文件配置的是什么类就应该实体类中返回什么类。但就是不明白我这样更改哪一个部分出错 我只是觉得理论上可以。。算了 我已经用笨方法解决了。。可能不同虚拟机之间的 set序列号不一致才是我最大的敌人吧 否则直接双方都引入hibernate3.jar 就简单多了 这样的话我的项目半个月前就应该结束了哈哈。。
    无奈 等着以后深入学习吧
      

  8.   

    呵呵 已经这么做了 虽然方法笨点 但毕竟是实现了。另外不知道您注意没有。。Hibernate自动生成的POJO实体类中没有外键的ID 而是直接把包含这个类的对象传输进去。。其实这个就是获得外键而已 所以我转换成List时候 感觉开始不知道怎么给这个包含它的对象赋值、、其实就直接给这个对象setID进去其他set null就行也就是只传输一个包含外键ID的父对象 不知道我说的对不对 反正我这么理解也这么做的 都实现了