日期:2014-05-20 浏览次数:20617 次
package gpf; public class Dog { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="gpf.Dog" table="Dog"> <id name="id" type="java.lang.Integer"> <column name="id"/> <generator class="native"></generator> </id> <property name="name" type="java.lang.String"> <column name="name"/> </property> </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="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">123</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.pool_size">1</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="format_sql">true</property> <property name="show_sql">true</property> <mapping resource="gpf/Dog.hbm.xml"/> </session-factory> </hibernate-configuration>
public class Test { private Session session = new AnnotationConfiguration().configure().buildSessionFactory().getCurrentSession(); @org.junit.Test public void test(){ session.beginTransaction(); Query query = session.createQuery("from Dog"); Iterator<Dog> iterator = query.iterate(); session.getTransaction().commit(); }