1

This question is related to the issue I was having with this question, but not identical:

How do I avoid NullPointerException with OneToOne mapping against a parent entity class?

Creating a OneToOne mapping seems to work fine if I specify my mapping like this:

Person + PersonPartDeux
  |
  +--User

However it doesn't work if I change the name of the secondary entity in the OneToOne:

Person + DersonPartDeux (note the D)
  |
  +--User

In this case I get an error:

java.lang.NullPointerException
    at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)

The names I picked for this example are notably bad, but my original code has less bogus names, with the OneToOne mapping to a completely different name than the original entity.

I debugged the Hibernate-Annotations source and I see that the personPartDeux gets mapped properly as a property (in OneToOneSecondPass.java), but it doesn't show up as a property on the Entity if the entity/property name is dersonPartDeux.

What is the reason this property doesn't get mapped?

Is there a way to override this behavior (e.g. specifying join column names, or entity names in one of the annotations)?

I am using Hibernate 3.2.4.sp1 and Hibernate Annotations 3.2.1.GA and can't upgrade right now.

Here's the code that works:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Person implements Serializable
{
    @Id
    @GeneratedValue
    public Long id;

    @Version
    public int version = 0;

    public String name;

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public PersonPartDeux personPartDeux;
}

@Entity
public class PersonPartDeux implements Serializable
{
    @Id
    @GeneratedValue(generator = "person-primarykey")
    @GenericGenerator(
        name = "person-primarykey",
        strategy = "foreign",
        parameters = @Parameter(name = "property", value = "person")
    )
    public Long id = null;

    @Version
    public int version = 0;

    @OneToOne(optional=false, mappedBy="personPartDeux")
    public Person person;

    public String someText;
}

@Entity
@PrimaryKeyJoinColumn(name = "person_Id")
public class User extends Person
{
    public String username;
    public String password;
}

If I replace these two classes I get the NullPointerException I referenced above:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Person implements Serializable
{
    @Id
    @GeneratedValue
    public Long id;

    @Version
    public int version = 0;

    public String name;

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public DersonPartDeux dersonPartDeux; // Note D in entity/field name
}

@Entity
public class DersonPartDeux implements Serializable
{
    @Id
    @GeneratedValue(generator = "person-primarykey")
    @GenericGenerator(
        name = "person-primarykey",
        strategy = "foreign",
        parameters = @Parameter(name = "property", value = "person")
    )
    public Long id = null;

    @Version
    public int version = 0;

    @OneToOne(optional=false, mappedBy="dersonPartDeux") // Note the d in the property name
    public Person person;

    public String someText;
}
Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183

1 Answers1

0

On your project enviroment, do you need to list entities class in the persistence.xml? Perhaps you forgot to change the name at the <class> element.

Guillermo
  • 1,523
  • 9
  • 19
  • The framework we're using (not sure if this is a Hibernate feature or a Seam feature) automatically detects all types annotated with `@Entity`, so this isn't the problem. Thanks for taking a look though! – Merlyn Morgan-Graham Oct 04 '12 at 16:55