java org.hibernate.exception.DataException: 无法执行语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36435057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:26:51  来源:igfitidea点击:

org.hibernate.exception.DataException: could not execute statement

javaspringhibernatefile-upload

提问by O.Kuz

I am new in Spring and Hibernate and i'm develop E-Commerce management system and my trouble is in the uploading files into DB with Hibernate. I have some service but that i see it doesn't work.

我是 Spring 和 Hibernate 的新手,我正在开发电子商务管理系统,我的麻烦在于使用 Hibernate 将文件上传到数据库中。我有一些服务,但我发现它不起作用。

here is my code

这是我的代码

domain

领域

@Entity
@Table(name = "DEPUTES_APPEAL")
public class DeputesAppeal implements Serializable {

@Id
@Column(name = "ID")
@GeneratedValue
private long id;

@Column(name = "NumberOfAppeal")
private Integer number;

@Column(name = "DateOfIncomingAppeal")
private Date IncomingDate;

@Column(name = "NameOfDepute")
private String NameOfDepute;

@Column(name = "ResolutionOfChief")
private String ResolutionOfChief;

@Column(name = "TypeOfAppeal")
private String typeOfAppeal;

@OneToMany(mappedBy = "deputesAppeal", cascade =                                                                           
CascadeType.ALL, fetch= FetchType.EAGER)

private final Set<UploadFiles> fileUpload = new HashSet<UploadFiles>           
();

public DeputesAppeal(){}

public DeputesAppeal(int id, int number, Date incomingDate, String   
nameOfDepute, String resolutionOfChief) {
    this.id = id;
    this.number = number;
    this.IncomingDate = incomingDate;
    this.NameOfDepute = nameOfDepute;
    this.ResolutionOfChief = resolutionOfChief;
}


public long getId() {
    return id;
}

public Integer getNumber() {
    return number;
}

public void setNumber(Integer number) {
    this.number = number;
}

public Date getIncomingDate() {
    return IncomingDate;
}

public void setIncomingDate(Date incomingDate) {
    IncomingDate = incomingDate;
}

public String getNameOfDepute() {
    return NameOfDepute;
}

public void setNameOfDepute(String nameOfDepute) {
    NameOfDepute = nameOfDepute;
}

public String getResolutionOfChief() {
    return ResolutionOfChief;
}

public void setResolutionOfChief(String resolutionOfChief) {
    ResolutionOfChief = resolutionOfChief;
}

public String getTypeOfAppeal() {
    return typeOfAppeal;
}

public void setTypeOfAppeal(String typeOfAppeal) {
    this.typeOfAppeal = typeOfAppeal;
}

public Set<UploadFiles> getFileUpload() {
    return fileUpload;
}
}  

dao layer where i am save object whose field is Set of UploadFiles

我保存对象的 dao 层,其字段是 UploadFiles 集

public class MysqlImpl implements DeputesAppealDao{

@Autowired
SessionFactory sessionFactory;

public List<DeputesAppeal> getAll() {
    Query query = sessionFactory.getCurrentSession().createQuery("from  
DeputesAppeal");
    return query.list();
}

public DeputesAppeal getById(Integer id) {
    DeputesAppeal deputesAppeal = (DeputesAppeal)   
sessionFactory.getCurrentSession().get(DeputesAppeal.class, id);
    return deputesAppeal;
}

public void addAppeal(DeputesAppeal deputesAppeal,   
CommonsMultipartFile[] fileUpload) {
    if (fileUpload != null && fileUpload.length > 0) {
        for (CommonsMultipartFile aFile : fileUpload) {
            UploadFiles uploadFiles = new UploadFiles();
            uploadFiles.setFileName(aFile.getOriginalFilename());
            uploadFiles.setData(aFile.getBytes());
            deputesAppeal.addFile(uploadFiles);
            sessionFactory.getCurrentSession().save(deputesAppeal);
        }
    }
}

public void deleteAppeal(Integer id) {
     DeputesAppeal deputesAppeal = (DeputesAppeal)  
sessionFactory.getCurrentSession().get(DeputesAppeal.class, id);
     sessionFactory.getCurrentSession().delete(deputesAppeal);
}

public void editAppeal(DeputesAppeal deputesAppeal, DeputesAppeal  
existingDeputesAppeal) {
     sessionFactory.getCurrentSession().save(existingDeputesAppeal);
}

public DeputesAppeal modSession(DeputesAppeal deputesAppeal) {
    DeputesAppeal deputesAppeal1 = (DeputesAppeal) 
sessionFactory.getCurrentSession().get(DeputesAppeal.class, 
deputesAppeal.getId());
    return  deputesAppeal1;
}


public List<DeputesAppeal> abstractSearch(String searchingChar) {
    Query query = sessionFactory.getCurrentSession().createQuery("from     
DeputesAppeal where id = " + searchingChar);
    return query.list();
}


}

and at least is Controller

至少是控制器

@Controller
@RequestMapping(value = "/main")
public class MainController {

@Qualifier("deputesAppealServiceBean")
@Autowired
DeputesAppealService deputesAppealService;


@RequestMapping(value = "/mainFrame", method = RequestMethod.GET)
public String getMainPage(){
    return "mainPage";
}

@RequestMapping(value = "/resultOfSearching", method =   
RequestMethod.GET)
public String getSearchResult(Model model, 
@ModelAttribute("searchChar")String searchResult){
    List<DeputesAppeal> deputesAppeals =  
deputesAppealService.abstractSearch(searchResult);
    model.addAttribute("ListOfAppeals", deputesAppeals);
    return "searchingResultPage";
}

@RequestMapping(value = "mainFrame/new", method = RequestMethod.GET)
public String getAddNewAppealPage(){
    return "addPage";
}


@RequestMapping(value = "mainFrame/new", method = RequestMethod.POST)
public String addNewAppeal(@ModelAttribute("Appeal")DeputesAppeal  
deputesAppeal,

@RequestParam("fileUpload")CommonsMultipartFile[] fileUpload){
    deputesAppealService.add(deputesAppeal, fileUpload);
    return "mainPage";
}

@RequestMapping(value = "mainFrame/deleted", method =  
RequestMethod.GET)
public String deleteAppeal(@RequestParam(value = "id", required = 
true) Integer id, Model model){
    deputesAppealService.delete(id);
    model.addAttribute("id", id);
    return "deletedPage";
}

@RequestMapping(value = "mainFrame/editPage", method =    
RequestMethod.GET)
public String GetEdit(@RequestParam(value = "id", required = true) 
Integer id, Model model){
    model.addAttribute("editedAppeal", 
deputesAppealService.getById(id));
    return "editPage";
}

@RequestMapping(value = "mainFrame/editPage", method =  
RequestMethod.POST)
public String editCurrentAppeal(@ModelAttribute("userAttribute") 
DeputesAppeal deputesAppeal,@RequestParam(value = "id", required =   
true)Integer id, Model model) {
    deputesAppeal.setNumber(id);
    deputesAppealService.edit(deputesAppeal);
    model.addAttribute("id", id);
    return "editedPage";
}
}

and when on JSP page i submit input data i handle the next errors

当我在 JSP 页面上提交输入数据时,我会处理下一个错误

HTTP Status 500 - Request processing failed; nested exception is       
org.springframework.dao.DataIntegrityViolationException: could not 
execute statement; SQL [n/a]; nested exception is 
org.hibernate.exception.DataException: could not execute statement

domain of UploadFiles

UploadFiles 的域

@Entity
@Table(name = "UploadFiles")
public class UploadFiles implements Serializable {

@Id
@GeneratedValue
@Column(name = "FILE_ID")
private long id;

@Column(name = "FILE_NAME")
private String fileName;

@Column(name = "FILE_DATA")
private byte[] data;

@ManyToOne
@JoinColumn(name = "ID")
private DeputesAppeal deputesAppeal;

public UploadFiles(){}

public UploadFiles(long id, String fileName, byte[] data){
    this.id = id;
    this.fileName = fileName;
    this.data = data;
}


public long getId() {
    return id;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public byte[] getData() {
    return data;
}

public void setData(byte[] data) {
    this.data = data;
}

public DeputesAppeal getDeputesAppeal() {
    return deputesAppeal;
}

public void setDeputesAppeal(DeputesAppeal deputesAppeal) {
    this.deputesAppeal = deputesAppeal;
}

回答by Geoff Lentsch

For anyone else stumbling on this - it has to do with the data you are trying to insert - for me it was attempting to insert a String that was longer than the size set in the Hibernate annotations. In debugging, you can follow the cause of the Exception to find the root cause.

对于任何在这方面绊倒的人 - 它与您尝试插入的数据有关 - 对我来说,它试图插入一个比 Hibernate 注释中设置的大小更长的字符串。在调试时,可以根据异常的原因来查找根本原因。

回答by Rorschach

I got this error because I was saving too much data in varchar. I changed it to type text and increased char limit to 65535.

我收到此错误是因为我在 varchar 中保存了太多数据。我将其更改为键入文本并将字符限制增加到 65535。