我的数据库操作使用的是hibernate。在save 的时候是将主键设置为0,然后在数据库中插入时,利用触发器调用存储过程获得主键。目前数据库插入是没问题的,但是,利用Long id = (Long)this.getHibernateTemplate().save(entity);
依然得不到主键请问,哪位大侠知道。。hibernate是否可以得到主键呢
通过什么方式呢。。

解决方案 »

  1.   

    用find方法查询主键~
    Long id = (Long)this.getHibernateTemplate().find("select id from entity ");
      

  2.   

    to longlonglong25:
    没有明白你的意思,
    entity是我要save的对象,
    在save的时候,通过在数据库中设置before insert的触发器,来为entity付主键,
    我如何可以得到触发器为这个entity付的主键呢??
      

  3.   

    this.getHibernateTemplate().save(entity);
    long id=entity.getId();
    试验下!
      

  4.   

    这跟实体在hibernate中的状态有关了,你插入的时候是持久态你得到的ID也就是持久态的ID,你插入后调用了存储过程,修改了ID,这时的ID就是脱管状态下的ID,而hibernateSession中还是持久态的ID,你当然得不到了。
      

  5.   

    我看到你给我的留言了
    这是我常用的spring的配置:<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <!-- 定义数据源Bean,使用C3P0数据源实现 -->
    <bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <!-- 指定连接数据库的驱动 -->
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <!-- 指定连接数据库的URL -->
    <property name="jdbcUrl" value="jdbc:mysql://localhost/ems" />
    <!-- 指定连接数据库的用户名 -->
    <property name="user" value="root" />
    <!-- 指定连接数据库的密码 -->
    <property name="password" value="" />
    <!-- 指定连接数据库连接池的最大连接数 -->
    <property name="maxPoolSize" value="40" />
    <!-- 指定连接数据库连接池的最小连接数 -->
    <property name="minPoolSize" value="1" />
    <!-- 指定连接数据库连接池的初始化连接数 -->
    <property name="initialPoolSize" value="1" />
    <!-- 指定连接数据库连接池的连接的最大空闲时间 -->
    <property name="maxIdleTime" value="20" />
    </bean> <!-- 定义Hibernate的SessionFactory -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <!-- 依赖注入数据源,注入正是上面定义的dataSource -->
    <property name="dataSource" ref="dataSource" />
    <!-- mappingResouces属性用来列出全部映射文件 -->
    <property name="mappingResources">
    <list>
    <!-- 以下用来列出Hibernate映射文件 -->
    <value>包名/XXX.hbm.xml</value>
    </list>
    </property>
    <!-- 定义Hibernate的SessionFactory的属性 -->
    <property name="hibernateProperties">
    <props>
    <!-- 指定数据库方言 -->
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLInnoDBDialect
    </prop>
    <!-- 是否根据需要每次自动创建数据库 -->
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <!-- 显示Hibernate持久化操作所生成的SQL -->
    <prop key="hibernate.show_sql">true</prop>
    <!-- 将SQL脚本进行格式化后再输出 -->
    <prop key="hibernate.format_sql">true</prop>
    </props>
    </property>
    </bean> <!-- 配置Hibernate的局部事务管理器,使用HibernateTransactionManager类 -->
    <!-- 该类实现PlatformTransactionManager接口,是针对Hibernate的特定实现-->
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <!-- 配置HibernateTransactionManager时需要依注入SessionFactory的引用 -->
    <property name="sessionFactory" ref="sessionFactory" />
    </bean> <!-- 配置事务切面Bean,指定事务管理器 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!-- 用于配置详细的事务语义 -->
    <tx:attributes>
    <!-- 所有以'get'开头的方法是read-only的 -->
    <tx:method name="get*" read-only="true" />
    <!-- 其他方法使用默认的事务设置 -->
    <tx:method name="*" />
    </tx:attributes>
    </tx:advice> <aop:config>
    <!-- 配置一个切入点,匹配xxxx包下
    所有以Impl结尾的类的所有方法的执行 -->
    <aop:pointcut id="leePointcut"
    expression="execution(* xxxx.*Impl.*(..))" />
    <!-- 指定在leePointcut切入点应用txAdvice事务切面 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="leePointcut" />
    </aop:config>
    </beans>
      

  6.   

    再次感谢啊。。
    我测了下,如果我的主键策略采用ORACLE序列的话,初始化为0,
    再存储后,entity的主键会是序列中的值,
    说明我的transaction的配置应该也没问题,(我的transaction和你的差不多)
    我在想7楼的话,
    是不是通过hibernate是拿不到通过我这个流程存储后的主键??(触发器调用存储过程),
    我也试了下hibernate3提供的触发器策略,
    也不可以,(可能是因为我的触发器是通过调用存储过程得到id?)
      

  7.   

    这跟配置文件的id生成方式有关,如果使用oracle的序列生成主键的话,不管你在程式中是否设置了主键值,都会使用序列,hibernate会先select 序列的值,然后再set进save(object)中的object中去的,也就是说只要save了,那么object中的id就得到了序列的值然后再生成insert语句插入数据库,这个对象也被称之为PO。被持久化的对象。long id=entity.getId(); 就可以得到。
      

  8.   

    因为hibernateSession中保存的是你持久态的对象,而你通过存储过程修改了对象的值,这时对象其实应该是脱离态了,而hibernateSession并不知道,他保存的还是持久态对象的数据,你就需要通过hibernate得到数据库中的数据,hibernate会自动的检查hibernateSession中的对象是否更新,并保存为持久态,这时得到的数据就你修改后的数据了
      

  9.   

    因为hibernateSession中保存的是你持久态的对象,而你通过存储过程修改了对象的值,这时对象其实应该是脱离态了,而hibernateSession并不知道,他保存的还是持久态对象的数据,你就需要通过hibernate得到数据库中的数据,hibernate会自动的检查hibernateSession中的对象是否更新,并保存为持久态,这时得到的数据就你修改后的数据了
      

  10.   

    非常感谢大家的帮助~问题解决了(lh446所说,用hibernate从数据库再取一次),
    解决办法:this.getHibernateTemplate().save(entity);
    entity = this.getHibernateTemplate().findByExample(entity).get(0);结贴散分~