-1

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
4

2 回答 2

0

例外说明了一切。在持久化实体之前,您必须为其分配一个 ID。所以只需调用一个imeiNo在调用之前设置值的方法persist()。不幸的是,你没有任何这样的方法。唯一看起来像这样的二传手的是以下一个:

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

但是,它不仅名字不好(它应该被称为setImeiNo),而且它还有一个明显的错误:它完全忽略了它的参数,并将值设置为imeiNo它已经拥有的值。

鉴于您的 getter 和 setter 很混乱,我的建议是:

  • 全部删除
  • 使用您的 IDE 自动重新生成它们

二传手应该是:

public void setImeiNo(Long imeiNo) {
    this.imeiNo = imeiNo;
}
于 2013-05-19T09:08:24.063 回答
0

给你一个模型,它会自动增加,你不应该看到错误,也不需要自己添加 id

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
于 2013-05-19T09:10:46.600 回答