查询的代码
Session session=bo.HibernateSessionFactory.getSession();
Session session=bo.HibernateSessionFactory.getSession();
//查询每个同学所拥有的所有书x;
Query query=session.createQuery("from Student");
List list=query.list();
for(int i=0;i<list.size();i++)
{
Student student=(Student)list.get(i);
System.out.println(student.getId());//到这里可以
Set listbooks=student.getBooks();//出错
System.out.println(listbooks);
/*Iterator it=listbooks.iterator();
while(it.hasNext())
{
Book book=(Book)it.next();
System.out.println("姓名 "+student.getName()+" 书名 "+book.getName());
}*/
}
Book.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="po.Book" table="BOOK" schema="DB2ADMIN">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<many-to-one name="student" class="po.Student" fetch="select">
<column name="SID" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="NAME" length="30" />
</property>
</class>
</hibernate-mapping>
Stuent.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="po.Student" table="STUDENT" schema="DB2ADMIN">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="10" />
</property>
<property name="age" type="java.lang.Short">
<column name="AGE" />
</property>
<set name="books" inverse="true">
<key>
<column name="SID" />
</key>
<one-to-many class="po.Book" />
</set>
</class>
</hibernate-mapping>
用的是DB2数据库;错误提示如下
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
1
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not initialize a collection: [po.Student.books#1]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2005)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:63)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1717)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)