Moro
Moro

Reputation: 2128

Why am I getting this NullPointer exception?

Two tables, primary key of one is foreign key of another (Legacy DB)
I used bi-directional one to one mapping:

@Entity
public class First {
    @Id protected int a;

    @OneToOne(mappedBy ="first", cascade = CascadeType.PERSIST)
    @JoinColumn(name = "a")
    protected Second second;
}


@Entity
public class Second {

    @Id protected int a;

    @OneToOne
    @JoinColumn(name = "a", insertable = false)
    First first;
}

The problem is when running:

public class Persister {

    public static void main(String[] args) {

        EntityManagerFactory aEntityManagerFactory;
        EntityManager aEntityManager;       
        aEntityManagerFactory =
                       Persistence.createEntityManagerFactory("bibit_notification_jpa");
        aEntityManager = aEntityManagerFactory.createEntityManager();

        Second aSecond = new Second();
        aSecond.a = 1;          

        First aFirst = new First();
        aFirst.a = 1;
        aFirst.second = aSecond;

        aEntityManager.getTransaction().begin();
        aEntityManager.persist(aFirst);
        aEntityManager.getTransaction().commit();

      }}

, it throws this exception:

Exception in thread "main" javax.persistence.PersistenceException: java.lang.NullPointerException
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:252)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:120)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
    at Persister.main(Persister.java:14)
Caused by: java.lang.NullPointerException
    at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
    at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1233)
    at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:869)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:183)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:240)
    ... 4 more

Upvotes: 1

Views: 7940

Answers (2)

David Rabinowitz
David Rabinowitz

Reputation: 30448

You may want to have a look at the second example in the JavaDoc. If I read this correctly the code should be

@Entity
public class First {
    @Id protected int a;

    @OneToOne @PrimaryKeyJoinColumn
    protected Second second;
}

@Entity
public class Second {

    @Id protected int a;

    @OneToOne @PrimaryKeyJoinColumn
    First first;
}

Upvotes: 1

Don Branson
Don Branson

Reputation: 13709

This post: http://forum.hibernate.org/viewtopic.php?t=970823)

Seems to indicate that there's some kind of problem with annotations 3.2.x. What version are you using? If it's 3.2.x, try 3.3 or 3.4.

Upvotes: 2

Related Questions