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();
	}
}

配置文件





    
  
    
    oracle.jdbc.driver.OracleDriver
    jdbc:oracle:thin:@localhost:1521:mldn
    scott
    tiger

    
    org.hibernate.dialect.Oracle10gDialect

    
    true
    
    
  


实体类:
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;
	}
}

你可能感兴趣的:(hibernate4创建工厂的方法AnnotationConfiguration类过期)