Hibernate @EmbeddedId + join

前端 未结 1 970
囚心锁ツ
囚心锁ツ 2020-12-19 17:59

I have a hibernate mapping problem. I have the following two DB tables (I am not allowed to change the DB):

LOCATIONS {
   ID, -- PK
   NAME
}

LOCATION_GROUP         


        
相关标签:
1条回答
  • 2020-12-19 18:39

    The location must be mapped with @JoinColumn, and not with @Column:

    @JoinColumn(name = "LOC_ID")
    public Location getLoc() {
        return loc;
    }
    

    Note that this is not standard JPA though. To make it standard, you would use

    Embeddable Class

    @Embeddable
    public class LocationGroupId implements Serializable {
    
        private static final long serialVersionUID = -6437671620548733621 L;
        private Long locationId;
        private String group;
    
        @Column(name = "LOC_ID")
        public Long getLocationId() {
            return loc;
        }
    
        @Column(name = "GROUP_NAME")
        public String getGroup() {
            return group;
        }
        // ...
    }
    

    EmbeddedId Used

    @Entity
    @Table(name = "LOCATION_GROUPS")
    public class LocationGroup {
    
        private LocationGroupId id;
        private Location location;
    
        @EmbeddedId
        public LocationGroupId getId() {
            return id;
        }
    
        @ManyToOne
        @JoinColumn(name = "LOC_ID")
        @MapsId("locationId")
        private Location getLocation() {
            return location;
        }
        // ...
    }
    

    These two mappings are explained in the documentation.

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