Announcement Announcement Module
Collapse
No announcement yet.
Save is not performing in Spring Mvc 3 Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Save is not performing in Spring Mvc 3 Hibernate

    Dispatcher Servlet is :

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
    <list>
    <value>net.devmanuals.model.Article</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    </props>
    </property>
    </bean>

    <bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
    <property name="viewClass"
    value="org.springframework.web.servlet.view.JstlVi ew" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    </bean>

    Article Java Class:

    mport 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 Integer imeiNo;
    @Column(name = "date_added")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date addedDate;

    public Article() {
    }
    public Integer getimei1() {
    return imeiNo;
    }

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

    public Date getAddedDate() {
    return addedDate;
    }

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

    Article Controller :

    @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 :
    public interface ArticleService {

    public void addArticle(Article article);

    public List<Article> listArticles();
    }

    Article ServiceImpl:

    @Service("articleService")
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    public ArticleServiceImpl() {
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void addArticle(Article article) {
    articleDao.saveArticle(article);
    }

    public List<Article> listArticles() {
    return articleDao.listArticles();
    }


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

    exception

    org.springframework.web.util.NestedServletExceptio n: 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

  • #2
    From your Entity class:
    @Entity
    @Table(name = "imei")
    public class Article {
    @Id
    @Column(name = "imei1",nullable = false)
    private Integer imeiNo;
    ...
    You have indicate @Id and this means, you MUST need to generate unique ID manually by your application and assign to "artical" instance and then perform the "save()" operation.

    If you indented framework to generate automatically the unique ID for you, then you MUST use the following.

    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)

    Comment


    • #3
      Thanks Sir actually i want to enter values manually id also, and i did it also, it was my fault actually in article class i was writing wrong setter method. I correct it , now works fine,,

      But Please tell me that how to enter value in two tables 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
      Last edited by pankaj369; May 19th, 2013, 12:11 PM.

      Comment


      • #4
        Help me friends..

        Comment

        Working...
        X