I have an entity which contains a Map:
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "matrix_columns", joinColumns = Array(new JoinColumn(name = "id")))
@MapKey(name="tenor")
@Fetch(value = FetchMode.SELECT)
@Access(AccessType.PROPERTY)
def getInputMapNative: java.util.Map[Int,Column] = inputMap
并且列包含一个数组
@(OneToMany @field)(cascade = Array(CascadeType.ALL))
@OrderColumn(name = "input_index")
@Fetch(value = FetchMode.SELECT)
var inputs:Array[Input] = _
因为我被 Hibernate 3.6 困住了,并且这个错误Failed to lazily initialize a collection, no session or session is closed (尽管热切地获取),我试图实现以下解决方法:
- 我合并并保存列
- 我更新地图并合并并保存父实体
(因为我不能依赖 Hibernate 级联)
但是,我仍然遇到一个我还没有准备好解决的异常
09:41:26.160 [GS-Notifier-pool-6-thread-2] ERROR o.h.util.JDBCExceptionReporter - Duplicate entry '781' for key 'inputs_id'
09:41:26.167 [GS-Notifier-pool-6-thread-2] ERROR o.h.e.d.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert collection: [Column.inputs#69]
这是我想用于在数据库中创建或保存新矩阵的代码
val mergedColumns = matrix.getInputMap.map {
case(tenor,column) =>
column.inputs foreach{
item =>
val mergedItem = session merge item
session saveOrUpdate mergedItem
}
(tenor, (session merge column).asInstanceOf[Column])
}
matrix.setInputMapNative(mergedColumns)
val mergedMatrix = session merge matrix
session saveOrUpdate mergedMatrix
transaction.commit()