标签:sql xxx 核心 one update round 定义 集合 form
1.1Hibernate的检索方式
1.1.1Hibernate的检索方式概述
Hibernate提供了以下几种检索对象的方式:
1.1.2HQL
1.1.3搭建环境、具体的实体类和映射文件以及核心配置文件
package cn.hibernate3.demo4; import java.io.Serializable; import java.util.HashSet; /** * 客户实体 */ import java.util.Set; public class Customer implements Serializable{ private Integer cid; private String cname; //一个客户有多个订单 private Set<Order> orders = new HashSet<Order>(); public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Set<Order> getOrders() { return orders; } public void setOrders(Set<Order> orders) { this.orders = orders; } }
package cn.hibernate3.demo4; import java.io.Serializable; /** * 订单实体 */ public class Order implements Serializable{ private Integer oid; private String addr; //订单属于某一个客户 private Customer customer ; public Integer getOid() { return oid; } public void setOid(Integer oid) { this.oid = oid; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入约束 --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.hibernate3.demo4.Customer" table="customer"> <!-- 配置唯一标识 --> <id name="cid" column="cid"> <generator class="native"/> </id> <!-- 配置普通属性 --> <property name="cname" column="cname" type="java.lang.String"/> <!-- 建立映射 --> <!-- 配置集合 --> <!-- set标签中的name表示关联对象的属性名称 --> <set name="orders"> <!-- key标签中的column用来一对多的多的一方的外键 --> <key column="cno"/> <!-- 配置一个one-to-many --> <one-to-many class="cn.hibernate3.demo4.Order" /> </set> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入约束 --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.hibernate3.demo4.Order" table="orders"> <!-- 配置唯一标识 --> <id name="oid" column="oid"> <generator class="native"/> </id> <!-- 配置普通属性 --> <property name="addr" column="addr" type="java.lang.String"/> <!-- 建立映射 --> <!-- many-to-one标签 属性: name:关联对象的属性名称。 column:表中外键的名称。 class:关联对象的全路径。 --> <many-to-one name="customer" column="cno" class="cn.hibernate3.demo4.Customer"></many-to-one> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置数据库的基本信息 --> <!-- 驱动的名称 --> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 访问数据库的url --> <property name="hibernate.connection.url"> jdbc:mysql:///hibernate_day03 </property> <!-- 用户名 --> <property name="hibernate.connection.username">root</property> <!-- 密码 --> <property name="hibernate.connection.password">root</property> <!-- 方言 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- C3P0连接池设定--> <!-- 使用c3po连接池 配置连接池提供的供应商--> <property name="connection.provider_class"> org.hibernate.connection.C3P0ConnectionProvider </property> <!--在连接池中可用的数据库连接的最少数目 --> <property name="c3p0.min_size">5</property> <!--在连接池中所有数据库连接的最大数目 --> <property name="c3p0.max_size">20</property> <!--设定数据库连接的过期时间,以秒为单位, 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 --> <property name="c3p0.timeout">120</property> <!--每3000秒检查所有连接池中的空闲连接 以秒为单位--> <property name="c3p0.idle_test_period">3000</property> <!-- 可选配置 --> <!-- 显示SQL --> <property name="hibernate.show_sql">true</property> <!-- 格式化SQL --> <property name="hibernate.format_sql">true</property> <!-- hbm:映射 2:to ddl:create drop alter --> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="cn/hibernate3/demo4/Customer.hbm.xml" /> <mapping resource="cn/hibernate3/demo4/Order.hbm.xml" /> </session-factory> </hibernate-configuration>
package cn.utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * Hibernate抽取工具类 */ public class HibernateUtils { private static Configuration configuration; private static SessionFactory sessionFactory; static{ configuration = new Configuration().configure(); sessionFactory = configuration.buildSessionFactory(); } public static Session openSession(){ return sessionFactory.openSession(); } public static void main(String[] args) { openSession(); } }
@Test //初始化数据 public void demo1(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); Customer customer = new Customer(); customer.setCname("小金"); for (int i = 0; i < 10; i++) { Order order = new Order(); order.setAddr("江苏"+i); order.setCustomer(customer); customer.getOrders().add(order); } session.save(customer); tx.commit(); session.close(); }
@Test //初始化数据 public void demo1(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); Customer customer = new Customer(); customer.setCname("小名"); for (int i = 0; i < 10; i++) { Order order = new Order(); order.setAddr("北京"+i); order.setCustomer(customer); customer.getOrders().add(order); } session.save(customer); tx.commit(); session.close(); }
<?xml version="1.0" encoding="UTF-8"?> <!-- 引入约束 --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.hibernate3.demo4.Customer" table="customer"> <!-- 配置唯一标识 --> <id name="cid" column="cid"> <generator class="native"/> </id> <!-- 配置普通属性 --> <property name="cname" column="cname" type="java.lang.String"/> <!-- 建立映射 --> <!-- 配置集合 --> <!-- set标签中的name表示关联对象的属性名称 --> <set name="orders" cascade="save-update"> <!-- key标签中的column用来一对多的多的一方的外键 --> <key column="cno"/> <!-- 配置一个one-to-many --> <one-to-many class="cn.hibernate3.demo4.Order" /> </set> </class> </hibernate-mapping>
1.1.4简单查询及别名查询
@Test //查询所有数据 public void demo2(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); Query query = session.createQuery("from Customer"); List<Customer> list = query.list(); for (Customer customer : list) { System.out.println(customer.getCname()); } tx.commit(); session.close(); }
@Test //查询所有数据 public void demo2(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); List<Customer> list = criteria.list(); for (Customer customer : list) { System.out.println(customer.getCname()); } tx.commit(); session.close(); }
@Test //查询所有数据 public void demo2(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); SQLQuery query = session.createSQLQuery("select * from customer"); List<Customer> list = query.addEntity(Customer.class).list(); for (Customer customer : list) { System.out.println(customer.getCname()); } tx.commit(); session.close(); }
@Test //查询所有数据 public void demo2(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); SQLQuery query = session.createSQLQuery("select * from customer"); List<Object[]> list = query.list(); for (Object[] obj : list) { System.out.println(Arrays.toString(obj)); } tx.commit(); session.close(); }
标签:sql xxx 核心 one update round 定义 集合 form
原文地址:http://www.cnblogs.com/xuweiweiailixing/p/6771569.html