Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@OneToOne annotation within composite key class is not working

Maybe somebody can clarify what is wrong with the code below. When I create one-to-one association within embedded class (it is composite primary key) like 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 the sample above works if I change association type to many-to-one and doesn't work with one-to-one?

like image 845
Graffinych Avatar asked Jul 16 '10 15:07

Graffinych


1 Answers

I wasn't aware this was possible but, according to the Hibernate Annotation reference documentation, it is (this is Hibernate specific though):

2.2.3.2.1. @EmbeddedId property

(...)

While not supported in JPA, Hibernate lets you place your association directly in the embedded id component (instead of having to use the @MapsId annotation).

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

And with the code you provided, the following snippet just works for me:

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();

Tested with Hibernate EM 3.4.0.GA, Hibernate Annotations 3.4.0.GA and Hibernate Core 3.3.0.SP1.

If it doesn't work for you, can you provide a bit more code allowing to reproduce the problem?

like image 185
Pascal Thivent Avatar answered Oct 17 '22 21:10

Pascal Thivent