Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Save is not performing in Spring Mvc 3Hibernate

 
gupta pankaj
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Article class
package net.devmanuals.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;

@Entity
@Table(name = "imei")
public class Article {
@Id
@Column(name = "imei1",nullable = false)
private Long imeiNo;
@Column(name = "date_added")
@Temporal(javax.persistence.TemporalType.DATE)
private Date addedDate;

public Article() {
}

public Long getimei1() {
return imeiNo;
}

public void setimei1(Long imei1) {
this.imeiNo = imeiNo;
}

public Date getAddedDate() {
return addedDate;
}

public void setAddedDate(Date addedDate) {
this.addedDate = addedDate;
}
}




ArticleController:



package net.devmanuals.controller;

import java.util.HashMap;
import java.util.Map;

import net.devmanuals.model.Article;
import net.devmanuals.service.ArticleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/articles")
public class ArticleController {

@Autowired
private ArticleService articleService;

@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveArticle(@ModelAttribute(" article") Article article,
BindingResult result) {
articleService.addArticle( article);
return new ModelAndView("redirect:/articles.html");
}

@RequestMapping(method = RequestMethod.GET)
public ModelAndView listArticles() {

Map<String, Object> model = new HashMap<String, Object>();
model.put("articles", articleService.listArticles());

return new ModelAndView("articlesList", model);
}

@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addArticle(@ModelAttribute("article") Article article,
BindingResult result) {
return new ModelAndView("addArticle");
}

}


Article service:

package net.devmanuals.service;

import java.util.List;

import net.devmanuals.model.Article;

public interface ArticleService {

public void addArticle(Article article);

public List<Article> listArticles();
}


This Error comes when saving data,(But no error while displaying data)

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): net.devmanuals.model.Article
root cause

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): net.devmanuals.model.Article
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is actually a Hibernate issue, not a Spring one.

But your mapping for the PK, id field is set up to be application assigned id. Meaning your code has to assign a new id to a new object.

You might want to add and @Generated…. annotation to your @Id mapping. I recommend reading the Hibernate Annotations documentation on ID mapping.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic