我在 session.save() 中得到了一个奇怪的结果。它总是返回 1。但数据库中的值已正确插入。请提出您发现的任何疑问
代码
session = getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
userId = (Long)session.save(user);
transaction.commit();
用户 - 持久性
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "USERNAME", length = 100)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "PASSWORD", length = 100)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@OneToOne(fetch = FetchType.EAGER)
@MapsId
@JoinColumn(name = "ROLEID")
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
@Column(name = "IMAGEURL", length = 2000)
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
版本
hibernate :4.3.7 最终版本
Spring :4.1.2
最佳答案
提到这个问题Difference between @GeneratedValue and @GenericGenerator .
问题可能出在您的 @GeneratedValue
上。
所以你可以改变
@GeneratedValue(strategy=GenerationType.SEQUENCE)
到
@GeneratedValue(generator="increment")
注意:可以引用 this 将您的数据库序列分配给@GeneratedValue
关于java - Hibernate session.save() 总是返回 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27524416/