2016-07-27 45 views
0

我知道這個問題以前可能已經被問過了,但我沒有得到解決我的問題的方法。我有一個varchar(20)categoryID的主鍵列,實體類通過id屬性映射到這個主鍵列。在添加數據時,通過UI輸入一個id值,但在編輯其他字段並通過UI保存時,其拋出異常「IdentifierGenerationException:此類的ids必須在調用save()之前手動分配:」org.hibernate.id.IdentifierGenerationException:在調用save()之前,必須手動分配此類的ids:

請看以下代碼:

  Category.java 
      ------------- 
      package com.niit.cakecuisinebe.model; 

      import java.util.UUID; 

      import javax.persistence.Column; 
      import javax.persistence.Entity; 
      import javax.persistence.Id; 
      import javax.persistence.PrePersist; 
      import javax.persistence.Table; 

      import org.springframework.stereotype.Component; 

     @Entity 
     @Table 
     @Component 
     public class Category { 

      @Id 
      @Column(name="CATEGORYID") 
      private String id; 


      public String getId() { 
       return id; 
      } 
      public void setId(String id) { 
       this.id = id; 
      } 
      public String getName() { 
       return name; 
      } 
      public void setName(String name) { 
       this.name = name; 
      } 
      public String getDescription() { 
       return description; 
      } 
      public void setDescription(String description) { 
       this.description = description; 
      } 
      @Column(name="CATEGORYNAME") 
      private String name; 
      @Column(name="CATEGORYDESCRIPTION") 
      private String description; 
     } 
     CategoryDAOImpl 
     ---------------- 
     package com.niit.cakecuisinebe.dao; 

     import java.util.List; 


     import javax.transaction.Transactional; 

     import org.hibernate.Criteria; 
     import org.hibernate.Query; 
     import org.hibernate.SessionFactory; 
     import org.springframework.beans.factory.annotation.Autowired; 
     import org.springframework.stereotype.Repository; 

     import com.niit.cakecuisinebe.model.Category; 
     @Repository("categoryDAO") 
     public class CategoryDAOImpl implements CategoryDAO{ 

      @Autowired 
      private SessionFactory sessionFactory; 

      public CategoryDAOImpl(SessionFactory sessionFactory) { 
       this.sessionFactory = sessionFactory; 
      } 

      @Transactional 
      public List<Category> list() 
      { 
       //Logger.debug("calling list"); 

       @SuppressWarnings("unchecked") 
       List<Category> listCategory = (List<Category>) 
          sessionFactory.getCurrentSession() 
         .createCriteria(Category.class) 
         .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list(); 
      // Logger.debug("calling list"); 
       return listCategory; 
      } 

      @Transactional 
      public Category get(String id) { 
       // TODO Auto-generated method stub 
       String hql = "from Category where id=" + "'"+ id +"'"; 
       // from category where id = '101' 
       Query query = sessionFactory.getCurrentSession().createQuery(hql); 
       List<Category> listCategory = (List<Category>) query.list(); 

       if (listCategory != null && !listCategory.isEmpty()) { 
        return listCategory.get(0); 
       } 
       return null; 
      } 

      @Transactional 
      public void saveOrUpdate(Category category) { 
       // TODO Auto-generated method stub 
       sessionFactory.getCurrentSession().saveOrUpdate(category); 
      } 
      @Transactional 
      public void delete(String id) { 
       // TODO Auto-generated method stub 
       Category category = new Category(); 
       category.setId(id); 
       sessionFactory.getCurrentSession().delete(category); 
      } 

      @Transactional 
      public Category getByName(String name) { 
       // TODO Auto-generated method stub 
       String hql = "from Category where name=" + "'"+ name +"'"; 
       Query query = sessionFactory.getCurrentSession().createQuery(hql); 

       @SuppressWarnings("unchecked") 
       List<Category> listCategory = (List<Category>) query.list(); 

       if (listCategory != null && !listCategory.isEmpty()) { 
        return listCategory.get(0); 
       } 

       return null; 

      } 

     } 
     CategoryController 
     -------------------- 
     package com.niit; 

     import java.util.List; 
     import java.util.Map; 

     import javax.servlet.http.HttpServletRequest; 
     import javax.servlet.http.HttpServletResponse; 

     import org.apache.log4j.Logger; 
     import org.springframework.beans.factory.annotation.Autowired; 
     import org.springframework.stereotype.Controller; 
     import org.springframework.ui.Model; 
     import org.springframework.ui.ModelMap; 
     import org.springframework.web.bind.annotation.ModelAttribute; 
     import org.springframework.web.bind.annotation.PathVariable; 
     import org.springframework.web.bind.annotation.RequestMapping; 
     import org.springframework.web.bind.annotation.RequestMethod; 

     import com.niit.cakecuisinebe.dao.CategoryDAO; 
     import com.niit.cakecuisinebe.model.*; 


     @Controller 
     public class CategoryController { 
      protected static Logger logger = Logger.getLogger("CategoryController"); 
      @Autowired 
      private CategoryDAO categoryDao; 

      @RequestMapping(value = "/ViewCategory", method = RequestMethod.GET) 
      public String getCategory(Model model) { 

       logger.info("entering showAllGreetings"); 
       model.addAttribute("category", new Category()); 

       List<Category> categories = categoryDao.list(); 
       if (!categories.isEmpty()) { 

        model.addAttribute("categorylist", categories); 
       } 

       return "Category"; 
      } 

      @RequestMapping(value = "/addCategory", method = RequestMethod.POST) 
      public String addCategory(@ModelAttribute("category") Category category) { 
       logger.info("entering showAllGreetings"); 

       categoryDao.saveOrUpdate(category); 

       return "redirect:/ViewCategory"; 

      } 

      @RequestMapping(value = "edit/addCategory", method = RequestMethod.POST) 
      public String editCategory(@ModelAttribute("category") Category category) { 
       logger.info("entering showAllGreetings"); 

       String id=category.getId(); 
       category.setId(id); 
       categoryDao.saveOrUpdate(category); 

       return "redirect:/ViewCategory"; 

      } 

      @RequestMapping(value = "delete/{id}", method = RequestMethod.GET) 
      public String deleteCategory(@PathVariable("id") String id, ModelMap model) { 
       logger.info("entering showAllGreetings"); 

       categoryDao.delete(id); 
       model.addAttribute("{msg}", "Successfully Deleted"); 
       return "redirect:/ViewCategory"; 
      } 

      @RequestMapping(value = "edit/{id}", method = RequestMethod.GET) 
      public String showEditCategory(@PathVariable("id") String id, Model model) { 
       logger.info("entering showAllGreetings"); 

       model.addAttribute("category", this.categoryDao.get(id)); 
       model.addAttribute("categorylist", categoryDao.list()); 

       return "Category"; 
      } 
     } 
     <div > 
     <spring:form method="POST" action="addCategory" modelAttribute="category" align="center"> 
     <table cellpadding="5" cellspacing="5" style="height=45px; width=45px; background-color: pink; padding=10px"> 
      <tr> 

       <c:choose> 
       <c:when test="${!empty category.id}"> 
        <td><spring:label path="id"><springtags:message text="CategoryID"></springtags:message></spring:label></td> 
        <td><spring:input path="id" disabled="true" readonly="true" /></td> 
       </c:when> 
       <c:otherwise> 
        <td><spring:label path="id"><springtags:message text="CategoryID"></springtags:message></spring:label></td> 
        <td><spring:input path="id" /></td> 
       </c:otherwise> 
       </c:choose> 
      </tr> 
      <tr> 
       <td> Category Name:</td> 
       <td><spring:input path="name" /></td> 
      </tr> 
      <tr> 
       <td> Category Description:</td> 
       <td><spring:input path="description" /></td> 
      </tr> 



      <tr> 

       <c:if test="${!empty category.id}"> 
          <td> <input type="submit" 
            value="<springtags:message text="Edit Category"/>" /> 
          </c:if> 
          </td> 
          <td><c:if test="${empty category.id}"> 
           <input type="submit" value="<springtags:message text="Add Category"/>" /> 
          </c:if> 
          </td> 
      </tr> 


     </table> 
     </spring:form> 
      </div> 

      <div class="container"> 
      <table class="table table-striped table-bordered table-hover table-condensed"> 
      <caption><h2>Categories</h2></caption> 
      <thead> 
       <tr> 
       <th>Category ID</th> 
       <th>Category Name</th> 
       <th>Category Description</th> 
       <th>Edit</th> 
       <th>Delete</th> 
       </tr> 
       </thead> 
       <c:if test="${not empty categorylist}" > 
       <tbody> 
        <c:forEach items="${categorylist}" var="category">  
        <tr> 
        <td>${category.id}</td> 
        <td>${category.name}</td> 
        <td>${category.description} </td> 
        <td><a href="edit/${category.id}">Edit</a> </td> 
        <td><a href="delete/${category.id}">Delete</a> </td> 
        </tr>  
        </c:forEach> 
       </tbody>  
     </c:if> 
     <c:if test="${empty categorylist}" > 
      There are no category yet. 
     </c:if> 
       </tr> 


     </table> 
     </div> 
     <%@ include file="footer.jsp"%> 
     </body> 
     </html> 

請幫助套牢此爲最近兩天

+1

'System.out.println(category.getId());'in Controller - 輸出是什麼? –

+0

試圖檢查ID是否被提取,但它是空的我是新的春季和冬眠,我無法完成它請幫助 –

+0

這是控制器的方法,給出了例外? * editCategory?* * addCategory *工作正常? – amicoderozer

回答

1

我沒有ID的字符串的類型的大風扇。你應該改變一個長ID。因爲它,你可以委託生成的id到數據庫與一些排序的東西。

如果您希望實際colomn中的數據是唯一的,只需在其中創建具有唯一約束的其他字段。

相關問題