1

In my test project I'm trying to make work the following:

@Entity
@IdClass(PersonId.class)
public class Person {
    @Id
    private String name;
    @Id
    @ManyToOne
    private Home home;


public class PersonId implements Serializable {
    private String name;
    private HomeId home;


@Entity
@IdClass(HomeId.class)
public class Home {
    @Id
    private String address;
    @Id
    private String bed;
    @OneToMany(mappedBy = "home")
    private List<Person> people = new LinkedList<>();

public class HomeId implements Serializable {
    private String address;
    private String bed;

I'm trying to store a home and then a person with the home:

    Home home = new Home();
    home.setAddress("address");
    home.setBed("bed");
    entityManager.persist(home);
    Person person = new Person();
    person.setName("name");
    person.setHome(home);
    entityManager.persist(person);

The home get's saved correctly but when it comes to a person I get the following exception:

org.springframework.orm.jpa.JpaSystemException: No part of a composite identifier may be null; nested exception is org.hibernate.HibernateException: No part of a composite identifier may be null

How can I make it work? Please, add any ideas, any are appreciated!

Dmitry Senkovich
  • 5,521
  • 8
  • 37
  • 74
  • every sample I have ever seen has composite key in form of object (class). You Person should contain PersonId, but nit have now. Similar : https://stackoverflow.com/questions/13032948/how-to-create-and-handle-composite-primary-key-in-jpa – Jacek Cz Aug 19 '17 at 15:36
  • @Jacek Cz thanks for the answer! however, you're talking about `@Embeddedid`, but I'm using `@IdClass` – Dmitry Senkovich Aug 19 '17 at 15:44
  • 2
    This seems to be valid as part of the JPA 2.0 spec, and is similar to the example here: https://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/DerivedIdentifiers You might check your Hibernate version support, or see if there is more to the error that might indicate where it is coming from and what it thinks is null. – Chris Aug 19 '17 at 17:08
  • @Chris yes, actually it is slightly changed example from "Pro JPA" book, so that is strange it doesn't work. Also the version with '@Embeddable's doesn't work. Thank you, I will try to dig dipper in the docs, I guess it is the thing I can do – Dmitry Senkovich Aug 19 '17 at 17:16

0 Answers0