2016-04-05 3 views
0

Я новичок в Spring и Hibernate, и я разрабатываю систему управления электронной коммерцией, и моя проблема заключается в загрузке файлов в DB с Hibernate. У меня есть служба, но я вижу, что она не работает.org.hibernate.exception.DataException: не удалось выполнить оператор

вот мой код

домен

@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; 
} 
} 

дао слой, где я сохранить объект, поле которой Набор 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(); 
} 


} 

и по крайней мере, контроллер

@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"; 
} 
} 

и когда на 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 

домен 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; 
} 
+0

Ваша проблема заключается в данных, которые вы передаете в БД. Покажите нам, что вы пытаетесь сохранить, потому что некоторая информация отсутствует, скорее всего, в отношении UploadedFile. – dambros

+0

добавлено свойство UploadFiles –

ответ

3

Для тех, кто еще спотыкаясь на это - он должен делать с данными, вы пытаетесь insert - для меня он пытался вставить строку, длина которой была больше, чем размер, заданный в аннотации Hibernate. При отладке вы можете следить за причиной исключения, чтобы найти основную причину.

Смежные вопросы