主键字段的Oracle序列不会检索Hibernate中当前插入的值 [英] Oracle sequence for a primary key field doesn't retrieve the currently inserted value in Hibernate

查看:120
本文介绍了主键字段的Oracle序列不会检索Hibernate中当前插入的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码试图为 COUNTRY 的主键列 COUNTRY_ID 提取生成的序列值>表在我的Oracle(10g)数据库中。

  Country c = new Country(); 

c.setCountryName(request.getParameter(txtCountryName));
c.setCountryCode(request.getParameter(txtCountryCode));
Zone z =(Zone)session.get(Zone.class,new BigDecimal(request.getParameter(zoneId)));
c.setZone(z);
session.save(c);

session.flush();
//session.refresh(c);
System.out.println(c.getCountryId()); //尝试显示序列值。
session.getTransaction()。commit();

这条语句 System.out.println(c.getCountryId());此代码中的尝试在插入完成后显示当前生成的序列值。

在Oracle表中的序列生成值被插入 2的倍数,即,就像414,416,418,420 ...将它们插入414,415,416,417,418,419,420 ...等链中。假设,当前在Oracle表中插入的序列值是 426 ,这个语句 System.out.println(c.getCountryId()); 显示 425 (而在Oracle表的 COUNTRY_ID 列中,插入的值是 426



可能是由于某些原因,该序列执行了两次






在我的Hibernate POJO中,我指定了 countryId 主键列如下,

  @Id 
@Column(名称= COUNTRY_ID)
@SequenceGenerator(名称= CountryIdSequence,sequenceName =COUNTRY_SEQ )
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =Co untryIdSequence)
private long countryId;

Country.hbm.xml 映射中文件中,此列 countryId 映射如下。

 < id name =countryIdtype =long> 
< column name =COUNTRY_IDprecision =35scale =0/>
< generator class =sequence>
< param name =sequence> COUNTRY_SEQ< / param>
< / generator>
< / id>

我在这里错过了什么?

解决方案

确实有一个针对 COUNTRY 表的触发器,该表由Oracle GUI工具自动创建,用于将序列与主键列关联 COUNTRY_ID



该序列由Hibernate执行两次,再由触发器执行。我暂时停用了触发器,该方法按预期工作。


I have the following code that attempts to fetch a generated sequence value for the primary key column COUNTRY_ID of the COUNTRY table in my Oracle (10g) database.

Country c=new Country();

c.setCountryName(request.getParameter("txtCountryName"));
c.setCountryCode(request.getParameter("txtCountryCode"));
Zone z=(Zone) session.get(Zone.class, new BigDecimal(request.getParameter("zoneId")));
c.setZone(z);
session.save(c);

session.flush();
//session.refresh(c);
System.out.println(c.getCountryId()); //Attempts to display the sequence value.
session.getTransaction().commit();     

This statement System.out.println(c.getCountryId()); in this codes attempts to display the currently generated sequence value after insertion is done.

Sequence generated values in the Oracle table are inserted in multiple of 2 i.e something like this 414, 416, 418, 420... instead of being inserted them in a chain like 414, 415, 416, 417, 418, 419, 420...

Suppose, the currently inserted sequence value in the Oracle table is 426, this statement System.out.println(c.getCountryId()); shows 425 (whereas in the COUNTRY_ID column in the Oracle table, the inserted value is 426).

Presumably, it appears that the sequence is executed twice for some reasons.


In my Hibernate POJO, I have designated the countryId primary key column as follows,

@Id
@Column(name = "COUNTRY_ID")
@SequenceGenerator(name = "CountryIdSequence", sequenceName = "COUNTRY_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CountryIdSequence")
private long countryId;

In the Country.hbm.xml mapping file, this column countryId is mapped as follows.

<id name="countryId" type="long">
  <column name="COUNTRY_ID" precision="35" scale="0"/>
  <generator class="sequence">
    <param name="sequence">COUNTRY_SEQ</param>
  </generator>
</id>

What am I missing here?

解决方案

I indeed had a trigger against the COUNTRY table which is automatically created by an Oracle GUI tool to associate the sequence with the primary key column COUNTRY_ID.

The sequence was being executed twice once by Hibernate and again by the trigger. I disabled the trigger temporarily and the approach worked as intended.

这篇关于主键字段的Oracle序列不会检索Hibernate中当前插入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆