Hibernate中的saveorupdate无法在hibernate中使用@generated值

时间:2013-02-27 10:57:29

标签: hibernate annotations

我在hibernate中遇到了saveOrUpdate()函数的问题。 我正在使用Oracle数据库。我需要插入一个带有自动生成主键的行。我在hibernate注释中配置了这个id,如下所示,

Model.java

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column (name = "blush_id")
private Integer blsId; 

但是在执行插入查询时,只会生成id。 saveOrUpdate方法未执行。

DAO.java

private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory){
    this.sessionFactory = sessionFactory;
} 
public void executeSaveOrUpdate(Object obj){            
    sessionFactory.getCurrentSession().saveOrUpdate(obj);//id generation occurs but the saveOrUpdate(obj) method is not executed
}

没有抛出错误或异常,但是插入查询没有执行(而是在控制台的第二行生成序列),控制进入下一个进程。

控制台:

16:41:55,928 INFO  [STDOUT] Hibernate: select category0_.cat_nbr as cat1_2_, category0_.added_by_name as added2_2_, category0_.added_ts as added3_2_, category0_.cat_dsc as cat4_2_, category0_.last_updt_by_name as last5_2_, category0_.last_updt_ts as last6_2_, category0_.mdse_grp_dsc as mdse7_2_, category0_.mdse_grp_nbr as mdse8_2_ from cvs_category_admin category0_ order by category0_.cat_nbr
16:41:56,037 INFO  [STDOUT] Hibernate: select hibernate_sequence.nextval from dual
16:41:56,084 INFO  [STDOUT] Hibernate: select blushrules0_.blush_id as blush1_1_, blushrules0_.added_by_name as added2_1_, blushrules0_.added_ts as added3_1_, blushrules0_.blush_item_type as blush4_1_, blushrules0_.cat_nbr as cat5_1_, blushrules0_.subcat_nbr as subcat12_1_, blushrules0_.is_active as is6_1_, blushrules0_.is_added as is7_1_, blushrules0_.last_updt_by_name as last8_1_, blushrules0_.last_updt_ts as last9_1_, blushrules0_.sku_nbr as sku10_1_, blushrules0_.status as status1_ from blush_admin_rules blushrules0_ where blushrules0_.status='A'

请帮我解决这个问题..

2 个答案:

答案 0 :(得分:0)

这是一个提交事务的问题,特别是在多个查询的情况下。

答案 1 :(得分:-1)

您还需要刷新会话。将以下内容添加到saveupdate语句中。

sessionFactory.getCurrentSession().flush()