Hibernate:从可调用的自定义插入接收生成的ID
我有一个实体类Hibernate:从可调用的自定义插入接收生成的ID,hibernate,stored-procedures,identity,Hibernate,Stored Procedures,Identity,我有一个实体类 @Entity @SQLInsert(callable = true, sql = "{ call sp_InsertEntity(?, ?, ?) }") public class Entity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "entity_id") private int entityId; ... } sp_InsertEntity执行标准插
@Entity
@SQLInsert(callable = true, sql = "{ call sp_InsertEntity(?, ?, ?) }")
public class Entity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "entity_id")
private int entityId;
...
}
sp_InsertEntity执行标准插入并返回@@IDENTITY。
但session.save(entity)entityId值在会话后保持为0
如何使用返回值或输出参数或@IDENTITY或SCOPE_IDENTITY()强制Hibernate进行生成的ID初始化?也许可以为遇到相同问题的其他人澄清一下-当您使用
false
时,Hibernate不会在您的过程后自动生成其选择范围_IDENTITY()(这显然首先不起作用,因为select与insert不在同一范围内),因此必须在存储过程中的insert之后手动添加select scope_IDENTITY()
以这种方式编写insert存储过程还将使它们与实体框架处理事情的方式兼容,这是一个很好的优点。我在设置false中找到了解决方案