Hibernate实验三.doc_第1页
Hibernate实验三.doc_第2页
Hibernate实验三.doc_第3页
Hibernate实验三.doc_第4页
Hibernate实验三.doc_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Hibernate实验三实验内容:1. Hibernate组件映射2. 使用二级缓存实验目的:1. 掌握组件映射的配置2. 区分值类型和实体类型3. 配置二级缓存一、 组件映射以客户和家庭住址为例1. 新建component Java项目,并添加Hibernate包2. 创建实体类POJO/创建Address类package component.pojo;public class Address private String province;private String city;private String street;private String zipcode;private Customer customer;/省略setter和getter/创建Customer类package component.pojo;public class Customer private int id;private String name;private Address homeAddress;private Address comAddress;/省略setter和getter3. 映射组件Customer.hbm.xml4. Hibernate配置文件Hibernate.cfg.xml org.hibernate.dialect.MySQLDialect jdbc:mysql:/localhost:3306/test root 1234 com.mysql.jdbc.Driver mysql true update 根据映射文件自动生成数据库表,数据库应先建好 5. 测试插入数据package component.test;import org.hibernate.Session;import org.hibernate.Transaction;import component.pojo.Address;import component.pojo.Customer;public class Test public static void main(String args) Address home=new Address();home.setCity(城市1);home.setProvince(省1);home.setZipcode(邮编1);home.setStreet(街道1);Address com=new Address();com.setCity(城市2);com.setProvince(省2);com.setZipcode(邮编2);com.setStreet(街道2);Customer tom=new Customer();tom.setName(Tom);tom.setId(1);tom.setHomeAddress(home);tom.setComAddress(com);home.setCustomer(tom);com.setCustomer(tom);Session session=cs.cwnu.sf.HibernateSessionFactory.getSession();Transaction tx=session.beginTransaction();session.save(tom);mit();session.close();区分值类型和实体类型:请测试,试图单独保存一个Address对象session.save(home);二、 使用二级缓存在上例的基础之上,验证使用二级缓存。1. 启用二级缓存EHCache在hibernate.cfg.xml配置文件中启动二级缓存 true org.hibernate.cache.EhCacheProvider注意:添加commons-logging-1.1.1.jar日志包2. 在类粒度上使用第二级缓存Customer.hbm.xml.3. 编写EHCache的配置文件ehcache.xml 4. 编写测试类请在测试之间插入两条以上记录public class Test1 public static void main(String args) Session s=HibernateSessionFactory.getSession();Customer c1=(Customer)s.get(Customer.class, 1);Customer c2=(Customer)s.load(Customer.class, 2);HibernateSessionFactory.closeSession();/关闭Session缓存System.out.println(-前后分割线-);Session s1=HibernateSessionFactory.getSession();/新Session缓存Customer c3=(Customer)s1.get(Customer.class, 1);Customer c4=(Customer)s1.load(Customer.class, 2);HibernateSessionFactory

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论