代码之家  ›  专栏  ›  技术社区  ›  user4821194

由:java.sql引起。SQLException:字段“customerNumber”没有默认值-hibernate和jpa

  •  0
  • user4821194  · 技术社区  · 8 年前

    我正在编写Spring Hibernate JPA示例。在本例中,我只是尝试将记录插入数据库,但当尝试这样做时,结果如下。为什么?

    org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:310)
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
        at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
        at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
        at com.sun.proxy.$Proxy39.save(Unknown Source)
        at net.javabeat.springdata.CustomerTest.createCustomer(CustomerTest.java:40)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:70)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
    Caused by: org.hibernate.exception.GenericJDBCException: could not execute statement
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:189)
        at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
        at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2987)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3499)
        at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
        at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:393)
        at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:227)
        at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:207)
        at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:191)
        at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:321)
        at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:286)
        at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
        at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
        at org.hibernate.ejb.event.EJB3MergeEventListener.saveWithGeneratedId(EJB3MergeEventListener.java:71)
        at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:236)
        at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:216)
        at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:282)
        at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
        at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
        at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:833)
        at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:817)
        at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:821)
        at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:889)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291)
        at com.sun.proxy.$Proxy31.merge(Unknown Source)
        at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:442)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:483)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:468)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:440)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
        ... 37 more
    Caused by: java.sql.SQLException: Field 'customerNumber' doesn't have a default value
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)
        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)
        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049)
        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034)
        at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
        at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:186)
        ... 82 more
    

    客户.java

    @Entity
    @Table(name="customers")
    @NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
    public class Customer implements Serializable {
        private static final long serialVersionUID = 1L;
    
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int customerNumber;
    
        private String addressLine1;
    
        private String addressLine2;
    
        private String city;
    
        private String contactFirstName;
    
        private String contactLastName;
    
        private String country;
    
        private double creditLimit;
    
        private String customerName;
    
        private String phone;
    
        private String postalCode;
    
        private String state;
    
        //bi-directional many-to-one association to Employee
        @ManyToOne
        @JoinColumn(name="salesRepEmployeeNumber")
        private Employee employee;
    
        //bi-directional many-to-one association to Order
        @OneToMany(mappedBy="customer",fetch = FetchType.EAGER)
        private List<Order> orders;
    
        //bi-directional many-to-one association to Payment
        @OneToMany(mappedBy="customer")
        private List<Payment> payments;
        // setters and getters
    }
    

    雇员.java

    @Entity
    @Table(name="employees")
    @NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e")
    public class Employee implements Serializable {
        private static final long serialVersionUID = 1L;
    
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int employeeNumber;
    
        private String email;
    
        private String extension;
    
        private String firstName;
    
        private String jobTitle;
    
        private String lastName;
    
        //bi-directional many-to-one association to Customer
        @OneToMany(mappedBy="employee")
        private List<Customer> customers;
    
        //bi-directional many-to-one association to Employee
        @ManyToOne
        @JoinColumn(name="reportsTo")
        private Employee employee;
    
        //bi-directional many-to-one association to Employee
        @OneToMany(mappedBy="employee")
        private List<Employee> employees;
    
        //bi-directional many-to-one association to Office
        @ManyToOne
        @JoinColumn(name="officeCode")
        private Office office;
        // setters and getters
    }
    

    弹簧上下文.xml

    <?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:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    
        <!-- For consider the using of annotations foe defining Spring Bean -->
        <context:annotation-config />
    
        <!-- For bootstrapping the Spring Repository -->
        <jpa:repositories base-package="net.javabeat.springdata.repository" />
    
        <!-- Load database.properties file -->
        <context:property-placeholder location="classpath:database.properties" />
    
        <!-- Enable Transaction Manager -->
        <tx:annotation-driven transaction-manager="transactionManager" />
    
        <!-- Necessary to get the entity manager injected into the factory bean -->
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
        <!-- ====== MYSQL DataSource ====== -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${mysql.jdbc.driverClassName}" />
            <property name="url" value="${mysql.jdbc.url}" />
            <property name="username" value="${mysql.jdbc.userName}" />
            <property name="password" value="${mysql.jdbc.password}" />
        </bean>
    
        <!-- ====== Hibernate JPA Vendor Adaptor ======= -->
        <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="${jpa.vendor.adapter.show_sql}"/>
            <property name="generateDdl" value="${jpa.vendor.adapter.generateDdl}"/>
            <property name="database" value="${jpa.vendor.adapter.database}"/>
        </bean>
    
        <!-- ======== Entity Manager factory ======== -->
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <!-- Data Source -->
            <property name="dataSource" ref="dataSource"/>
    
            <!-- JPA Vendor Adaptor -->
            <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
    
            <!-- spring based scanning for entity classes-->
            <property name="packagesToScan" value="net.javabeat.springdata.*" />
    
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <!-- <prop key="hibernate.enable_lazy_load_no_trans">true</prop> -->
                    <!-- <prop key="hibernate.cache.use_query_cache">true</prop> --> 
                </props>
            </property>
        </bean>
    
        <!-- Transaction Manager -->
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    </beans>
    

    客户测试.java

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:SpringContext.xml")
    /*@Transactional*/
    public class CustomerTest {
        @Autowired
        private CustomerRepository customerRepository;
    
    
        @Test
        public void createCustomer(){
            Customer customer = new Customer();
            customer.setCustomerNumber(498);
            customer.setCustomerName("John Kerr");
            customer.setContactLastName("Kerr");
            customer.setContactFirstName("John");
            customer.setPhone("11.22.3333");
            customer.setAddressLine1("500 Street");
            customer.setAddressLine2("Green Tree Hills");
            customer.setCity("London"); 
            customer.setState("United Kingdom");
            customer.setPostalCode("233214");
            customer.setCountry("England");
            customer.setCreditLimit(321312);
    
            customerRepository.save(customer);
        }
    }
    

    随附ER关系图。 enter image description here

    编辑-1

    mysql> desc customers;
    +------------------------+-------------+------+-----+---------+-------+
    | Field                  | Type        | Null | Key | Default | Extra |
    +------------------------+-------------+------+-----+---------+-------+
    | customerNumber         | int(11)     | NO   | PRI | NULL    |       |
    | customerName           | varchar(50) | NO   |     | NULL    |       |
    | contactLastName        | varchar(50) | NO   |     | NULL    |       |
    | contactFirstName       | varchar(50) | NO   |     | NULL    |       |
    | phone                  | varchar(50) | NO   |     | NULL    |       |
    | addressLine1           | varchar(50) | NO   |     | NULL    |       |
    | addressLine2           | varchar(50) | YES  |     | NULL    |       |
    | city                   | varchar(50) | NO   |     | NULL    |       |
    | state                  | varchar(50) | YES  |     | NULL    |       |
    | postalCode             | varchar(15) | YES  |     | NULL    |       |
    | country                | varchar(50) | NO   |     | NULL    |       |
    | salesRepEmployeeNumber | int(11)     | YES  | MUL | NULL    |       |
    | creditLimit            | double      | YES  |     | NULL    |       |
    +------------------------+-------------+------+-----+---------+-------+
    
    mysql> desc employees;
    +----------------+--------------+------+-----+---------+-------+
    | Field          | Type         | Null | Key | Default | Extra |
    +----------------+--------------+------+-----+---------+-------+
    | employeeNumber | int(11)      | NO   | PRI | NULL    |       |
    | lastName       | varchar(50)  | NO   |     | NULL    |       |
    | firstName      | varchar(50)  | NO   |     | NULL    |       |
    | extension      | varchar(10)  | NO   |     | NULL    |       |
    | email          | varchar(100) | NO   |     | NULL    |       |
    | officeCode     | varchar(10)  | NO   | MUL | NULL    |       |
    | reportsTo      | int(11)      | YES  | MUL | NULL    |       |
    | jobTitle       | varchar(50)  | NO   |     | NULL    |       |
    +----------------+--------------+------+-----+---------+-------+
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   rapasoft    8 年前

    在这种情况下,DDL会更有用,但消息非常清楚-您正在尝试插入行,但没有生成id(即使您正在使用 @GeneratedValue 设置为 AUTO 看见 documentation ).

    您是否正在尝试映射到现有数据库架构?如果是,请检查上述字段是否设置为支持MySQL AUTO_INCREMENT .

        2
  •  0
  •   gurubelli    8 年前

    在您的测试中,您正在设置客户编号。

    Customer customer = new Customer(); customer.setCustomerNumber(498);

    当您将GenerationType标记为Auto时,我认为您不应该设置客户编号。