本文介绍了如何使用枚举与jpa作为持久化实体的数据成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请最好的练习和如何使用枚举与jpa作为持久实体的数据成员。最好的做法是什么?
我想从枚举中保留C,O。 (代码)。如果这不是正确的方法请建议。
枚举是 -
public enum Status {
CLOSED(C)
OPEN(O)
private final int value;
private Status(final int pValue){
this.value = pValue;
}
public int value(){
return this.value;
}
解决方案
b 枚举定义
public enum Status {
CLOSED(1),NEW 2),RUNNING(3),OPEN(4),ADDED(5),SUCEESS(-1),DONE(0)
私有int代码;
私人状态(int code){
this.code = code;
}
public int getCode(){
return code;
}
public void setCode(int code){
this.code = code;
}
public static Status valueOf(int i){
for(Status s:values()){
if(s.code == i){
回报
}
}
throw new IllegalArgumentException(no matching constant for+ i);
}
}
实体定义
@Entity
@NamedQuery(name =Process.findAll ,query =从进程p中选择p)
public class Process {
@Id
private long id;
private String name;
@Transient
private transient状态; //实际枚举不存储在db
@Column(name =STATUS)
private int statusCode; //枚举代码存储在db
@PrePersist
void populateDBFields(){
statusCode = status.getCode();
}
@PostLoad
void populateTransientFields(){
status = Status.valueOf(statusCode);
}
public long getId(){
return id;
}
public void setId(long id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Status getStatus(){
return status;
}
public void setStatus(Status status){
this.status = status;
}
}
Please best practice and 'how to' for using enum with jpa as a data member of persisted entity.what is the best practice?I want to persist "C", "O" from enum. (codes). If this is not the correct approach please suggest.
Enum defination is --
public enum Status{
CLOSED ("C")
OPEN ("O")
private final int value;
private Status(final int pValue){
this.value = pValue;
}
public int value(){
return this.value;
}
解决方案
expected Solution:enum defination:
public enum Status {
CLOSED(1), NEW(2), RUNNING(3), OPEN(4), ADDED(5), SUCEESS(-1), DONE(0);
private int code;
private Status(int code) {
this.code = code;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public static Status valueOf(int i){
for (Status s : values()){
if (s.code == i){
return s;
}
}
throw new IllegalArgumentException("No matching constant for " + i);
}
}
Entity Definition:
@Entity
@NamedQuery(name="Process.findAll", query="select p from Process p ")
public class Process {
@Id
private long id;
private String name;
@Transient
private transient Status status; //actual enum; not stored in db
@Column(name="STATUS")
private int statusCode; // enum code gets stored in db
@PrePersist
void populateDBFields(){
statusCode = status.getCode();
}
@PostLoad
void populateTransientFields(){
status = Status.valueOf(statusCode);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
这篇关于如何使用枚举与jpa作为持久化实体的数据成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!