问题描述
我有两个实体,Credential
和Account
.每个Account
必须具有唯一的Credential
,并且每个Credential
必须具有唯一的电子邮件地址和唯一的用户名.我打算稍后向Account
添加更多属性,因此我隔离了Credential
实体的一些属性:
I have two entities, Credential
and Account
. Every Account
must have a unique Credential
, and each Credential
must have a unique e-mail address and a unique username. I intend to add more atributes to Account
later, so I isolated some atributes to a Credential
entity:
@Entity
public class Credential {
@Id
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false, unique = true)
private String nickname;
@Column(nullable = false)
private String password;
还有我的Account
属性:
@Entity
public class Account {
@Id
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn(name = "email", referencedColumnName = "email")
private Credential credential;
@Column(nullable = false)
private long level;
@Column(nullable = false)
private boolean loggedIn;
我看到了此答案,并决定使用PrimaryKeyJoinColumn
表示法.当我尝试实例化EntityManagerFactory
时,出现此堆栈错误:
I saw this answer and decided to use PrimaryKeyJoinColumn
notation. When I try to instantiate a EntityManagerFactory
, I get this stack error:
Caused by: java.lang.NullPointerException
at org.hibernate.mapping.PersistentClass.createPrimaryKey(PersistentClass.java:363)
还有更多行,但是没有一行显示Credential
或Account
.在我的persistence.xml
上,我与标准文件有以下主要区别:
There are more lines, but none of them has Credential
or Account
on it. On my persistence.xml
I have these main differences from standard file:
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>entity.PersistentEntity.Credential</class>
<class>entity.PersistentEntity.Account</class>
<properties>
<property name="tomee.jpa.factory.lazy" value="true"/>
//unchanged stuff before it stopped working
我正在将Intellij与JEE 2.0一起用于TomEE 8.5.6,并且必须按照所述此处.在这种情况下,这是我的问题:
I'm using Intellij with TomEE 8.5.6 with JPA 2.0 and I have to set that property as stated here. Given this scenario, here are my questions:
-
什么原因导致NPE?
What is causing NPE?
我显然缺少好的编码习惯?如果是这样,那是谁?
There are obviously good coding pratices I'm missing? If so, which ones are they?
鉴于问题开始时我的实体逻辑,映射此实体的最佳方法是什么?
What is the best way to map this entities, given my entity logic at question's beginning?
推荐答案
我发现我错误地使用了PrimaryKeyJoinColumn
此处,并且此符号来自JPA1.0.现在,通过JPA2.0,我可以使用@Id + @JoinColumn
如上一个链接和此中所述. 我的问题已解决,现在两个表都将Credential
中的email
字段用作PK.
I discovered I was wrongly using PrimaryKeyJoinColumn
here and this notation was from JPA1.0. Now with JPA2.0 I can use @Id + @JoinColumn
as stated on previous link and on this one. My problem was solved and now both tables use email
field from Credential
as PK.
这篇关于NPE在createPrimaryKey上.如何正确映射这两个实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!