初学Hibernate,对它的理解就是对数据的持久化操作,也看不一些源码,总之感觉把对数据库的操作简化了(个人初学的理解)
今天又看了看多表之间的查询,在做实验的时候还被老师训斥了一番,说我写的HQL语句跟SQL语句没有上面区别,我又仔细的看了看书,改了一下
下面是多表查询的所有代码
项目目录:
Hibernate我是通过Myeclipse自动导入的,本来我认为这些框架类的代码手写是最牛逼的,但我认为,既然能自动生成,何乐而不为呢?再说了,你写的代码能有人家开发的写的好么(关于Hibernate的自动导入博客里有介绍)
再来看看数据库的表,我用的是mysql
/* MySQL Data Transfer Source Host: localhost Source Database: book Target Host: localhost Target Database: book Date: 2014-10-22 10:31:17 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for bookinfo -- ---------------------------- CREATE TABLE `bookinfo` ( `id` int(4) NOT NULL AUTO_INCREMENT, `bookISBN` varchar(17) NOT NULL, `bookName` varchar(100) NOT NULL, `author` varchar(100) NOT NULL, `price` float(8,0) DEFAULT NULL, `typeId` int(4) DEFAULT NULL, `publish` varchar(100) NOT NULL, PRIMARY KEY (`id`), KEY `FK_bookinfo_bookType` (`typeId`), CONSTRAINT `FK_bookinfo_bookType` FOREIGN KEY (`typeId`) REFERENCES `booktype` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for booktype -- ---------------------------- CREATE TABLE `booktype` ( `id` int(4) NOT NULL AUTO_INCREMENT, `typeName` varchar(60) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records -- ---------------------------- INSERT INTO `bookinfo` VALUES ('1', '10101010', 'JavaEE', '赵燕', '40', '1', '清华大学'); INSERT INTO `bookinfo` VALUES ('2', '010101010', '软件测试', '王涛', '40', '1', '滁州学院'); INSERT INTO `bookinfo` VALUES ('3', '10100010', '高等数学', '大王涛', '20', '2', '滁州学院'); INSERT INTO `bookinfo` VALUES ('4', '12313', '离散数学', '佚名', '20', '2', '数学院'); INSERT INTO `booktype` VALUES ('1', '计算机类'); INSERT INTO `booktype` VALUES ('2', '数学类');注意里面有个关联,Hibernate使用多表查询的时候,数据库里的关联非常重要
关联好之后,通过MyEclipse李的逆向工程生成的数据表映射文件的内容就是这样
Booktype.java
public class Booktype implements java.io.Serializable { // Fields private Integer id; private String typeName; private Set bookinfos = new HashSet(0); // Constructors /** default constructor */ public Booktype() { } /** minimal constructor */ public Booktype(String typeName) { this.typeName = typeName; } /** full constructor */ public Booktype(String typeName, Set bookinfos) { this.typeName = typeName; this.bookinfos = bookinfos; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getTypeName() { return this.typeName; } public void setTypeName(String typeName) { this.typeName = typeName; } public Set getBookinfos() { return this.bookinfos; } public void setBookinfos(Set bookinfos) { this.bookinfos = bookinfos; }注意里面的Set
Booktype.hbm.xml
<hibernate-mapping> <class name="com.sx11.bean.Booktype" table="booktype" catalog="book"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="increment" /> </id> <property name="typeName" type="java.lang.String"> <column name="typeName" length="60" not-null="true" /> </property> <set name="bookinfos" cascade="all-delete-orphan" lazy="false" inverse="true"> <key> <column name="typeId" not-null="true" /> </key> <one-to-many class="com.sx11.bean.Bookinfo" /> </set> </class> </hibernate-mapping>里面的关系一对多:
one-to-many class="com.sx11.bean.Bookinfo" />
<many-to-one name="booktype" class="com.sx11.bean.Booktype" fetch="select"> <column name="typeId" /> </many-to-one>全部的代码就不贴了,大家自己可以试试,MyEclipse自动给你生成了,很强大
紧接着是dao
SelectBook.java
public class SelectBook { Configuration configuration = null; public void findAll(){ /*多表查询,增加持久化类*/ /*※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※*/ // configuration = HibernateSessionFactory.getConfiguration(); // configuration.addClass(Bookinfo.class); // configuration.addClass(Booktype.class); //实例化Session Session session = HibernateSessionFactory.getSession(); //定义事务处理对象 Transaction tx = session.beginTransaction(); //开始事务 tx.begin(); //查询所有的商品信息由于ProductInfoVo已经关联到ProductSortVo中,所以HQL语句仅查询ProductSortVo即可 Query query=session.createQuery("from Booktype as a order by a.id "); List<?> list=query.list(); System.out.println("图书名\t\t图书类别\t\t作者\t\t价格\t\t出版社"); for(int i=0;i<list.size();i++){ Booktype type = (Booktype) list.get(i); Set<Bookinfo> bookinfo = type.getBookinfos(); Iterator<Bookinfo> it = bookinfo.iterator(); while(it.hasNext()){ Bookinfo bi=(Bookinfo) it.next(); System.out.print(bi.getBookName()+"\t\t"); System.out.print(type.getTypeName()+"\t\t"); System.out.print(bi.getAuthor()+"\t\t"); System.out.print(bi.getPrice()+"\t\t"); System.out.println(bi.getPublish()); } } } }※下面注释的语句我是照书上写的,编译的时候有错误,找了老师问了问,老师说在Hibernate.cfg.xml里自动加载了这两项,就不需要写了,我打开视图界面,果然
到此,写个测试类就能实现多表之间的查询了
这一个例子中,需要注意的就是数据表之间的关联关系要建立好
还有就是Hql语句,直接查询Booktype就行了,当运行到typeid的时候,Hibernate会自动将于typeid关联的数据表找出来
最后就是遍历代码的编写,如上述代码
如果有大神发现错误,请指出,毕竟我只是一个菜鸟。。。。