Hibernate exception with @MapsId, @EmbeddedId

前端 未结 4 1265
一整个雨季
一整个雨季 2021-02-09 01:58

I\'ve got a problem with @MapsId annotation and @EmbeddedId. When running a code in Hibernate I get:

Caused by: org.hibernate.Pr

相关标签:
4条回答
  • 2021-02-09 02:22

    See this issue. It simply states that

    org.hibernate.PropertyAccessException may be thrown if an Entity contains the following conditions:

    1. Uses @EmbeddedId
    2. Uses @JoinTable on a collection or association property/field, which references another property/field of the entity.

    As you see, the only 'workaround' suggested so far is Do not use @EmbeddedId, which is kind of weird.

    0 讨论(0)
  • 2021-02-09 02:31

    its an old but might be useful for some one

    try to

    @Entity
    public class Employee implements Serializable {
    
    @EmbeddedId
    private EmployeeId id;
    
    private String firstName;
    
    @ManyToOne
    @MapsId("id")
    @JoinColumn(name ="serverId")
    private Server server;
    
    @OneToOne
    @MapsId("id")
    @JoinColumn(name= "websiteId")
    private Website website;
    
    0 讨论(0)
  • 2021-02-09 02:42

    I had the same problem and fixed it by doing the following, when Hibernate attempts to set the values for Employee.id, employee.id is null, just instantiate employee.id and you'll be set.

    
    @Entity
    public class Employee implements Serializable {
    
        @EmbeddedId
        private EmployeeId id = new EmployeeId();
    
        private String firstName;
    
        @ManyToOne
        @MapsId("serverId")
        private Server server;
    
        @OneToOne
        @MapsId("websiteId")
        private Website website;
    
        public Employee() {}
    
        public Employee(String firstName, Server server, Website website) {
            this.firstName = firstName;
            this.server = server;
            this.website = website;
        }
    }
    
    0 讨论(0)
  • Solution Here: Got the same problem, and got it working now.

    For it to work, the EmployeeId of your Employee class should be instantiated either in the constructor of Employee, or in the SessionBean before making the persistence.

    Otherwise, it tries to populates the values of your embeddedid with it being null.

    Not sure if this is normal or if it's a bad implementation of JPA specifications. I think the latter since the constructor for your PK is defined and hibernate should be able to call it by itself.

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