Article
班级:
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
服务:
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();
}
保存数据时出现此错误,(但显示数据时没有错误):
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