2 doumanggu2230 doumanggu2230 于 2017.08.28 15:47 提问

整合Spring+Hibernate时,主键策略是assigned插入不进数据,也不报任何错!

在save()前打印了一下实体类,save()后也打印了一下实体类,都是有值,但是数据库里就是插入不进数据,我把Student.hbm.xml中的assigned改成native,就能插入进数据。。。

Dao层

 public class UserDaoImpl implements UserDao {

    private SessionFactory sessionFactory;

    @Autowired
    public void setSessionFactoryOverride(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void addStudent(Student student) {
        System.out.println(sessionFactory);
        System.out.println("添加前:" + student);
        sessionFactory.getCurrentSession().save(student);
    }
}

Student.hbm.xml

<hibernate-mapping>  
    <class name="com.zlm.entity.Student" table="student">    
        <id name="studentId" type="java.lang.Integer" >
            <column name="studentId" />
            <generator class="assigned" />
        </id>
        <property name="password" type="java.lang.String">  
            <column name="password" />  
        </property>  
        <property name="identify" type="java.lang.Integer">  
            <column name="identify" />  
        </property>  
    </class>  
</hibernate-mapping> 

实体类就是
private int studentId;
get
set

applicationContext.xml

<beans>
    <context:component-scan base-package="com.zlm"
        use-default-filters="false" />
    <context:property-placeholder location="classpath:config/db.properties" />
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"></property>
        <property name="url" value="${jdbc.jdbcUrl}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>

            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/zlm/entity/Student.hbm.xml</value>
            </list>
        </property>

    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>

5个回答

doumanggu2230
doumanggu2230   2017.08.28 15:49

图片说明

YAN_HUAXIANGMO
YAN_HUAXIANGMO   2017.08.28 16:09

assigned数据库主键增长方式,主键必须是手动分配的,如果不分配,就出错了。
要这样user.setId("01");

doumanggu2230
doumanggu2230 我都设置了,在controller层中,student.setStudentId(username);这个username就是我从页面上获取来得值。所以我说在save()前后我都打印了一下实体类的tostring方法,都是有值的
3 个月之前 回复
weixin_37697279
weixin_37697279   2017.08.28 16:34

assigned是自己生成的,比如用UUID,然后复制给对象,再save,native是自动选择的。你现在代码是怎么实现的,传递到dao层的时候ID是有值的?

doumanggu2230
doumanggu2230 有值的啊。。在controller层中,student.setStudentId(username);这个username就是我从页面上获取来得值。
3 个月之前 回复
doumanggu2230
doumanggu2230   2017.08.28 16:43

贴上controller层代码

    @RequestMapping(value = "register.do")
    public String register(HttpServletRequest request) {
        String usernameStr = request.getParameter("username");
        String password = request.getParameter("password");
        String identifyStr = request.getParameter("identify");
        int username = Integer.parseInt(usernameStr);
        int identify = Integer.parseInt(identifyStr);
        Student student = new Student();
        student.setStudentId(username);
        student.setPassword(password);
        student.setIdentify(identify);
        userService.addStudent(student);
        System.out.println("添加后:" + student);
        return "success";
    }
doumanggu2230
doumanggu2230   2017.08.28 17:02

解决了,还是自己hibernate学的太笼统了。在DaoImpl类中,把插入代码改成就好了,assigned不像native一样能支持事物自动提交。

    public void addStudent(Student student) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(student);
        session.getTransaction().commit();
        session.close();
    }
doumanggu2230
doumanggu2230 回复vvvvip用户: 是啊。我改成native原先的代码就完全OK
3 个月之前 回复
wohuozheng
wohuozheng 你把assigned改成native就能自动提交事务了?
3 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片