Hibernate一对多关系表中如何在多的一方通过外键查询数据
classBook{privateStringbookId;privateBooktypebooktype;privateStringbookName;}classBoo...
class Book{
private String bookId;
private Booktype booktype;
private String bookName;
}
class Booktype{
private Integer typeId;
private String typeName;
private Set bookinfors = new HashSet(0);
}
book.xml 中的配置
<many-to-one name="booktype" class="com.dto.Booktype" fetch="select">
<column name="type" not-null="true" />
</many-to-one>
booktype.xml 中的配置
<set name="bookinfors" inverse="true">
<key>
<column name="type" not-null="true" />
</key>
<one-to-many class="com.dto.Book" />
</set>
在Hibernate中如何通过booktype属性查询book表。 展开
private String bookId;
private Booktype booktype;
private String bookName;
}
class Booktype{
private Integer typeId;
private String typeName;
private Set bookinfors = new HashSet(0);
}
book.xml 中的配置
<many-to-one name="booktype" class="com.dto.Booktype" fetch="select">
<column name="type" not-null="true" />
</many-to-one>
booktype.xml 中的配置
<set name="bookinfors" inverse="true">
<key>
<column name="type" not-null="true" />
</key>
<one-to-many class="com.dto.Book" />
</set>
在Hibernate中如何通过booktype属性查询book表。 展开
1个回答
展开全部
你是想通过查询书的种类,把属于该类的所有书查询出来吧?可以这样做
List<Booktype> list=new ArrayList<Booktype>();
list=session.createCriteria(Booktype.class).list();
for(Booktype tq:list){
System.out.println(tq.getQx());
for (Book B:tq.getBookinfors ) {
System.out.println(" "+tj.getBookName);
}
}
session.beginTransaction().commit();
HibernateSessionFactory.closeSession();
}
List<Booktype> list=new ArrayList<Booktype>();
list=session.createCriteria(Booktype.class).list();
for(Booktype tq:list){
System.out.println(tq.getQx());
for (Book B:tq.getBookinfors ) {
System.out.println(" "+tj.getBookName);
}
}
session.beginTransaction().commit();
HibernateSessionFactory.closeSession();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询