在 JPA (Hibernate) 中保存非持久对象 ID

发布于 2024-12-19 23:20:35 字数 1349 浏览 0 评论 0原文

我有一个静态类,例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫离伤花 2024-12-26 23:20:36

为什么不使用枚举器 Sex 来代替,然后使用 @Enumerated 来代替?

 @Enumerated(EnumType.STRING)
   Sex sex

Why not to use enumerator Sex instead and then use @Enumerated instead?

 @Enumerated(EnumType.STRING)
   Sex sex
小鸟爱天空丶 2024-12-26 23:20:35

既然您不想在数据库中创建新的 Sex 实例,为什么不使用 enum 而不是 classSex 实例?代码>性别?

如果这样做,您只需在 Person 类中使用 @Enumerated(EnumType.STRING) 注释您的 Sex 属性即可。完整示例此处(或者直接 Google 一下,您会发现很多)

Since you don't want to create new Sex instance in your database, why don't you use an enum instead of a class for Sex?

If you do so, you will just have to annotate you Sex attribute with @Enumerated(EnumType.STRING) in the Person class. Full example here (or just Google it and you will find plenty)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文