无法解析 属性:userId.username

could not resolve property: userId.username

我有以下实体 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;
    }

我在创建休眠 Criteria 时遇到了这个异常。在我的 TicketDao class 中,我有一个按用户名搜索票证的方法,当我调用下面的代码时

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));

它没有显示任何异常和 returns 我的结果。知道为什么我的 criteria.add(Restrictions.eq("userId.username", username)); 语法和名字、姓氏等其他属性是错误的吗?

Criteria 不像 ELJava 方法或属性那样工作,你不能用点 [=] 引用 内部对象 14=]。

你必须在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));