关闭

2012年3月25日 hibernate

346人阅读 评论(0) 收藏 举报

基本hibernate所需的Jar包


hibernate-annotations所需的Jar包


在此基础上用log4j 显示日志所需的Jar包




hibernate.cfg.xml文件 最好不要用其它名字,不然要在configure()方法中加参数

        
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</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>
        <property name="format_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        property name="hbm2ddl.auto">update</property>
        <mapping resource="com/wx/hibernate/Student.hbm.xml"/>
        <!-- 使用hibernate-annotations~~ 目前主要使用 -->
        <mapping class="com.wx.hibernate.Teacher"/>

domain model:TeacherPK.java|Teacher.java


//联合主键第一种 @Embeddable + @id 
//类要实现Serializable接口,重写equals和hashCode方法
//@Embeddable
public class TeacherPK implements java.io.Serializable{
	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;
	}
	
	@Override
	public boolean equals(Object o) {
		if(o instanceof TeacherPK) {
			TeacherPK pk = (TeacherPK)o;
			if(this.id == pk.getId() && this.name.equals(pk.getName())) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return this.name.hashCode();
	}



//定义生成器的名字(下面id处指定这个生成器)
@Entity
@TableGenerator(
	    name="Teacher_GEN",
	    table="GENERATOR_TABLE",
	    pkColumnName = "pk_key",
	    valueColumnName = "pk_value",
	    pkColumnValue="Teacher",
	    allocationSize=1
	)
@SequenceGenerator(name="teacherSEQ", sequenceName="teacherSEQ_DB")
//@IdClass(TeacherPK.class)
//联合主键第三种 @IdClass + @id + @id 
public class Teacher {
	//private TeacherPK pk;
	private int id;
	private String name;
	private String title; 
	private String yourWifeName;
	private Date birthDate;
	private boolean good;
	private Gender gender;
	
	//枚举类型
	@Enumerated(EnumType.STRING)
	public Gender getGender() {
		return gender;
	}
	public void setGender(Gender gender) {
		this.gender = gender;
	}
	public boolean isGood() {
		return good;
	}
	public void setGood(boolean good) {
		this.good = good;
	}
	//透明
	@Transient
	public String getYourWifeName() {
		return yourWifeName;
	}
	public void setYourWifeName(String yourWifeName) {
		this.yourWifeName = yourWifeName;
	}

	//指定id生成策略  和 所用的生成器的名字(应该是不能和联合主键一起用,已测~)
	@Id
	@GeneratedValue(strategy=GenerationType.TABLE, generator="Teacher_GEN")
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	//@Id
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	//时间精度
	@Temporal(TemporalType.TIME)
	public Date getBirthDate() {
		return birthDate;
	}
	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}
	
	/*@EmbeddedId
	 //联合主键第二种 直接@EmbeddedId (个人选这种)
	public TeacherPK getPk() {
		return pk;
	}
	public void setPk(TeacherPK pk) {
		this.pk = pk;
	}*/

JUnit测试类:(建议放在新建的Source Folder-test下)

private static SessionFactory sessionFactory;
	
	@BeforeClass
	public static void beforeClass() {
			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
	}
	@AfterClass
	public static void afterClass() {
		sessionFactory.close();
	}
		
	@Test
	public void testTeacherSave() {	
		Teacher t = new Teacher();
		//t.setId(1);
		t.setName("t1");
		t.setTitle("middle");
		t.setGender(Gender.FEMALE);
		t.setBirthDate(new Date());
		
		//getCurrentSession() 与 openSession() 
		//openSession每次new一个Session对象(新的hibernate不推荐使用)
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(t);
		session.getTransaction().commit();
	}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:18523次
    • 积分:310
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:2篇
    • 译文:0篇
    • 评论:2条
    文章分类