Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate @从主表(compsite主键)到辅助表(单个主键)的辅助表映射_Hibernate_Hibernate Mapping_Composite Primary Key - Fatal编程技术网

Hibernate @从主表(compsite主键)到辅助表(单个主键)的辅助表映射

Hibernate @从主表(compsite主键)到辅助表(单个主键)的辅助表映射,hibernate,hibernate-mapping,composite-primary-key,Hibernate,Hibernate Mapping,Composite Primary Key,表:person id (pk) first_name last_name 实体: @Entity @Table(name="person") public class Person { @Id @Column(name="id") private String id; @Column(name="first_name") private String firstName; @Column(name="last_name")

表:
person

id (pk)  
first_name  
last_name
实体:

@Entity
@Table(name="person")
public class Person {

    @Id
    @Column(name="id")
    private String id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    @Column(name="birthDate")
    private String birthDate;

    @Column(name="detail")
    private String detail;
}
表:
活动

id(pk)
name;
实体:

@Entity
@Table(name="activity")
public class Activity {

    @Id
    @Column(name="id")
    private String id;

    @Column(name="name")
    private String name;

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="activity_id")
    private List<PersonRole> personRoleList;
}
@Entity
@Table(name="person_role")
public class PersonRole {


        @Id
        PersonRolePK personRolePk;


    //  private String personName; /* I want this value to be loaded form person table using the person_id when a PersonRole is reader from database */


        @Column(name="role_name")
        private String roleName;

}


@Embeddable
public class PersonRolePK implements Serializable
{
    public static final long serialVersionUID = 1L;

    @Column(name="activity_id")
    public String activityId;

    @Column(name="person_id")
    public String personId;

}
实体:

@Entity
@Table(name="activity")
public class Activity {

    @Id
    @Column(name="id")
    private String id;

    @Column(name="name")
    private String name;

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="activity_id")
    private List<PersonRole> personRoleList;
}
@Entity
@Table(name="person_role")
public class PersonRole {


        @Id
        PersonRolePK personRolePk;


    //  private String personName; /* I want this value to be loaded form person table using the person_id when a PersonRole is reader from database */


        @Column(name="role_name")
        private String roleName;

}


@Embeddable
public class PersonRolePK implements Serializable
{
    public static final long serialVersionUID = 1L;

    @Column(name="activity_id")
    public String activityId;

    @Column(name="person_id")
    public String personId;

}
当我将数据存储在数据库中时,这对我来说非常有效

当我想从数据库中删除活动时,它也可以按预期工作并加载所有相关的PersonRole

现在,我想在加载PersonRole时,从表
person
中填充类
PersonRole
(注释)的
personName
字段

我读到了关于
@SecondaryTable
注释的内容,并与之接触

但问题是,所有的教程和文档(本)节目中也有 如果我想映射辅助表,那么两个表中必须有相同数量的主键字段(如果是复合键),并且这些字段将使用@PrimaryKeyJoinColumn进行映射

但是在我的例子中,没有必要使用多个键,因为这个人只有一个主键

那么,有没有办法将包含复合主键的表映射到包含单个主键的表以使用
@SecondaryTable

或者


有没有其他好的方法来实现我想要的呢?

通常在这种情况下,我会在活动和个人pojo中建立一个单一的关系。在PersonRole pojo中,每个Activity和Person都有一个manytone关系,它允许您在不复制数据(personname)的情况下获取所需的所有数据

在Person pojo中添加一个OneToMany

Set<PersonRole> personRoles = new HashSet<PersonRoles>();

@OneToMany(fetch = FetchType.EAGER, mappedBy = "person")
public Set<PersonRole> getPersonRoles() {
   return this.personRoles;
}
public void setPersonRoles(Set<PersoneRole> personRoles) {
   this.personRoles = personRoles;
}

您现在应该能够在查询活动时从Person中获取姓名。

我必须说这是一个好方法。但是我只需要
名字
而不需要
本身的引用。和
Person
pojo可以有一些其他字段,如
birthDate``细节
(很抱歉,我没有在问题中添加这些字段)我可以使用manytone获得一些选择性字段吗?