JPA Composite key with ManyToOne getting org.hibernate.PropertyAccessException: could not set a field value by reflection setter of

前端 未结 2 2015
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 22:15

I have a composite key ContractServiceLocationPK made out of three id\'s (contractId, locationId, serviceId) of type long

相关标签:
2条回答
  • 2020-12-18 22:42

    You need to put the getters and setters in your @Embeddable class as well, your hashCode() and equals() methods will go in to that class which I couldn't see in your class posted here.

    In order to save the ContractServiceLocation, following objects needs to be saved first because you are using their ids as composite key for the ContractServiceLocation, right? Here what you are doing is you are creating these as new objects so obviously they won't have their id, because they are not persisted. so you need to persist them first and use the persisted objects and set objects into the ContractServiceLocation.

        Service service = new Service();
        // Set all service properties       
        Contract contract = new Contract();
        // Set all contract properties
        Location location = new Location();
        // Set all location properties
    
    0 讨论(0)
  • 2020-12-18 22:49

    For your original question (not modified variant):

    You have to instatiate "ContractServiceLocationPK id" in your ContractServiceLocation class. Replace line:

    @EmbeddedId ContractServiceLocationPK id;

    with this:

    @EmbeddedId ContractServiceLocationPK id = new ContractServiceLocationPK();

    Then it should works. Because Hibernate is trying to set properties inside, but fail on NullPointerException.

    0 讨论(0)
提交回复
热议问题