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
                  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 >
          <id name="id">
	        <generator class="sequence">
		       <param name="sequence">
			     seq_id_letter
			</param>
		</generator>
	 </id>
         

(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 >
         <hibernate-configuration>
<session-factory>
	<property name="hibernate.connection.driver_class">
		oracle.jdbc.driver.OracleDriver
	</property>
	<property name="hibernate.connection.url">
		jdbc:oracle:thin:@localhost:1521:jf
	</property>
	<property name="hibernate.connection.username">jf</property>
	<property name="hibernate.connection.password">jf</property>
	<property name="hibernate.dialect">
             org.hibernate.dialect.OracleDialect
        </property>
	<property name="hibernate.show_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>
	<property name="hibernate.cache.provider_class">
		org.hibernate.cache.HashtableCacheProvider
	</property>
	<mapping resource="com/chinasoft/jfb/model/LetterInformation.hbm.xml" />
</session-factory>
</hibernate-configuration>
         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值