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

为君一笑 提交于 2019-11-29 09:46: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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!