在表格中我保存了应用,选择,未选择等的整数值...我想在Java Domain类中使用Enum。我试着实现它。当我从DAO调用此域名时,我收到了一些错误的错误。请找到下面的代码。
import java.util.Date;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import javax.persistence.*;
import static javax.persistence.GenerationType.IDENTITY;
@Entity
@Table(name="xxxxxx")
public class JobApplied{
public enum RSVP {
APPLIED_REJECTED(0),
APPLIED_SHORTLISTED(1),
SHORTLISTED_SELECTED (3),
SHORTLISTED_IN_PROGRESS (4),
SHORTLISTED_ON_HOLD (5),
SHORTLISTED_REJECTED (6);
private int value;
RSVP(int value) { this.value = value; }
public int getValue() { return value; }
public static RSVP parse(int id) {
RSVP rsvp = null;
for (RSVP item : RSVP.values()) {
if (item.getValue()==id) {
rsvp = item;
break;
}
}
return rsvp;
}
};
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id",unique = true, nullable = false)
private long id;
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="job_opportunity_id")
@NotFound(action= NotFoundAction.IGNORE)
private JobOpportunity jobOpportunity;
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="user_id")
@NotFound(action= NotFoundAction.IGNORE)
private Users users;
@Column(name="status")
private int RSVP_Status;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public JobOpportunity getJobOpportunity()
{
return jobOpportunity;
}
public void setJobOpportunity(JobOpportunity jobOpportunity)
{
this.jobOpportunity=jobOpportunity;
}
public Users getUsers()
{
return users;
}
public void setUsers(Users users){
this.users=users;
}
public RSVP getRSVP_Status () {
return RSVP.parse(this.RSVP_Status);
}
public void setRSVP_Status(RSVP rsvp) {
this.RSVP_Status = rsvp.getValue();
}
public void setRSVP_Status(int rSVP_Status) {
RSVP_Status = RSVP.parse(rSVP_Status).getValue();
}
}
使用此方法调用上述域类时,
public List<JobApplied> getAppliedCandidates(long jobId) {
Map<String, Object> conditions = getConditionsTemplate();
conditions.put("status", (Integer)JobApplied.RSVP.APPLIED_SHORTLISTED.getValue());
//Error is not because of the above line.
conditions.put("jobOpportunity.jobOpportunityId", jobId);
return findByCriteria(null, conditions); //stack trace points the error in this line.
}
我收到此错误,
org.hibernate.QueryException: could not resolve property: status of: com.xxxx.xxxxx.domain.JobApplied
请清楚我错在哪里。希望我的问题很明确。先谢谢你。
答案 0 :(得分:3)
枚举更改
public enum RSVP {
APPLIED_REJECTED,
APPLIED_SHORTLISTED,
SHORTLISTED_SELECTED,
SHORTLISTED_IN_PROGRESS,
SHORTLISTED_ON_HOLD,
SHORTLISTED_REJECTED;
private int status;
public int getValue() {
return status;
}
}
域类更改
@Enumerated(EnumType.ORDINAL)
@Column(name="status")
private RSVP rsvpStatus;
public RSVP getRsvpStatus() {
return this.rsvpStatus;
}
public void setRsvpStatus(RSVP rsvp) {
this.rsvpStatus = rsvp;
}
DAO通话中的更改
conditions.put("rsvpStatus", RSVP.APPLIED_SHORTLISTED);
表格中的变化
Keep "status" column in table as int datatype
在Hibernate中集成Enum Type非常容易。如果您使用EnumType.STRING进行注释,那么它将在DB中存储字符串值。
答案 1 :(得分:0)
您的财产“状态”不再具有任何getter / setter。 Hibernate需要这些能够访问底层实例变量RSVP_Status。所以你需要这个:
int getStatus() { return RSVP_Status; }
void setStatus(int status) { RSVP_Status = status; }