读书人

Hibernate 运用 Annotation 2(主键生

发布时间: 2012-09-21 15:47:26 作者: rapoo

Hibernate 使用 Annotation 2(主键生成方式)

hibernate.cfg.xml配置文件:

?

JleeTest.java代码:

package com.jlee02.createId;import junit.framework.TestCase;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.tool.hbm2ddl.SchemaExport;public class JleeTest extends TestCase {public void testJlee(){SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory() ;Session session = sf.getCurrentSession() ;session.beginTransaction() ;Jlee jlee = new Jlee() ;jlee.setName("JLee") ;jlee.setAge(23) ;jlee.setPhone("12123123123") ;session.save(jlee) ;session.getTransaction().commit() ;}//相当于hbm2ddl 设为 createpublic void testExport(){new SchemaExport(new AnnotationConfiguration().configure()).create(false, true) ;}}

?

读书人网 >软件架构设计

热点推荐