我一直在努力解决在添加 OneToMany
时无法实例化 EntityManager 的问题已具有 OneToMany
的类的列专栏。
现在 EducationInfo
有一个 OneToOne
给用户,因为每一行对一个人来说都是唯一的,但我希望能够加载给定用户名的所有教育信息。
我在添加列时没有更改 orm.xml 文件,我只是删除了数据库以便重新创建它。
class User {
@Id
@GeneratedValue(){val strategy = GenerationType.AUTO}
var id : Int = _
@Column{val nullable = false, val length=32}
var firstName : String = ""
var lastName : String = ""
@Column{val nullable = false, val unique = true, val length = 30}
var username : String = ""
@OneToMany{val fetch = FetchType.EAGER, val cascade=Array(CascadeType.ALL)}
var address : java.util.List[Address] = new java.util.ArrayList[Address]()
@OneToMany{val fetch = FetchType.EAGER, val cascade=Array(CascadeType.ALL), val mappedBy="user"}
var educationInfo : java.util.List[EducationInfo] = new java.util.ArrayList[EducationInfo]()
如果我不包含最后一列,那么我的单元测试工作正常,但如果我包含最后一列,那么我的所有单元测试都会失败。
这是它尝试使用的类,并且该类的单元测试工作正常:
@Entity
@Table{val name="educationinfo"}
class EducationInfo {
@Id
@GeneratedValue(){val strategy = GenerationType.AUTO}
var id : Long = _
@Column{val nullable = false, val length = 100}
var name : String = "";
@OneToOne{val fetch = FetchType.EAGER, val cascade = Array(CascadeType.PERSIST, CascadeType.REMOVE)}
@JoinColumn{val name = "educationinfo_address_fk", val nullable = false}
var location : Address = _
@ManyToOne{val fetch = FetchType.LAZY, val cascade=Array(CascadeType.PERSIST)}
@JoinColumn{val name = "educationinfo_user_fk", val nullable = false}
var user : User = _
@Column{val nullable = false, val length = 100}
var degree : String = ""
@Temporal(TemporalType.DATE)
@Column{val nullable = true}
var startyear : Date = new Date()
var endyear : Date = new Date()
@Column{val nullable = true, val length = 1000}
var description : String = ""
}
我也很好奇是否有某种方法可以知道阻止 EntityManager 被实例化的错误是什么,但主要问题是当我将此列添加到 User 时可能导致问题的原因是什么?强>
更新:
删除了错误的部分。
更新 2:
我将教育实体中的用户列更改为 @ManyToOne
我所有的单元测试都失败了。
我的错误日志的结尾是这样的:
15035 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
15035 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
15047 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
[PersistenceUnit: jpaweb] Unable to build EntityManagerFactory
[PersistenceUnit: jpaweb] Unable to build EntityManagerFactory
然后 EntityManager 在每个测试中都为 null,因此失败。
更新 3:
Address
实体没有任何外键,因为它是从多个实体调用的。
这个问题会不会是数据库使用Apache Derby引起的?
很遗憾,我没有收到任何错误消息。我从 Maven 中运行它,虽然我每次都删除数据库,但我无法判断它发生了什么,那是错误的。来自 EducationInfo -> User 的 @ManyToOne 不会造成任何问题,但来自 User 的 @OneToMany -> EducationInfo 会造成任何问题。
更新 4:
我按照建议向 User 实体添加了一个 mappedBy。我在另一个列定义中有它,因为我有五个不起作用的定义,我相信它们都是出于相同的原因,所以我只用一个进行测试。不幸的是,它仍然导致未构建 EntiyManagerFactory,因此所有测试都失败。
最终更新:
实际情况是
cannot simultaneously fetch multiple bags
所以我只是从 EducationInfo 中完全删除了 FetchType,问题就消失了。
最佳答案
OneToMany
在关联另一边的补映射是ManyToOne
,它不能是OneToOne
。这就是导致 EntityManager
失败的原因。
当您从 User
中删除最后一行时它会起作用,因为那样您只剩下 OneToMany
到 Address
并且,虽然没有显示在这里,Address
可能没有指向 User
的 OneToOne
链接。
我不是 Scala 专家;在 Java 中,当 EntityManager
初始化失败时,通常会出现错误并打印堆栈跟踪;错误消息并不总是非常具有描述性,但通常会说明它有问题的映射。如果没有收到该错误,您可能需要调整日志记录设置。
我不太明白你的模型:
Basically, a user should be able to have several addresses, so I expect it will become a ManyToMany, but each use can also have several instances of education information, so the need for ManyToOne.
对我来说,这听起来像是从 User
到 Address
的 @OneToMany 和(如果“use”意味着“user”)作为来自 User
的 @OneToMany > 到 EducationInfo
。
Right now the EducationInfo has a OneToOne to user, since each row will be unique to a person, but I want to be able to load all the education information given a username.
这与上面的冲突。您可以为每个 User
拥有多个 EducationInfo
,或者它“对一个人来说是独一无二的”。
关于hibernate - 当我添加 OneToMany 注释时,EntityManager 无法使用 JPA/Hibernate 进行实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1947627/