0

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.

3
  • 1
    I'm not a Hibernate guru, but have you tried Restrictions.eq("systemAdministrator", true)? Commented Jul 12, 2014 at 19:30
  • You might not be one, but your answer worked actually. I am just curious, why the hell Hibernate takes out the IS prefix from my property? Commented Jul 12, 2014 at 19:46
  • @LukeWoodward maybe you should post yours as the correct answer so I can accept it. If you have any sort of explanation about the missing IS preffix, that would awesome. Commented Jul 12, 2014 at 19:52

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

nope, the issue doesn't have to do with the ID field. Other queries work well using that column. The problem is only with isSystemAdministrator field. Luke has answered already the solution on his comment above. Thanks.
I used your code completely and it did not work for me neither. I change it as I wrote and worked. Then I saw @Luke Woodward comment I tried is and it works for me, too. I am curious why.
Please excuse me lack of Java knowlegde. I come from a C# background and it's been like three weeks since I am coding in Java. But AFAIK when mapping Hibernate fields you need to chose to put annotations on the fields or on the getter/setter methods. I went for the later and I don't think that makes any difference at all, does it?
After litte time I maybe figured it why and and next post gives you the answer. Ony special case with your field isSystemAdministrator. As you used annotation on getter method, Hibernate automaticaly expected that you adhere JavaBean spec and probably it looked for isIsSystemAdministrator() method. I used field access and worked because of no naming mismatch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.