Hibernate @EmbeddedId +加入 [英] Hibernate @EmbeddedId + join

查看:95
本文介绍了Hibernate @EmbeddedId +加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个hibernate映射问题。我有以下两个数据库表(我不允许更改数据库):

i have a hibernate mapping problem. I have the following two DB tables (I don't 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
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;
    }

    // ...
}   

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

    // ...
}

难点是我想在一列和embeddedId列的一部分之间建立OneToMany连接。
对这个问题有任何想法? (我使用hibernate 4.0.1)

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

推荐答案

该位置必须使用 @JoinColumn ,而不是 @Column :

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

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

请注意,这不是标准的JPA。为了达到这个标准,你可以使用

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

@Embeddable
public class LocationGroupId implements Serializable {

    private static final long serialVersionUID = -6437671620548733621L;
    private Long locationId;  
    private String group;   

    @Column(name = "LOC_ID")
    public Long getLocationId() {
        return loc;
    }

    @Column(name = "GROUP_NAME")
    public String getGroup() {
        return group;
    }

    // ...
}   

@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.

这篇关于Hibernate @EmbeddedId +加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆