gpt4 book ai didi

java - 为什么当我添加productimage代码和 "productname"ProductNAME时不支持 "NULL not allowed for column "; SQL语句:"

转载 作者:行者123 更新时间:2023-12-02 11:56:46 25 4
gpt4 key购买 nike

   i am not add any null in "productName" but error   org.hibernate.exception.ConstraintViolationException: NULL not allowed for column "PRODUCTNAME"; SQL statement

我的错误 null not allowed Productname 那么我该如何解决这个问题 HTTP Status 500 - 请求处理失败;嵌套异常是 org.hibernate.exception.ConstraintViolationException:列“ProductNAME”不允许为 NULL; SQL语句:

    org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions SQL Error: 23502, SQLState: 23502

org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions NULL not allowed for column "PRODUCTNAME"; SQL statement

这是完整的错误:

   type: Exception report

message Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: NULL not allowed for column "PRODUCTNAME"; SQL statement:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: NULL not allowed for column "PRODUCTNAME"; SQL statement:
insert into Product (productId, productCategory, productCondition, productDescription, productManufacturer, productName, productPrice, productStatus, unitInStock) values (null, ?, ?, ?, ?, ?, ?, ?, ?) [23502-196]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.hibernate.exception.ConstraintViolationException: NULL not allowed for column "PRODUCTNAME"; SQL statement:
insert into Product (productId, productCategory, productCondition, productDescription, productManufacturer, productName, productPrice, productStatus, unitInStock) values (null, ?, ?, ?, ?, ?, ?, ?, ?) [23502-196]
org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:128)
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
com.sun.proxy.$Proxy47.executeUpdate(Unknown Source)
org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:55)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2757)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3268)
org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:78)
org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272)
org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:304)
org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:128)
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:615)
org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:608)
org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:604)
com.emusicstore.dao.impl.ProductDaoImpl.addProduct(ProductDaoImpl.java:25)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)`enter code here`
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy42.addProduct(Unknown Source)
com.emusicstore.controller.HomeController.addProductPost(HomeController.java:84)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.h2.jdbc.JdbcSQLException: NULL not allowed for column "PRODUCTNAME"; SQL statement:
insert into Product (productId, productCategory, productCondition, productDescription, productManufacturer, productName, productPrice, productStatus, unitInStock) values (null, ?, ?, ?, ?, ?, ?, ?, ?) [23502-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.table.Column.validateConvertUpdateSequence(Column.java:345)
at org.h2.table.Table.validateConvertUpdateSequence(Table.java:797)
at org.h2.command.dml.Insert.insertRows(Insert.java:151)
at org.h2.command.dml.Insert.update(Insert.java:114)
at org.h2.command.CommandContainer.update(CommandContainer.java:101)
at org.h2.command.Command.executeUpdate(Command.java:260)
at org.h2.server.TcpServerThread.process(TcpServerThread.java:354)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:158)
at java.lang.Thread.run(Unknown Source)

org.h2.engine.SessionRemote.done(SessionRemote.java:629)
org.h2.command.CommandRemote.executeUpdate(CommandRemote.java:209)
org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:164)
org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:150)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
com.sun.proxy.$Proxy47.executeUpdate(Unknown Source)
org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:55)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2757)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3268)
org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:78)
org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272)
org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:304)
org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:128)
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:615)
org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:608)
org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:604)
com.emusicstore.dao.impl.ProductDaoImpl.addProduct(ProductDaoImpl.java:25)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy42.addProduct(Unknown Source)
com.emusicstore.controller.HomeController.addProductPost(HomeController.java:84)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.28 logs.

我的添加产品页面:

这是添加产品页面代码

    <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@include file="/WEB-INF/views/template/header.jsp"%>


<div class="container-wrapper">
<div class="container">
<div class="page-header">
<h1>Add Product</h1>
<p class="lead">Fill the below information to a product: </p>
</div>


<form:form action="${pageContext.request.contextPath}/admin/productInventory/addProduct" method="post" commandName="product" enctype="multipart/form-data">

<div class="form-group">
<label for="name">Name</label>
<form:input path="productName" id="name" class="form-Control"/>
</div>

<div class="form-group">
<label for="category">Category</label>
<label class="checkbox-inline"><form:radiobutton path="productCategory" id="category" value="instrument"/>Instrument</label>
<label class="checkbox-inline"><form:radiobutton path="productCategory" id="category" value="record"/>Record</label>
<label class="checkbox-inline"><form:radiobutton path="productCategory" id="category" value="accessory"/>Accessory</label>

</div>

<div class="form-group">
<label for="description">Description</label>
<form:textarea path="productDescription" id="description" class="form-Control"/>
</div>

<div class="form-group">
<label for="price">Price</label>
<form:input path="productPrice" id="price" class="form-Control"/>
</div>

<div class="form-group">
<label for="condition">Condition</label>
<label class="checkbox-inline"><form:radiobutton path="productCondition" id="condition" value="new"/>New</label>
<label class="checkbox-inline"><form:radiobutton path="productCondition" id="condition" value="used"/>Used</label>

</div>



<div class="form-group">
<label for="status">Status</label>
<label class="checkbox-inline"><form:radiobutton path="productStatus" id="status" value="active"/>Active</label>
<label class="checkbox-inline"><form:radiobutton path="productStatus" id="status" value="inactive"/>Inactive</label>

</div>

<div class="form-group">
<label for="unitInstock">Unit In stock</label>
<form:input path="unitInStock" id="unitInstock" class="form-Control"/>
</div>

<div class="form-group">
<label for="manufacturer">Manufacturer</label>
<form:input path="productManufacturer" id="manufacturer" class="form-Control"/>
</div>

<div class="form-group">
<label class="control-label" for="productImage">Upload Picture</label>
<form:input id="productImage" path="productImage" type="file" class="form:input-large"/>

</div>

<br><br>

<input type="submit" value="submit" class="btn btn-success">
<a href="<c:url value="/admin/productInventory"/> " class="btn btn-default">Cancle</a>



</form:form>
<%@include file="/WEB-INF/views/template/footer.jsp"%>

我的 Controller 映射:

这是产品图像代码

    //productimage code
@RequestMapping(value = "/admin/productInventory/addProduct" , method = RequestMethod.POST)
public String addProductPost(@ModelAttribute("product") Product product, HttpServletRequest request){

productDao.addProduct(product);

MultipartFile productImage = product.getProductImage();
String rootDirectory = request.getSession().getServletContext().getRealPath("/");
path = Paths.get(rootDirectory + "\\WEB-INF\\resources\\images\\"+product.getProductId()+".png");

if (productImage !=null && !productImage.isEmpty())
{
try{
productImage.transferTo(new File(path.toString()));
}catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException("Product image saving failed",e);

}
}

return "redirect:/admin/productInventory";

}
productimage//

我的模型Product.java

    this is add our product column

package com.emusicstore.model;

import org.springframework.web.multipart.MultipartFile;

import javax.persistence.*;

@Entity

public class Product {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)

private String productId;
private String productName;
private String productCategory;
private String productDescription;
private double productPrice;
private String productCondition;
private String productStatus;
private int unitInStock;
private String productManufacturer;

@Transient//automatic create productImage in database
private MultipartFile productImage;

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}



public String getProductName() {
return productName;
}

public void setProductName(String productName) {
this.productName = productName;
}

public String getProductCategory() {
return productCategory;
}

public void setProductCategory(String productCategory) {
this.productCategory = productCategory;
}

public String getProductDescription() {
return productDescription;
}

public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}

public double getProductPrice() {
return productPrice;
}

public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}

public String getProductCondition() {
return productCondition;
}

public void setProductCondition(String productCondition) {
this.productCondition = productCondition;
}

public String getProductStatus() {
return productStatus;
}

public void setProductStatus(String productStatus) {
this.productStatus = productStatus;
}

public int getUnitInStock() {
return unitInStock;
}

public void setUnitInStock(int unitInStock) {
this.unitInStock = unitInStock;
}

public String getProductManufacturer() {
return productManufacturer;
}

public void setProductManufacturer(String productManufacturer) {
this.productManufacturer = productManufacturer;
}


public MultipartFile getProductImage() {
return productImage;
}

public void setProductImage(MultipartFile productImage) {
this.productImage = productImage;
}
}

我的产品daoimpl:

    package com.emusicstore.dao.impl;

import com.emusicstore.dao.ProductDao;
import com.emusicstore.model.Product;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Repository
@Transactional
public class ProductDaoImpl implements ProductDao {


@Autowired
private SessionFactory sessionFactory;

public void addProduct(Product product){
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(product);
session.flush();
}

public Product getProductById(String id){
Session session = sessionFactory.getCurrentSession();
Product product = (Product) session.get(Product.class,id);
session.flush();
return product;
}

public List<Product> getAllProducts(){
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from Product");
List<Product> products= query.list();
session.flush();

return products;
}

public void deleteProduct(String id){
Session session = sessionFactory.getCurrentSession();
session.delete(getProductById(id));
session.flush();


}
}

pom.xml for image
-----------------

这是我们的图像 pom.xml

  <dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

实际上,错误是什么,导致我无法将文件插入数据库,请帮助我

最佳答案

您要保存的产品的 PRODUCTNAME 属性值为空。在调用函数saveOrUpdate之前确保它不为空从数据库中的表中删除not null约束。

关于java - 为什么当我添加productimage代码和 "productname"ProductNAME时不支持 "NULL not allowed for column "; SQL语句:",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47556746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com