2011-04-05 41 views
0

當我創建一個沒有ID的新實體並將其存儲在數據庫中時,實體將接收除數據庫中標識符以外的標識符。 ID是通過oracle中的序列生成的。任何建議如何解決它?感謝您的回覆。通過堅持在Hibernate中實體的不同標識符

示例:我創建了DiReview review = new DiReview()。我設置正確的所有領域,除了ID我希望通過getSessionFactory()。getCurrentSession()。持久化()審查數據庫,我希望休眠設置正確的ID。假設oracle序列生成的最後一個ID是25,所以我假設表DI_REVIEW中的新行將是26.在表中存儲並提交之後,確實存儲了具有id 26的新行,但是在審閱中設置了字段id另一個數字。在我的情況下,例如2000!這個是正常的?

這個問題在我的案例中不僅涉及到DiReview,而且涉及我所有的實體。當我從數據庫加載實體時,加載了corect id。

編輯 - 我嘗試使用Oracle和序列來實現這個example和至少現在我知道這是不正常的行爲;-)

@Entity 
@Table(name = "DI_REVIEW", uniqueConstraints = @UniqueConstraint(columnNames = { 
     "OBJECT_ID", "USER_ID" })) 
public class DiReview{ 

    private Long id; 
    private DiUser user; 
    private DiObject object; 
    private String text; 
    private Date createDate; 

    private Set<DiRating> ratings = new HashSet<DiRating>(0); 
    private Set<DiReviewContext> reviewContexts = new HashSet<DiReviewContext>(
      0); 

    private Collection<DiReviewContext> reviewContextsList = new ArrayList<DiReviewContext>(); 
    private Set<DiComment> comments = new HashSet<DiComment>(0); 

    public DiReview() { 
    } 

    public DiReview(Long id, DiUser user, DiObject object, String text, 
      Date createDate) { 
     this.id = id; 
     this.user = user; 
     this.object = object; 
     this.text = text; 
     this.createDate = createDate; 
    } 

    @Id 
    @Column(name = "ID", unique = true, nullable = false, precision = 18, scale = 0) 
    @SequenceGenerator(name = "di_review_seq", sequenceName = "DI_REVIEW_SEQ") 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "di_review_seq") 
    public Long getId() { 
     return this.id; 
    } 

    getters and setters .. 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result 
       + ((createDate == null) ? 0 : createDate.hashCode()); 
     result = prime * result + ((id == null) ? 0 : id.hashCode()); 
     result = prime * result + ((object == null) ? 0 : object.hashCode()); 
     result = prime * result + ((text == null) ? 0 : text.hashCode()); 
     result = prime * result + ((user == null) ? 0 : user.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     DiReview other = (DiReview) obj; 
     if (createDate == null) { 
      if (other.createDate != null) 
       return false; 
     } else if (!createDate.equals(other.createDate)) 
      return false; 
     if (id == null) { 
      if (other.id != null) 
       return false; 
     } else if (!id.equals(other.id)) 
      return false; 
     if (object == null) { 
      if (other.object != null) 
       return false; 
     } else if (!object.equals(other.object)) 
      return false; 
     if (text == null) { 
      if (other.text != null) 
       return false; 
     } else if (!text.equals(other.text)) 
      return false; 
     if (user == null) { 
      if (other.user != null) 
       return false; 
     } else if (!user.equals(other.user)) 
      return false; 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "DiReview [id=" + id + ", user=" + user + ", object=" + object 
       + ", text=" + text + ", createDate=" + createDate + "]"; 
    } 

} 

    public T makePersistent(T entity) { 
    try { 

     getSessionFactory().getCurrentSession().persist(entity); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return entity; 
} 
+1

目前還不清楚究竟是什麼你想達到的。 – axtavt 2011-04-06 08:06:05

回答

0

我不知道這是否解決您的問題,而是何時使用序列生成器,默認情況下,Hibernate一次分配一個包含50個ID值的塊,這可能意味着它無法獲取您期望的序列值。您可以通過更改註釋爲@SequenceGenerator如下改變這一點:

@SequenceGenerator(name = "di_review_seq", sequenceName = "DI_REVIEW_SEQ", 
     allocationSize = 1) 
+0

雖然這不是我的問題的解決方案,但它對我有很大的幫助。爲了創建數據庫模式,我使用了Oracle HTML嚮導來創建表格及其序列。但是這個嚮導也創建了增量ID的觸發器。這些觸發器與Hibernatem並不是很好的結合:-)我的實體的id的計算算法比 - (從sequnce最後一個數字)*(分配大小)。這就是這樣一個ID大的數字。解決方案是 - 刪除所有增量觸發器!非常感謝你 – rizler 2011-04-07 22:15:57

相關問題