hibernate-3.5.3 Annotations 零配置注释
所需要的包清单:
hibernate-distribution-3.5.3-Final\hibernate3.jar
hibernate-distribution-3.5.3-Final\lib\required\*
hibernate-distribution-3.5.3-Final\lib\jpa\hibernate-jpa-2.0-api-1.0.0.Final.jar
slf4j-nop-1.5.8.jar(这个得单独下载的,我提供在附件里面了^^)
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!-- Database connection settings --><property name="connection.driver_class">oracle.jdbc.OracleDriver</property><property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property><property name="connection.username">superman</property><property name="connection.password">superman</property><!-- JDBC connection pool (use the built-in) --><property name="connection.pool_size">2</property><!-- SQL dialect --><property name="dialect">org.hibernate.dialect.OracleDialect</property><!-- Enable Hibernate's current session context --><property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property><!-- Disable the second-level cache --><property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property><!-- Echo all executed SQL to stdout --><property name="show_sql">true</property><!-- Drop and re-create the database schema on startup --><property name="hbm2ddl.auto">update</property><mapping /></session-factory></hibernate-configuration>
package com.web.entities;import javax.persistence.Entity;import javax.persistence.Id;/**部门实体 @Table(name = "DEPT")可省略*/@Entitypublic class Dept {/**部门编号*/private int deptNo;/**部门名称*/private String dName;/**部门地址*/private String loc;@Idpublic int getDeptNo() {return deptNo;}public void setDeptNo(int deptNo) {this.deptNo = deptNo;}public String getdName() {return dName;}public void setdName(String dName) {this.dName = dName;}public String getLoc() {return loc;}public void setLoc(String loc) {this.loc = loc;}}
package com.lhfc.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.AnnotationConfiguration;import com.web.entities.Dept;/**部门实体测试*/public class DeptTest {public static void main(String[] args) {Dept dept = new Dept();dept.setDeptNo(12);dept.setdName("锦衣卫");dept.setLoc("火焰山A洞B座");//SessionFactory sessionFactory = new AnnotationConfiguration().addPackage("com.web.entities")//.addAnnotatedClass(Dept.class).configure().buildSessionFactory();SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();Session session = sessionFactory.openSession();Transaction transaction = session.beginTransaction();session.save(dept);transaction.commit();session.close();}}