-1

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 class:

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
tereško
  • 58,060
  • 25
  • 98
  • 150
pankaj369
  • 119
  • 1
  • 4
  • 14

2 Answers2

0

The exception says it all. You must assign an ID to your entity before persisting it. So just call a method that sets the value of imeiNo before calling persist(). Unfortunately, you don't have any such method. The only one that looks like such a setter is the following one:

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

But, not only is it badly named (it should be called setImeiNo), but it also has an obvious bug: it completely ignores its argument, and sets the value of imeiNo to the value it already has.

Given the mess that your getters and setters are, my advice would be:

  • remove them all
  • use your IDE to regenerate them automatically

The setter should be:

public void setImeiNo(Long imeiNo) {
    this.imeiNo = imeiNo;
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Ok Thanks too all and i will try to do that you all have told then reply you,, thanksss – pankaj369 May 19 '13 at 10:23
  • Thanks to all , actually it was my mistake,, you are right sir @JB Nizet .my error was public void setimei1(Long imei1) { this.imeiNo = imeiNo; } – pankaj369 May 19 '13 at 10:28
  • But Sir i want to display the time also when the date display. In my database Date and time is shown but not in Browser, Please tell me how to get time also? – pankaj369 May 19 '13 at 10:47
  • If you want to have date and time, don't you think TemporalType.TIMESTAMP would be more appropriate than TemporalType.DATE? – JB Nizet May 19 '13 at 16:25
  • Sir can you tell me that how to enter value in two database at one time?? because i have to enter imei number in one table and imei and date and time in second table. i have perform working of second table but i didnt know how to enter values at one time in two tables. Pleae help if you can – pankaj369 May 19 '13 at 16:55
  • Sorry it was a mistake, i am talking about two tables. i will edit it. ok sir – pankaj369 May 19 '13 at 17:00
  • And i will enter new Question also.. Thankss Sir – pankaj369 May 19 '13 at 17:08
0

put to you model an it that is to be autoincremented automatically and you should not see the error and will not need to add the id yourself

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
MatthiasLaug
  • 2,924
  • 6
  • 28
  • 43