hibernate4创建工厂的方法AnnotationConfiguration类过期

p
ackage org.ymm.vo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class StudentTest {
	public static void main(String args[]){
		Student stu=new Student();
		stu.setId(1);
		stu.setAge(20);
		stu.setName("ymm");
		
		Configuration cfg = new Configuration(); 
        cfg.configure();//读取配置文件  
        
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
		SessionFactory factory = cfg.buildSessionFactory(serviceRegistry);  //得到session工厂
		Session session=factory.openSession();			//得到session
		
		session.beginTransaction();
		session.save(stu);
		session.getTransaction().commit();
		session.close();
		factory.close();
	}
}

配置文件
<?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>
  
    <!-- Database connection settings -->
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:mldn</property>
    <property name="connection.username">scott</property>
    <property name="connection.password">tiger</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <mapping class="org.ymm.vo.Student"/>
    
  </session-factory>
</hibernate-configuration>

实体类:
package org.ymm.vo;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Student {
	private int id;
	private int age;
	private String name;
	@Id
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

你可能感兴趣的:(configuration)