无法解析属性:userId.username

时间:2015-06-16 14:32:47

标签: java hibernate criteria

我有以下实体类:

@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;
    }

我从创建一个hibernate Criteria中得到了这个例外。在我的TicketDao课程中,我有一个按用户名搜索票证的方法,当我调用下面的代码时

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

它抛出异常:

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

然而,当我写下以下标准时:

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

它没有显示任何异常并返回结果。知道为什么我的criteria.add(Restrictions.eq("userId.username", username));语法和其他属性如firstname,last name是错误的吗?

1 个答案:

答案 0 :(得分:5)

Criteria不像ELJava方法或属性那样有效,您不能使用点.来引用内部对象

你必须在Ticket中创建一个限制,对吧? Ticket有什么作用?一个User。然后......您必须创建一个新的User,将username设置为此User,然后将创建的User设置为Ticket的条件:

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