Hibernate为Oracle自动创建表且主键自增

转自http://hua04104.javaeye.com/blog/669104
1.注意:实体类映射表字段时不要使用oralce关键字,否则不能映射成功。
2.步骤:
    (1)往oralce里建一个sequence:     
     Sql代码 
     create sequence seq_id_supervision minvalue 1 start with 1 increment by 1 

         注:如果多个表要映射自增长主键,每个表要建一个sequence。
    (2)model映射配置:
        
Xml代码  
  1. < id   name = "id" >   
  2.        < generator   class = "sequence" >   
  3.             < param   name = "sequence" >   
  4.                seq_id_letter  
  5.             </ param >   
  6.        </ generator >   
  7. </ id >   
  8.          

    (3)Hibernate映射文件配置:
        
Xml代码  
  1. < hibernate-configuration >   
  2.     < session-factory >   
  3.         < property   name = "hibernate.connection.driver_class" >   
  4.               oracle.jdbc.driver.OracleDriver  
  5.         </ property >   
  6.         < property   name = "hibernate.connection.url" >   
  7.               jdbc:oracle:thin:@localhost:1521:jf  
  8.         </ property >   
  9.         < property   name = "hibernate.connection.username" > jf </ property >   
  10.         < property   name = "hibernate.connection.password" > jf </ property >   
  11.         < property   name = "hibernate.dialect" >   
  12.              org.hibernate.dialect.OracleDialect  
  13.         </ property >   
  14.         < property   name = "hibernate.show_sql" > true </ property >   
  15.         < property   name = "hibernate.hbm2ddl.auto" > update </ property >   
  16.         < property   name = "hibernate.cache.provider_class" >   
  17.               org.hibernate.cache.HashtableCacheProvider  
  18.         </ property >   
  19.         < mapping   resource = "com/chinasoft/jfb/model/LetterInformation.hbm.xml"   />   
  20.      </ session-factory >   
  21. </ hibernate-configuration >   
  22.            

你可能感兴趣的:(oracle,Hibernate,xml,jdbc,Class,generator)