【发布时间】:2017-04-07 21:51:01
【问题描述】:
我有点卡住了,不明白发生了什么。 这个不行
@Entity
@DynamicInsert
@DynamicUpdate
@SelectBeforeUpdate
@Table
class Entity {
@Column(nullable = false)
var owner: String = _
}
val myEntity = new Entity() {
owner = "some owner 1"
}
session.persist(myEntity)
Hibernate 抛出异常:
java.lang.IllegalArgumentException: Unknown entity:persistence.dao.EntityDaoTest$$anonfun$13$$anonfun$14$$anon$5
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:777)
这个有效:
val myEntity = new Entity()
entity.owner = "some owner 1"
session.persist(myEntity)
为什么?为什么 hibernate 无法识别我的 Entity 实例?
更新: @Sheinbergon,谢谢,很清楚。我完全忘记了注释丢失了。有没有可能用一些快捷方式设置实体字段? 写作
val myEntity = new MyEntity()
myEntity.owner = "some owner"
myEntity.someOtherProperty = "value"
超级无聊
还有一个问题 这个有效:
val parent = new Parent
parent.owner = "Our parent"
parent.addChild(new Child() {
name = "First parent's child"
addGrandChild(new GrandChild() {
name = "Grand child name"
addGrandGrandChild(new GrandGrandChild() {
name = "Grand Grand child name"
address = new Address() {
id = 1L
}
})
})
})
为什么? Child、GrandChild、GrandGrandChild 也是匿名创建的。 addChild、addGrandChild、addGrandGrandChild 只是列表修改器。
def addChild(child: Child): Unit = {
if (children == null) {
children = new util.ArrayList[Child]()
}
if (Option(child.parent).isEmpty) {
child.parent = this
}
children.add(child)
}
【问题讨论】:
-
请“接受”我的回答(其中任何一个),因为它对您有帮助。我在下面的答案中添加了另一部分,该部分与您提出的新子问题有关
-
完成,谢谢!现在很清楚了
标签: scala hibernate anonymous-class