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原理 求骨灰级玩家帮帮我!!!
把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原理 求骨灰级玩家帮帮我!!!
public class Photo implements Serializable {//.......
/** persistent field */
private Set<Comment> comments;
我这样说不知道楼主能不能明白,这也是我个人的一些理解,如有不当之处欢迎各位朋友批评指正!
无奈 等着以后深入学习吧