posts - 57, comments - 4, trackbacks - 0, articles - 10
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

集成Hibernate配置

Posted on 2009-04-15 23:08 hanwei 阅读(159) 评论(0)  编辑  收藏
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/oa</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>   
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping resource="com/bjsxt/oa/model/ACL.hbm.xml"/>
    <mapping resource="com/bjsxt/oa/model/ApproveInfo.hbm.xml"/>
    <mapping resource="com/bjsxt/oa/model/Document.hbm.xml"/>
    <mapping resource="com/bjsxt/oa/model/Module.hbm.xml"/>
    <mapping resource="com/bjsxt/oa/model/Organization.hbm.xml"/>
    <mapping resource="com/bjsxt/oa/model/Person.hbm.xml"/>
    <mapping resource="com/bjsxt/oa/model/Role.hbm.xml"/>
    <mapping resource="com/bjsxt/oa/model/User.hbm.xml"/>
    <mapping resource="com/bjsxt/oa/model/UsersRoles.hbm.xml"/>
    <mapping resource="com/bjsxt/oa/model/Workflow.hbm.xml"/>
  </session-factory>
</hibernate-configuration>






********************************************************************************************


public static void main(String[] args) {
   
            try {
                config = new Configuration().configure(new File(
                        "src/hibernate.cfg.xml"));

                System.out.println("Creating tables...");

                SessionFactory sessionFactory = config.buildSessionFactory();
                session = sessionFactory.openSession();
                tx = session.beginTransaction();

                SchemaExport schemaExport = new SchemaExport(config);
                schemaExport.create(true, true);
//                BasicCorp bc = new BasicCorp();
//                bc.setBasicCorpAddress("jerryhan");
//                bc.setBasicCorpName("特种不对");
//                session.save(bc);
                System.out.println("Table created.");
               
                tx.commit();

            } catch (HibernateException e) {
                e.printStackTrace();
                try {
                    tx.rollback();
                } catch (HibernateException e1){
                    e1.printStackTrace();
                }
            } finally {
                session.close();

            }


          

}   


只有注册用户登录后才能发表评论。


网站导航: