How to query a reference table with a value

how to query a reference table with a value.

I am trying to query a table with a unique string value but got an exception:

ClassCastException: java.lang.String cannot be cast to com.company.cvr.entity.Status

Here is the code:

 @Override
        public Status newtatus() {

            Status value;

            try (Transaction tx = persistence.createTransaction()) {
                Query query = persistence.getEntityManager().createQuery(
                        "select e.status from cvr$Status e where e.status = 'TIMESHEET-NEW' ");

               // query.setParameter("sts", "TIMESHEET-NEW");


                value = (Status) query.getFirstResult();
                //  tx.commit();
            }

            return value;

        }

any idea ?

thanks,

Hi, without more context you cannot get much help here.
You need at least to post your entity’s definition and explain what you’re trying to achieve with your code.

P.

I have a table called timesheet. in the timesheet table there is a column called status which is link to status table. I just want to set the default value in timesheet.status as one of the records from status table with the value of “TIMESHEET-NEW”.

here are the entitys:

package com.company.cvr.entity;

import javax.persistence.Entity;
import javax.persistence.Table;
import com.haulmont.cuba.core.entity.StandardEntity;
import javax.persistence.Column;
import javax.validation.constraints.NotNull;
import com.haulmont.cuba.core.entity.annotation.CaseConversion;
import com.haulmont.chile.core.annotations.NamePattern;

@NamePattern("%s|status")
@Table(name = "CVR_STATUS")
@Entity(name = "cvr$Status")
public class Status extends StandardEntity {
    private static final long serialVersionUID = 4345940019731765775L;

    @CaseConversion
    @NotNull
    @Column(name = "STATUS", nullable = false, unique = true, length = 40)
    protected String status;


    public void setStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }


}

package com.company.cvr.entity;

import javax.persistence.Entity;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import com.haulmont.cuba.core.entity.StandardEntity;
import com.haulmont.cuba.security.entity.User;
import com.haulmont.chile.core.annotations.NamePattern;

@NamePattern("%s|user")
@Table(name = "CVR_TIME_SHEET")
@Entity(name = "cvr$TimeSheet")
public class TimeSheet extends StandardEntity {
    private static final long serialVersionUID = -1159443290299199192L;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "USER_ID")
    protected User user;

    @Temporal(TemporalType.DATE)
    @NotNull
    @Column(name = "DATE_", nullable = false)
    protected Date date;

    @Temporal(TemporalType.TIME)
    @NotNull
    @Column(name = "STARTTIME", nullable = false)
    protected Date starttime;

    @Temporal(TemporalType.TIME)
    @NotNull
    @Column(name = "ENDTIME", nullable = false)
    protected Date endtime;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "ORDER_ID")
    protected Order order;

    @Column(name = "CHEQUERECEIVED")
    protected BigDecimal chequereceived=BigDecimal.ZERO;

    @Column(name = "DESCRIPTION", length = 1000)
    protected String description;

    @Column(name = "CASHRECEIVED")
    protected BigDecimal cashreceived=BigDecimal.ZERO;

    @Column(name = "ODOMETER")
    protected Integer odometer=0;

    @Column(name = "HOURRATE")
    protected BigDecimal hourrate=BigDecimal.ZERO;

    @Column(name = "HOURS")
    protected BigDecimal hours=BigDecimal.ZERO;

    @Column(name = "HOURAMOUNT")
    protected BigDecimal houramount=BigDecimal.ZERO;

    @Column(name = "ODORATE")
    protected BigDecimal odorate=BigDecimal.ZERO;

    @Column(name = "ODOAMOUNT")
    protected BigDecimal odoamount=BigDecimal.ZERO;

    @Column(name = "TOTAL")
    protected BigDecimal total=BigDecimal.ZERO;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "STATUS_ID")
    protected Status status;

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    public void setStatus(Status status) {
        this.status = status;
    }

    public Status getStatus() {
        return status;
    }




    public void setDate(Date date) {
        this.date = date;
    }

    public Date getDate() {
        return date;
    }

    public void setStarttime(Date starttime) {
        this.starttime = starttime;
    }

    public Date getStarttime() {
        return starttime;
    }

    public void setEndtime(Date endtime) {
        this.endtime = endtime;
    }

    public Date getEndtime() {
        return endtime;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public Order getOrder() {
        return order;
    }

    public void setChequereceived(BigDecimal chequereceived) {
        this.chequereceived = chequereceived;
    }

    public BigDecimal getChequereceived() {
        return chequereceived;
    }

    public void setCashreceived(BigDecimal cashreceived) {
        this.cashreceived = cashreceived;
    }

    public BigDecimal getCashreceived() {
        return cashreceived;
    }

    public void setOdometer(Integer odometer) {
        this.odometer = odometer;
    }

    public Integer getOdometer() {
        return odometer;
    }

    public void setHourrate(BigDecimal hourrate) {
        this.hourrate = hourrate;
    }

    public BigDecimal getHourrate() {
        return hourrate;
    }

    public void setHours(BigDecimal hours) {
        this.hours = hours;
    }

    public BigDecimal getHours() {
        return hours;
    }

    public void setHouramount(BigDecimal houramount) {
        this.houramount = houramount;
    }

    public BigDecimal getHouramount() {
        return houramount;
    }

    public void setOdorate(BigDecimal odorate) {
        this.odorate = odorate;
    }

    public BigDecimal getOdorate() {
        return odorate;
    }

    public void setOdoamount(BigDecimal odoamount) {
        this.odoamount = odoamount;
    }

    public BigDecimal getOdoamount() {
        return odoamount;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }

    public BigDecimal getTotal() {
        return total;
    }


}

you’re selecting the attribute status of the Status entity, not the entity itself, that’s why the subsequent cast does not work.
Try this instead:
select e from cvr$Status e where e.status = 'TIMESHEET-NEW'