2016-10-04 41 views
0

SEVERE:servlet [appServlet]在上下文與路徑[]的Servlet.service()拋出異常[請求處理失敗; 嵌套的異常是org.hibernate.id.IdentifierGenerationException: 必須在調用帶有根本原因的save():com.cihangir.model.Book之前手動分配此類的id。org.hibernate.id.IdentifierGenerationException:此ids for this類必須調用save()之前手動分配: com.cihangir.model.Book如何使用字符串作爲主鍵與註釋在休眠

package com.cihangir.model; 

import javax.persistence.Basic; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 

@Entity 
public class Book { 



    @Id 
    @Column(name = "ISBN", nullable = false, unique=true) 
    @Basic(optional=false) 
    private String ISBN; 
    private String bookTitle; 
    private String category; 
    private String author; 

    public Book() { 

    } 

    public Book(String ISBN, String bookTitle, String category, String author) { 
     this.ISBN=ISBN; 
     this.bookTitle = bookTitle; 
     this.category = category; 
     this.author = author; 
    } 

    public String getISBN() { 
     return ISBN; 
    } 

    public void setISBN(String iSBN) { 
     ISBN = iSBN; 
    } 

    public String getBookTitle() { 
     return bookTitle; 
    } 

    public void setBookTitle(String bookTitle) { 
     this.bookTitle = bookTitle; 
    } 

    public String getCategory() { 
     return category; 
    } 

    public void setCategory(String category) { 
     this.category = category; 
    } 

    public String getAuthor() { 
     return author; 
    } 

    public void setAuthor(String author) { 
     this.author = author; 
    } 

    } 

如何解決它,你能幫我嗎?

回答

0

使用String ISBN的以下代碼作爲主鍵。

@Id 
@GeneratedValue(generator="system-uuid") 
@GenericGenerator(name="system-uuid", strategy = "uuid") 
@Column(name = "ISBN") 
private String ISBN; 

OR

您可以從Java中UUID像下面的代碼:

UUID.randomUUID().toString();

+0

感謝answering.It works.Fine :) – kafkas