Hibernate exception with @MapsId, @EmbeddedId

前端 未结 4 1264
一整个雨季
一整个雨季 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: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;
        }
    }
    

提交回复
热议问题