列不能在JAVA服务器端具有空值

列不能在JAVA服务器端具有空值,java,mysql,hibernate,mysql-workbench,Java,Mysql,Hibernate,Mysql Workbench,我不熟悉java客户机/服务器编码。 我正在实施REST服务。我的数据库有一个表Customer(名字、姓氏、电话、电子邮件、地址) 我试图从客户端向这个rest类的POST方法发送一个ajax调用 但是,数据库正在引发NULL异常,并给出错误:“Email”列不能为NULL 这就是我在post方法中所做的: @Path("customer") public class CustomerREST { @POST @Produces(MediaType.APPLICATION_JSON) publ

我不熟悉java客户机/服务器编码。 我正在实施REST服务。我的数据库有一个表Customer(名字、姓氏、电话、电子邮件、地址) 我试图从客户端向这个rest类的POST方法发送一个ajax调用

但是,数据库正在引发NULL异常,并给出错误:“Email”列不能为NULL

这就是我在post方法中所做的:

@Path("customer")
public class CustomerREST {
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response addCustomer(@FormParam("FirstName") String fname, @FormParam("LastName") String lname, @FormParam("Phone") String phone, @FormParam("Email") String email, @FormParam("Address") String address)
{
//  , @FormParam("uname") String uname, @FormParam("password") String pwd
    List<Login> L= DataAccessUtil.getAll(Login.class);

    Integer size = L.size();
    Login newLogin = new Login();
    newLogin = L.get(size-1);

    Customer newCustomer = new Customer();

    newCustomer.setAddress("123");
    newCustomer.setEmail("abc");
    newCustomer.setFirstName("abc");
    newCustomer.setLastName("abc");
    newCustomer.setPhone("abc");
    newCustomer.setLogin(newLogin);

    DataAccessUtil.save(newCustomer);

    try {
        return Response.created(new URI("/"+newCustomer.getCustomerId())).build();
    } catch (URISyntaxException exp) {
        return Response.status(Status.BAD_REQUEST).entity(exp).build();
    }
}
}

您正在尝试将值作为
null
插入不允许
null
值的字段中。所讨论的列是
电子邮件
列。空字符串与null不同,因此如果需要插入时不带值,可以将其设置为空字符串。由于您似乎在代码中设置了它,如果您手动执行插入,则很可能会指向Hibernate配置中的某个问题或HQL/SQL中的某个问题。不过,您必须共享更多代码才能真正分辨。

@Funtik我添加了stacktraceFirst,它将其指向“地址”列。我从Address列中删除了NotNULL约束,然后它开始指向Email列。我确信这段代码有问题。你发布的代码看起来不错。创建一个新客户,填充电子邮件,然后使用util类保存它。那里没有什么可搞砸的。我怀疑您的问题可能在您的实体/域对象中,或者,如果您手动插入(不要认为您是,但是,嘿,永远不知道)它可能在那里。
Hibernate: insert into customer (Address, Email, FirstName, LastName, Phone,   
idcustomer) values (?, ?, ?, ?, ?, ?)
Jan 07, 2014 10:03:12 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1048, SQLState: 23000
Jan 07, 2014 10:03:12 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Column 'Email' cannot be null
Jan 07, 2014 10:03:12 PM com.sun.jersey.spi.container.ContainerResponse                                                  mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP  container
org.hibernate.exception.ConstraintViolationException: Column 'Email' cannot be null
at     org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
    at     org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
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.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at com.sun.proxy.$Proxy67.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3028)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3469)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:402)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at mypackage.DataAccessUtil.save(DataAccessUtil.java:13)
at serverside.CustomerREST.addCustomer(CustomerREST.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
   Caused by:   com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'Email'   cannot be null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at     org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 52 more