我在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;
}
版
休眠:4.3.7最终版
春季: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/