当我创建一个没有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;
}
目前还不清楚究竟是什么你想达到的。 – axtavt 2011-04-06 08:06:05