coffekid
coffekid

Reputation: 605

Boolean field Hibernate QueryException: could not resolve property

I am really newbie to Hibernate and it's been like two hours trying to figure it out how to fix this issue. I am using Hibernate 4 and Postgres 9.3

Given the CatalogBase class

@MappedSuperclass
public class CatalogBase {

    @Id
    @Type(type = "pg-uuid")
    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    protected UUID id;
}

And the derived User class

@Entity
@Table(name="erpuser")
public class User extends CatalogBase {
    private String lastName;
    private String name;
    private String email;
    private boolean isSystemAdministrator;

    @Type(type="org.hibernate.type.StringClobType")
    @Column(nullable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name="lastname")
    @Type(type="org.hibernate.type.StringClobType")
    @NotNull(message = "es campo mandatorio")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(length = 100,unique = true)
    @NotNull(message = "es campo mandatorio")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "issystemadministrator", nullable = false)
    public boolean isSystemAdministrator() {
        return isSystemAdministrator;
    }

    public void setSystemAdministrator(boolean isSystemAdministrator) {
        this.isSystemAdministrator = isSystemAdministrator;
    }
}

I am trying to filter just the first result of a query using Hibernate Criteria. Like this

    public boolean existsSystemAdministrator() throws NoSuchAlgorithmException{
    Criteria criteria=currentSession()
            .createCriteria(User.class)
            .add(Restrictions.eq("isSystemAdministrator", true));
    return  criteria.uniqueResult() != null;
}

But I always get org.hibernate.QueryException: could not resolve property: isSystemAdministrator exception

I have changed to all lowercase since the database field is like that, but it didn't work either. From what I've read Hibernate maps with the Java property, which hasn't been the case as well.

Have tried also change the isSystemAdministrator field to Boolean instead of boolean, but it didn't work out either.

I know this must sound stupid to any Hibernate guru, if someone can come up with an answer that would save me lots of time.

Thanks in advance.

Upvotes: 0

Views: 2464

Answers (2)

Arthur Loder
Arthur Loder

Reputation: 36

You should adhere to the JavaBeans spec (http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html). The field should be 'systemAdministrator', and the method should be 'isSystemAdministrator'.

Upvotes: 1

PaulP
PaulP

Reputation: 76

The problem is in @Id annotation in CatalogBase class. If you change so it will work fine:

@MappedSuperclass
public class CatalogBase {

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    @Id
    @Type(type = "pg-uuid")
    protected UUID id;
}

You can have 2 access types in Hibernate. Property access (as you did) or field access. Hibernate will guess the access type from the position of @Id or @EmbeddedId.

As I know (I am not a Hibernate guru), it should be no difference between these two access types. But some frameworks requires to have field access. Anyway, I do not know why your implementation does not work for querying and have not found any other explanation.

Upvotes: 0

Related Questions