Query database with login user

hi ,

got this error:

IllegalArgumentException: You have attempted to set a value of type class java.util.UUID for parameter login with expected type of class com.haulmont.cuba.security.entity.User from query string

select e.hourrate from cvr$Employee e where e.user = :login.

please help.

here is the code:

   public BigDecimal userhourrate() {
        BigDecimal value;
        try (Transaction tx = persistence.createTransaction()) {
            Query query = persistence.getEntityManager().createQuery(
                    "select e.hourrate from cvr$Employee e where e.user = :login");


            User thisuser=  userSessionSource.getUserSession().getUser();

                query.setParameter("login", thisuser);


            value = (BigDecimal) query.getFirstResult();
            tx.commit();
        }
        if (value == null)
            value = BigDecimal.ZERO;
        return value;

        // return null;
    }

here is the entity:

@NamePattern("%s|user")
@Table(name = "CVR_EMPLOYEE")
@Entity(name = "cvr$Employee")
public class Employee extends StandardEntity {
    private static final long serialVersionUID = 7683984161080010795L;

    @NotNull
    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "USER_ID", unique = true)
    protected User user;

    @NotNull
    @Column(name = "EMPLOYEE_ID", nullable = false, unique = true)
    protected Integer employeeID;

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

    @NotNull
    @Column(name = "ODORATE", nullable = false)
    protected BigDecimal odorate;

Please use ``` for code blocks (before and after them) and fix your posts with proper formatting.

Hi,

any update ?

thanks,

Hi,
The framework version 6.x has a legacy feature mentioned here - it implicitly converts a parameter of entity type to the entity id. So this query will work: select e.hourrate from cvr$Employee e where e.user.id = :login.

In the upcoming version 7, this conversion is not performed by default.

Regards,
Konstantin

Thanks,

when I changed to " select e.hourrate from cvr$Employee e where e.user.id = :login ", no error, but the query returns o row. the value is always null.

here is the code:

 @Override
    public BigDecimal userhourrate() {
        BigDecimal value;
        try (Transaction tx = persistence.createTransaction()) {
            Query query = persistence.getEntityManager().createQuery(
                    "select e.hourrate from cvr$Employee e where e.user.id  = :login");



            User thisuser= userSessionSource.getUserSession().getUser();

            // output message with DEBUG level
            log.debug(query.toString());

                query.setParameter("login", thisuser);

            log.debug(query.toString());

            value = (BigDecimal) query.getFirstResult();
          //  tx.commit();
        }
          if (value == null)
            value = BigDecimal.TEN;
          return value;

        // return null;
    }

ok, sorry . it works when I set with the user id.

  @Override
    public BigDecimal userhourrate() {
        BigDecimal value;
        try (Transaction tx = persistence.createTransaction()) {
            Query query = persistence.getEntityManager().createQuery(
                    "select e.hourrate from cvr$Employee e where e.user.id  = :login");



            User thisuser= userSessionSource.getUserSession().getUser();

            // output message with DEBUG level
            log.debug(query.toString());

                query.setParameter("login", thisuser.getId());

            log.debug(query.toString());

            value = (BigDecimal) query.getFirstResult();
          //  tx.commit();
        }
          if (value == null)
            value = BigDecimal.TEN;
          return value;

        // return null;
    }

Actually, due to the implicit conversion of entity to its id it should work in both cases.