could not resolve property: userId.username

1.9k views Asked by At

I have following entity classes:

@MappedSuperclass
public class AbstractEntity implements Serializable, Comparable<AbstractEntity> {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    protected Integer id;

    @Override
    public int compareTo(AbstractEntity o) {
        return this.toString().compareTo(o.toString());
    }

    public Integer getId() {
        return id;
    }

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

}

@Entity
@Table(name = "ticket")
@NamedQueries({
    @NamedQuery(name = "Ticket.findAll", query = "SELECT t FROM Ticket t")})
public class Ticket extends AbstractEntity {

    @Column(name = "title")
    private String title;
    @Column(name = "description")
    private String description;

    @Enumerated(EnumType.STRING)
    @Column(name = "status")
    private TicketStatus status;

    @Enumerated(EnumType.STRING)
    @Column(name = "priority")
    private TicketPriority priority;

    @Column(name = "categories")
    private String categories;
    @Column(name = "views")
    private Integer views;
    @Column(name = "date_time_created")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateTimeCreated;

    @Column(name = "date_time_modified")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateTimeModified;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ticketId")
    private List<TicketFollower> ticketFollowerList;

    @JoinColumn(name = "project_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Project projectId;

    @JoinColumn(name = "ticket_attachment_id", referencedColumnName = "id")
    @ManyToOne
    private TicketAttachment ticketAttachmentId;

    @JoinColumn(name = "user_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private User userId;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ticketId")
    private List<TicketComment> ticketCommentList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ticketId")
    private List<TicketAttachment> ticketAttachmentList;

    @Inject
    public Ticket() {
    }


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    ...

    @Override
    public String toString() {
        return getTitle();
    }

}

@Entity
@Table(name = "user")
@NamedQueries({
    @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")})
public class User extends AbstractEntity {

    @Enumerated(EnumType.STRING)
    @Column(name = "role")
    private Role role;
    @Column(name = "username")
    private String username;
    @Column(name = "password")
    private String password;
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    @Column(name = "email")
    private String email;
    @Column(name = "avatar_path")
    private String avatarPath;
    @Column(name = "date_time_registered")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateTimeRegistered;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "userId")
    private List<TicketFollower> ticketFollowerList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "userId")
    private List<Ticket> ticketList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "userId")
    private List<TicketComment> ticketCommentList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "userId")
    private List<ProjectFollower> projectFollowerList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "userId")
    private List<TicketAttachment> ticketAttachmentList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "userId")
    private List<Project> projectList;

    @Inject
    public User() {}

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

I get this exception from creating a hibernate Criteria. In my TicketDao class I have a method which search ticket by username, and when I invoke code below

Criteria criteria = session.createCriteria(Ticket.class);
criteria.add(Restrictions.eq("userId.username", username));

it throws exception:

could not resolve property: userId.username of: com.entities.Ticket

However, when I write criteria like:

criteria.add(Restrictions.eq("userId.id", userId));

it does not show any exception and returns me result. Any idea why my syntax for criteria.add(Restrictions.eq("userId.username", username)); and other properties like firstname, last name is wrong ?

1

There are 1 answers

4
Jordi Castilla On BEST ANSWER

Criteria does not work like EL or Java methods or attributes, you cannot refer to inner objects with a dot ..

You have to create a restriction in Ticket, right? What does Ticket has? An User. Then... you have to create a new User, set the username to this User and then set the created User to Ticket's criteria:

Criteria criteria = session.createCriteria(Ticket.class);
User user = new User();
user.setUsername(username);
criteria.add(Restrictions.eq("user", user));