2

In table I am saving integer values for applied, selected, not selected etc... What I thought is to use Enum in Java Domain class. I tried implementing it. While I am calling this domain from DAO, I am getting some weired error. Please find the code(s) below.

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();
    }   
}

While calling the above domain class using this method,

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.
}

I am getting this error,

org.hibernate.QueryException: could not resolve property: status of: com.xxxx.xxxxx.domain.JobApplied

Kindly clear me where I am wrong. Hope my question is clear. Thank you in Advance.

Vignesh Gopalakrishnan
  • 1,962
  • 9
  • 31
  • 53

2 Answers2

3

Change in Enum

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;
    }
}

Change in Domain Class

    @Enumerated(EnumType.ORDINAL)
    @Column(name="status")
    private RSVP rsvpStatus;

    public RSVP getRsvpStatus() {
        return this.rsvpStatus;
    }

    public void setRsvpStatus(RSVP rsvp) {
        this.rsvpStatus = rsvp;
    }

Change in DAO Call

conditions.put("rsvpStatus", RSVP.APPLIED_SHORTLISTED);

Change in Table

Keep "status" column in table as int datatype

It's very easy to integrate Enum Type in Hibernate.If you annotate with EnumType.STRING then it will store string values in DB.

Gabriel Llamas
  • 18,244
  • 26
  • 87
  • 112
Vignesh Gopalakrishnan
  • 1,962
  • 9
  • 31
  • 53
0

Your property "status" doesn't have any getter/setter anymore. Hibernate needs these to be able to access the underlying instance variable RSVP_Status. So you would need this:

int getStatus() { return RSVP_Status; }
void setStatus(int status) { RSVP_Status = status; }
geert3
  • 7,086
  • 1
  • 33
  • 49