【发布时间】:2017-03-11 09:57:02
【问题描述】:
我意识到需要为主键不是 GeneratedValue 的实体调用 session.flush(),然后才能将它们持久化到数据库中。即使我已经将自动提交设置为 true,我也必须这样做。 这是我的休眠配置
<property name="hibernate.dialect">${hibernate.dialect}</property>
<property name="hibernate.connection.driver_class">${hibernate.connection.driver_class}</property>
<property name="hibernate.connection.url">${hibernate.connection.url}</property>
<property name="hibernate.connection.username">${hibernate.connection.username}</property>
<property name="hibernate.connection.password">${hibernate.connection.password}</property>
<property name="connection.autocommit">true</property>
这是一个实体示例,我需要在保存后在代码中调用 session.flush() 之后才能持久化
@Id
@Column(name = "MembTypeCode")
private String memTypeCode;
@Column(name = "MemberType")
private String memberType;
...
但是对于 Id 为 GeneratedValue 的实体(如下面的实体),我不需要在保存后在代码中调用 session.flush() 以使其持久保存在数据库中,因为我已将自动提交设置为 true .
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PrescriptId")
private int prescriptId;
@Column(name = "InvNum")
private Integer invNum;
@Column(name = "DocType")
private String docType;
...
有人可以向我解释为什么我会遇到这种情况吗?我正在使用 Hibernate 4.1.0.FINAL
【问题讨论】:
标签: java sql-server hibernate session