在 JPA (Hibernate) 中保存非持久对象 ID
我有一个静态类,例如:
public class Sex{
private final int code;
private final String status;
public static final int TOTAL = 3;
private Sex(int c, String s) {
code = c;
status = s;
}
public static final Sex UNDEFINED = new Sex(1, "UNDEFINED");
public static final Sex MALE = new Sex(2, "MALE");
public static final Sex FEMALE = new Sex(3, "FEMALE");
private static final Sex[] list = { UNDEFINED, MALE, FEMALE };
public int getCode() {
return code;
}
public String getStatus() {
return status;
}
public static Sex fromInt(int c) {
if (c < 1 || c > TOTAL)
throw new RuntimeException("Unknown code for fromInt in Sex");
return list[c-1];
}
public static List getSexList() {
return Arrays.asList(list);
}
}
而且,我有一个实体类
@Entity
@Table(name="person")
public class Person{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;// setter/getter omitted
private Sex sex;
public final Sex getSex() {
return this.sex;
}
public final void setSex(final Sex argSex) {
this.sex = argSex;
}
}
,我想将 sex_id
保存在数据库的 person 表中。但 setter/getter 应该按照指定的那样,因为我想将代码编写为 -
Person person = new Person();
person.setSex(Sex.MALE);
Dao.savePerson(person);
How to annotate Sex
with JPA?
I've a static class like:
public class Sex{
private final int code;
private final String status;
public static final int TOTAL = 3;
private Sex(int c, String s) {
code = c;
status = s;
}
public static final Sex UNDEFINED = new Sex(1, "UNDEFINED");
public static final Sex MALE = new Sex(2, "MALE");
public static final Sex FEMALE = new Sex(3, "FEMALE");
private static final Sex[] list = { UNDEFINED, MALE, FEMALE };
public int getCode() {
return code;
}
public String getStatus() {
return status;
}
public static Sex fromInt(int c) {
if (c < 1 || c > TOTAL)
throw new RuntimeException("Unknown code for fromInt in Sex");
return list[c-1];
}
public static List getSexList() {
return Arrays.asList(list);
}
}
And, I've an entity class
@Entity
@Table(name="person")
public class Person{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;// setter/getter omitted
private Sex sex;
public final Sex getSex() {
return this.sex;
}
public final void setSex(final Sex argSex) {
this.sex = argSex;
}
}
I want to save sex_id
in database in person table. But setter/getter should be as specified, because I want write my code as -
Person person = new Person();
person.setSex(Sex.MALE);
Dao.savePerson(person);
How to annotate Sex
with JPA?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用枚举器 Sex 来代替,然后使用
@Enumerated
来代替?Why not to use enumerator Sex instead and then use
@Enumerated
instead?既然您不想在数据库中创建新的
Sex
实例,为什么不使用enum
而不是class
来Sex
实例?代码>性别?如果这样做,您只需在
Person
类中使用@Enumerated(EnumType.STRING)
注释您的Sex
属性即可。完整示例此处(或者直接 Google 一下,您会发现很多)Since you don't want to create new
Sex
instance in your database, why don't you use anenum
instead of aclass
forSex
?If you do so, you will just have to annotate you
Sex
attribute with@Enumerated(EnumType.STRING)
in thePerson
class. Full example here (or just Google it and you will find plenty)