错误如下org.hibernate.exception.GenericJDBCException: could not insert: [cn.itcast.bean.Person]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2186)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at
测试代码:public class PersonServiceBeanTest {
private static PersonService personService; @BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
personService=(PersonService)ctx.getBean("personService");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @Test
public void testSave() {
personService.save(new Person("夏普"));
}
}
xml文件配置如下:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<!--配置数据源-->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!--把数据源产品定义成一个<bean id="datasource"> 这个destroy-method方法就是关闭数据源-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/> <!-- org.gjt.mm.mysql.Driver ,jdbc的驱动类 或者是com.mysql.jdbc.Driver-->
<property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/> <!-- jdbc的 URL -->
<property name="username" value="root"/><!--数据库的用户名密码 -->
<property name="password" value="root"/><!-- -->
<property name="initialSize" value="1"/><!--连接池启动时的初始值-->
<property name="maxActive" value="500"/><!--连接池的最大的连接的值 -->
<property name="maxIdle" value="2"/><!--最大空闲值-->
<property name="minIdle" value="1"/><!-- 最小空闲值 -->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect<!-- 数据库方言 -->
hibernate.hbm2ddl.auto=update<!-- 创建数据库并更新 -->
hibernate.show_sql=true<!-- 显示SQL语句 -->
</value>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><!-- 使用SPRING事务 -->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/><!--是采用注解的方式打开事务,当业务层进行注解的时候,打开事务 -->
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
</beans>
保存数据代码:
import java.util.List;import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.bean.Person;
import cn.itcast.service.PersonService;@Transactional
public class PersonServiceBean implements PersonService {
@Resource private SessionFactory sessionFactory;
public void save(Person person) {
sessionFactory.getCurrentSession().save(person); }
}
Person bean 代码:package cn.itcast.bean;public class Person {
private Integer id;
private String name;
public Person(){}
public Person(String name){
this.name=name;
System.out.println(name);
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Person.hbm.xml 代码:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.bean">
<class name="Person" table="person">
<id name="id">
<generator class="native"/>
</id>
<property name="name" length="10" not-null="true"/>
</class></hibernate-mapping>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货