我是hibernate
的新用户,我正在尝试实现一个使用此架构的基本应用程序(它不遵循我为了清晰起见而使用它的符号)
这是我的课程
@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_femPlayer
和id_npCharacter
是唯一的。
我应该使用EmbeddedId
注释还是以某种方式使用关联注释来表示相同的关系?
答案 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;
}