0

In my project I'm using JPA2 and Hibernate within eclipse Indigo.
I have an abstract BaseEntity class which provides some fields that are needed for all entities in my project. Therefore this class is extended by all other entities I use in my project.
One of the fields in BaseEntity is a boolean field deleted, which is needed to mark an entry as deleted (it's not allowed to physically delete an object from the database). Now when I try to set up a named query in a subclass and use a "NOT u.deleted" in the WHERE-clause, i get the following error message from the compiler:
The state field path 'r.deleted' cannot be resolved to a valid type.

Below the code of the BaseEntity:

@MappedSuperclass
public abstract class BaseEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  protected Long id;

  @Version
  protected int version;

  @Column(nullable = false, columnDefinition = "boolean default false")
  protected boolean deleted;

  public boolean isDeleted() {
      return deleted;
  }

  public boolean getDeleted() { // for test-purposes introduced both - isDeleted() & getDeleted()
      return deleted;
  }

  public void setDeleted(boolean deleted) {
      this.deleted = deleted;
  }

  // some other attributes, getters & setters

}

The code of an inheriting class:

@Entity
@Table(name = "User")
@NamedQueries({
    @NamedQuery(name = "getAllUsers", query = "SELECT u FROM User u WHERE NOT u.deleted")
})
public class User extends BaseEntity {
    // some other fields and getter/setters
}

Any guesses what could be wrong?

EDIT: updated code according to comments below.

Stefan
  • 337
  • 6
  • 20

2 Answers2

0

I think the getters and setters are important. I am not sure, about the naming vonvention, but ther shoul be a "getIsDeleted()" or "isIsDeleted()" method.

Try to rename your boolean to "deleted"

update: Don't use NOT

SELECT u FROM User u WHERE u.deleted = FALSE

There is a Tutorial about JPA

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
  • Right, a second "is" would have missed in the getter. However I renamed the attribute name to `deleted` and renamed the getter/setter to `isDeleted()` and `setDeleted(boolean deleted)` - but still getting the error. By the way, both methods are public in BaseEntity. – Stefan May 15 '12 at 07:08
  • I already tried that too - didn't work either. And just for "fun" also tried using the getter-Method in the WHERE-Part like `WHERE NOT r.isDeleted()` but this didn't work. – Stefan May 15 '12 at 07:20
  • One silly question: You select *Select u from User u*, so it must be *u.deleted*, not *r.deleted*. Have you a simple typo in you query? – Christian Kuetbach May 15 '12 at 07:25
  • Sorry, I copy-pasted to the comment above from another class where the query is the same but another object name - but the variable u is correct - just a typo in the comment above. – Stefan May 15 '12 at 07:29
  • I've tried: `WHERE NOT u.isDeleted()` and `WHERE NOT u.getDeleted()` and `WHERE NOT u.deleted` - but none of them did work. – Stefan May 15 '12 at 07:30
  • Use *u.deleted* not the name of the method in the query. – Christian Kuetbach May 15 '12 at 07:31
  • Just to be clear: `SELECT u FROM User u WHERE NOT u.deleted` => this doesn't work. – Stefan May 15 '12 at 07:33
  • Response to update for not using NOT: For this topic I already searched through a lot of tutorials. I already tried different options like using `NOT u.deleted` or `!u.deleted` or `u.deleted = FALSE` – Stefan May 15 '12 at 07:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11256/discussion-between-ckuetbach-and-stefan) – Christian Kuetbach May 15 '12 at 07:49
0

Actually you mapped your table "User" with Class "Role". So your query should be like:

@NamedQuery(name = "getAllUsers", query = "SELECT r FROM Role r WHERE NOT r.deleted")

Result : Return List of Role having deleted = false

kunal
  • 323
  • 2
  • 9
  • sorry, that was a typo - Table User is mapped to class User. however it seems to be a bug in the hibernate preprocessor in eclipse as we could reproduce the behaviour with every boolean value in every table/class... – Stefan Jul 27 '12 at 12:25