使用关联注释的表的组合键

时间:2017-07-09 12:22:33

标签: java hibernate

我是hibernate的新用户,我正在尝试实现一个使用此架构的基本应用程序(它不遵循我为了清晰起见而使用它的符号)

enter image description here

这是我的课程

@Entity
@Table(name = "race")
public class Race {
    @Id
    @GeneratedValue
    private UUID id;
    private String name;
}

@Entity
@Table(name="np_character")
public class NPCharacter {
    @Id
    @GeneratedValue
    private UUID id;
    @OneToOne
    private Race race;
    private String name;
    private int age;
}

@Entity
@Table(name="main_female_character")
public class MainFemaleCharacter {
    @Id
    @GeneratedValue
    private UUID id;
    @OneToOne
    private Race race;
    private String name;
    private int age;
}

@Entity
@Table(name="copulation_registry")
public class CopulationRegistry {
    // ??
    private NPCharacter npCharacter;
    // ??
    private MainFemaleCharacter femCharacter;
    private int times;
}

但是我遇到了copulation_registry类的问题。我使用了OneToOne注释,而不是使用对键的引用。但我应该在这做什么?成对的id_femPlayerid_npCharacter是唯一的。

我应该使用EmbeddedId注释还是以某种方式使用关联注释来表示相同的关系?

1 个答案:

答案 0 :(得分:1)

You can annotate class CopulationRegistry with @IdClass

@Entity
@IdClass(CopulationRegistryKey.class)
@Table(name="copulation_registry")
public class CopulationRegistry {
    @Id
    private NPCharacter npCharacter;
    @Id
    private MainFemaleCharacter femCharacter;
    private int times;
}


public class CopulationRegistryKey{
    private NPCharacter npCharacter;
    private MainFemaleCharacter femCharacter;
}