1

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;

And my Account atribute:

@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;

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)

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

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:

  1. What is causing NPE?

  2. There are obviously good coding pratices I'm missing? If so, which ones are they?

  3. What is the best way to map this entities, given my entity logic at question's beginning?

rado
  • 5,720
  • 5
  • 29
  • 51

1 Answers1

0

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.

rado
  • 5,720
  • 5
  • 29
  • 51