Hibernate工作总结
编程技术  /  houtizong 发布于 3年前   87
private Integer id;private String companyname;private String linkman;private String telephone;private String email;private Login login;映射文件:
<class name="cn.changluo.Login" table="login" catalog="test"> <id name="id" type="integer"> <column name="id" length="4"/> <generator class="foreign"> <param name="property">company</param> </generator> </id> <property name="loginname" type="string" column="loginname"/> <property name="loginpwd" type="string" column="loginpwd"/> <one-to-one name="company" class="cn.changluo.Company" constrained="true"></one-to-one> </class>Login类:
private Integer id;private String loginname;private String loginpwd;private Company company;映射文件:
<class name="cn.changluo.Company" table="company" catalog="test"> <id name="id" type="integer"> <column name="id" length="4"/> <generator class="identity"></generator> </id> <property name="companyname" type="string" column="companyname"/> <property name="email" type="string" column="email"/> <property name="linkman" type="string" column="linkman"/> <property name="telephone" type="string" column="telephone"/> <one-to-one name="login" class="cn.changluo.Login" cascade="all" lazy="false"></one-to-one> </class>测试:
public static void main(String[] args) {save();query();}public static void save(){Configuration cfg = new Configuration();cfg.configure("hibernate.cfg.xml");SessionFactory sf = cfg.buildSessionFactory();Session session = sf.openSession();Transaction tx = session.beginTransaction();Company c = new Company();c.setCompanyname("gouyundong");c.setEmail("[email protected]");c.setLinkman("zhangsan");c.setTelephone("13329902303");Login l = new Login();l.setLoginname("LL");l.setLoginpwd("ll");c.setLogin(l);l.setCompany(c);session.save(c);session.save(l);tx.commit();}public static void query(){Configuration cfg = new Configuration();cfg.configure("hibernate.cfg.xml");SessionFactory sf = cfg.buildSessionFactory();Session session = sf.openSession();Company c = (Company) session.get(Company.class, 1);System.out.println(c.getCompanyname());}显示的SQL语句:
Hibernate: insert into test.company (companyname, email, linkman, telephone) values (?, ?, ?, ?)Hibernate: insert into test.login (loginname, loginpwd, id) values (?, ?, ?)Hibernate: select company0_.id as id10_1_, company0_.companyname as companyn2_10_1_, company0_.email as email10_1_, company0_.linkman as linkman10_1_, company0_.telephone as telephone10_1_, login1_.id as id11_0_, login1_.loginname as loginname11_0_, login1_.loginpwd as loginpwd11_0_ from test.company company0_[b] left outer join [/b]test.login login1_ on company0_.id=login1_.id where company0_.id=?一对一关联在默认的情况下采用的是迫切做外连接的检索策略。
private Integer id;private String province;private String city;private String street;private String zipcode;private Client client;映射文件:
<class name="cn.changluo.Address" table="address" catalog="test"> <id name="id" type="integer"> <column name="id" length="4"/> <generator class="identity"></generator> </id> <property name="province" type="string" column="province"/> <property name="city" type="string" column="city"/> <property name="street" type="string" column="street"/> <property name="zipcode" type="string" column="zipcode"/> <one-to-one name="client" class="cn.changluo.Client" property-ref="address"></one-to-one> </class>Client类:
private Integer id;private String clientname;private String phone;private String email;private Address address;映射文件:
<class name="cn.changluo.Client" table="client" catalog="test"><id name="id" type="integer"><column name="id" length="4" /><generator class="identity"></generator></id><property name="clientname" type="string" column="clientname" /><property name="email" type="string" column="email" /><property name="phone" type="string" column="phone" /><many-to-one name="address" class="cn.changluo.Address"column="address" lazy="false" cascade="all" unique="true"></many-to-one></class>测试:
public static void main(String args[]){save();}public static void save(){Configuration cfg = new Configuration();cfg.configure("hibernate.cfg.xml");SessionFactory sf = cfg.buildSessionFactory();Session session = sf.openSession();Transaction tx = session.beginTransaction();Address a = new Address();a.setProvince("FUJIAN");a.setCity("XM");a.setStreet("GAOXIONG road");a.setZipcode("361000");Client c = new Client();c.setClientname("changluo");c.setEmail("[email protected]");c.setPhone("8344793");c.setAddress(a);a.setClient(c);session.save(c);session.save(a);tx.commit();}多对一单项关联关系:
private Integer id;private String cname;private String bank;private String phone;
<class name="cn.changluo.Customer" table="customer"catalog="test"><id name="id" type="integer"><column name="id" length="4" /><generator class="identity"></generator></id><property name="cname" type="string" column="cname" /><property name="bank" type="string" column="bank" /><property name="phone" type="string" column="phone" /></class>Order类:
private Integer id;private String orderno;private double money;private Customer customer;映射文件:
<class name="cn.changluo.Order" table="order" catalog="test"><id name="id" type="integer"><column name="id" length="4" /><generator class="identity"></generator></id><property name="orderno" type="string" column="orderno" /><property name="money" type="double" column="money" /><many-to-one name="customer" column="customer_id"class="cn.changluo.Customer" lazy="false" not-null="true"></many-to-one></class>测试:
Configuration cfg = new Configuration();cfg.configure("hibernate.cfg.xml");SessionFactory sf = cfg.buildSessionFactory();Session session = sf.openSession();Transaction tx = session.beginTransaction();Customer c = new Customer();c.setBank("GSYH");c.setCname("LSL");c.setPhone("0234403");Order o = new Order();o.setMoney(669);o.setOrderno("208203");o.setCustomer(c);session.save(c);session.save(o);tx.commit();多对一双向关联:
private Integer id;private String cname;private String bank;private String phone;private Set orders = new HashSet();映射文件:
<class name="cn.changluo.Customer" table="customer"catalog="test"><id name="id" type="integer"><column name="id" length="4" /><generator class="identity"></generator></id><property name="cname" type="string" column="cname" /><property name="bank" type="string" column="bank" /><property name="phone" type="string" column="phone" /><set name="orders" cascade="all" inverse="true" lazy="false"><key column="customer_id"></key><one-to-many class="cn.changluo.Order"/></set></class>Order类和Order的映射文件同单项映射一致
Configuration cfg = new Configuration();cfg.configure("hibernate.cfg.xml");SessionFactory sf = cfg.buildSessionFactory();Session session = sf.openSession();Transaction tx = session.beginTransaction();Customer c = new Customer();c.setBank("GSYH");c.setCname("LSL");c.setPhone("0234403");Order o = new Order();o.setMoney(669);o.setOrderno("208203");o.setCustomer(c);Order o1 = new Order();o1.setMoney(390);o1.setOrderno("oisd");o1.setCustomer(c);Set orders = new HashSet();orders.add(o);orders.add(o1);c.setOrders(orders);session.save(c);session.save(o);tx.commit();映射一对多双向自身关联关系:
private Integer id;private String cateno;private String catename;private Goodscate parent;private Set childs = new HashSet();映射文件:
<class name="cn.changluo.Goodscate" table="goodscate"catalog="test"><id name="id" type="integer"><column name="id" length="4" /><generator class="identity"></generator></id><property name="cateno" type="string" column="cateno" /><property name="catename" type="string" column="catename" /><many-to-one name="parent" column="parentid"class="cn.changluo.Goodscate" lazy="false"></many-to-one><set name="childs" cascade="all" lazy="false" inverse="true"><key><column name="parentid"></column></key><one-to-many class="cn.changluo.Goodscate"/></set></class>测试:
Configuration cfg = new Configuration();cfg.configure("hibernate.cfg.xml");SessionFactory sf = cfg.buildSessionFactory();Session session = sf.openSession();Transaction tx = session.beginTransaction();Goodscate pgs = new Goodscate();pgs.setCateno("001");pgs.setCatename("no1");Goodscate cgs = new Goodscate();cgs.setCateno(pgs.getCateno()+"_"+001);cgs.setCatename("no101");pgs.getChilds().add(cgs);cgs.setParent(pgs);session.save(pgs);session.save(cgs);tx.commit();
private Integer id;private String itemno;private String itemname;映射文件:
<class name="cn.changluo.Items" table="items" catalog="test"> <id name="id" type="integer"> <column name="id" length="4"/> <generator class="identity"></generator> </id> <property name="itemno" type="string" column="itemno"/> <property name="itemname" type="string" column="itemname"/> </class>Orders类:
private Integer id;private String orderno;private double money;private Set items = new HashSet();映射文件:
<class name="cn.changluo.Orders" table="orders" catalog="test"> <id name="id" type="integer"> <column name="id" length="4"/> <generator class="identity"></generator> </id> <property name="orderno" type="string" column="orderno"/> <property name="money" type="double" column="money"/> <set name="items" table="selectitems" lazy="true" cascade="save-update"> <key column="orderid"></key> <many-to-many class="cn.changluo.Items" column="itemid"></many-to-many> </set> </class>测试类:
Configuration cfg = new Configuration();cfg.configure("hibernate.cfg.xml");SessionFactory sf = cfg.buildSessionFactory();Session session = sf.openSession();Transaction tx = session.beginTransaction();Items i1 = new Items();i1.setItemname("车");i1.setItemno("car");Items i2 = new Items();i2.setItemname("食物");i2.setItemno("food");Set items = new HashSet();items.add(i1);items.add(i2);Orders o = new Orders();o.setMoney(200);o.setOrderno("2220");o.setItems(items);session.save(i1);session.save(i2);session.save(o);
<class name="cn.changluo.Customer" table="customer"catalog="test" lazy="false"><id name="id" type="integer"><column name="id" length="4" /><generator class="identity"></generator></id><property name="cname" type="string" column="cname" /><property name="bank" type="string" column="bank" /><property name="phone" type="string" column="phone" /><set name="orders" cascade="all" inverse="false" lazy="false"><key column="customer_id"></key><one-to-many class="cn.changluo.Order"/></set></class>当关联关系中的lazy属性值,设置为false时,当装载Customer后,会立即装载与当前Customer对象一对多管理啊你的所有Order对象;
<set name="orders" cascade="all" inverse="true" lazy="false" batch-size="5"><key column="customer_id"></key><one-to-many class="cn.changluo.Order"/></set>设置batch-size来设置批量检索的大小,这个值依据系统的实际情况进行设置,当设置了批量检索的值之后,可以发现仅执行了一条SQL语句:
select orders0_.customer_id as customer4_1_, orders0_.id as id1_, orders0_.id as id7_0_, orders0_.orderno as orderno7_0_, orders0_.money as money7_0_, orders0_.customer_id as customer4_7_0_ from test.order orders0_ [b]where orders0_.customer_id in (?, ?)[/b]不管<class>标签中的lazy属性为何值,当使用Session实例的get()方法装载持久化对象时,均使用立即检索
<set name="orders" cascade="all" inverse="true" lazy="true" batch-size="5"><key column="customer_id"></key><one-to-many class="cn.changluo.Order"/></set>
Customer cust = (Customer) session.get(Customer.class, 1);if(!Hibernate.isInitialized(cust.getOrders())){Hibernate.initialize(cust.getOrders());}session.close();要在Session范围内进行初始化。
<set name="orders" cascade="all" inverse="false" outer-join="true"><key column="customer_id"></key><one-to-many class="cn.changluo.Order"/></set>当使用session.createQuery(“from Customer as a”).list()查询所有的Customer对象时,迫切左外连接检索策略将被忽略而失效,检索策略有lazy属性决定。
<property name="hibernate.connection.isolation">2</property>设置隔离级别:1、读未提交数据;2、读已提交数据;4、可重复度、8、序列化
Configuration cfg = new Configuration();cfg.configure("hibernate.cfg.xml");SessionFactory sf = cfg.buildSessionFactory();Session session = sf.openSession();Query query = session.createQuery("from Customer as c");[b]query.setLockMode("c", LockMode.UPGRADE);[/b]Iterator it = query.list().iterator();Customer cust = null;while(it.hasNext()){cust = (Customer)it.next();System.out.println(cust.getCname());System.out.println(cust.getPhone());}LockMode.UPGRADE:利用数据库的for update字句实现锁定
select customer0_.id as id6_, customer0_.cname as cname6_, customer0_.bank as bank6_, customer0_.phone as phone6_ from test.customer customer0_ [b]for update[/b]乐观锁:它通常认为多个事务同是操纵一个数据的情况是很少的,因而根本不做数据库层次上的锁定,只是基于数据的版本表示实现应用程序级别上的锁定机制。基于数据的版本标识就是为数据添加一个版本标识,一般通过数据库表增加一个“version”字段来实现。
private Integer id;private String cname;private String bank;private String phone;private Integer version;
<version name="version" column="version"></version>
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接