3

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_GROUPS {
   LOC_ID, -- PK, and FK to LOCATIONS.ID
   GROUP_NAME -- PK
}

I tried to create entities for these DB tables, but i don't know how to map the connection between the tables. Here is my attempt (but it's wrong):

Embeddable Class

    @Embeddable
    public class LocationGroupId implements Serializable {

        private static final long serialVersionUID = -6437671620548733621L;
        private Location loc;  
        private String group;   
        
        @Column(name = "LOC_ID")
        public Location getLoc() {
            return loc;
        }
        
        @Column(name = "GROUP_NAME")
        public String getGroup() {
            return group;
        }
        
        // ...
    }   

EmbeddedId used

    @Entity
    @Table(name = "LOCATION_GROUPS")
    public class LocationGroup {

        private LocationGroupId id;

        @EmbeddedId
        public LocationGroupId getId() {
            return id;
        }
        
        // ...
    }
    @Entity
    @Table(name = "LOCATIONS")
    public class Location {

        private Long id;
        private String name;
        private List<LocationGroup> groups;
        
        @Column(name = "NAME")
        public String getName() {
            return this.name;
        }
        
        @OneToMany(mappedBy = "id.loc")
        public List<LocationGroup> getGroups() {
            return this.groups;
        }
        
        @Id
        @Column(name = "ID")
        @SequenceGenerator(name = "LocationIdGen", sequenceName = "LOCATION_SQ")
        @GeneratedValue(strategy = GenerationType.AUTO, generator = "LocationIdGen")
        public Long getId() {
            return this.id;
        }
        
        // ...
    }

The difficulty is that I want to make an OneToMany connection between a column and a part of an embeddedId column. Any idea to this problem? (I'm using hibernate 4.0.1)

4

1 回答 1

10

该位置必须用 映射@JoinColumn,而不是用映射@Column

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

请注意,这不是标准的 JPA。为了使其成为标准,您将使用

可嵌入类

@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

@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;
    }
    // ...
}

这两个映射在文档中进行了解释。

于 2013-05-27T14:55:31.347 回答