@OneToOne annotation in composite key class not working

Perhaps someone can clarify what is wrong with the code below. When I create a one-to-one association inside an inline class (this is a composite primary key), as in the code below:

@Entity
public class Test {

    @EmbeddedId
    private TestId id;

    @Embeddable
    public static class TestId implements Serializable {
        private static final long serialVersionUID = 1950072763330622759L;

        @OneToOne(optional = false)
        @JoinColumn(name = "linkedTable_id")
        private LinkedTable linkedTable;

    }
    ..........
}

I get the following stack trace:

--------------------------------------------

Caused by: java.lang.NullPointerException
    at org.hibernate.cfg.AnnotationBinder.bindOneToOne(AnnotationBinder.java:1867)
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1286)
    at org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:1662)
    at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1695)
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1171)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:706)
    at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:452)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:268)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1121)
    at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1211)
    at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:847)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:178)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:235)
    ... 26 more

What is interesting, why does the example above work if I change the type of association to many-one and do not work with a single one?

+5
source share
1 answer

I did not know that this is possible, but according to the reference documentation for Hibernate Annotation, this (this is more like Hibernate):

2.2.3.2.1. Property @EmbeddedId

(...)

JPA, Hibernate id ( , @MapsId ).

@Entity
class Customer {
  @EmbeddedId CustomerId id;
  boolean preferredCustomer;
}

@Embeddable
class CustomerId implements Serializable {
  @OneToOne
  @JoinColumns({
    @JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"),
    @JoinColumn(name="userlastname_fk", referencedColumnName="lastName")
  }) 
  User user;
  String customerNumber;
}

@Entity 
class User {
  @EmbeddedId UserId id;
  Integer age;
}

@Embeddable
class UserId implements Serializable {
  String firstName;
  String lastName;
}

, , :

LinkedTable linkedTable = new LinkedTable();
linkedTable.setId(1l);
session.persist(linkedTable);
session.flush();

Test.TestId testId = new Test.TestId();
testId.setLinkedTable(linkedTable);
Test test = new Test();
test.setId(testId);
session.persist(test);
session.flush();

Hibernate EM 3.4.0.GA, Hibernate Annotations 3.4.0.GA Hibernate Core 3.3.0.SP1.

, , ?

+5

All Articles