2015-06-16 189 views
6

我有以下實體類:無法解析屬性:userId.username

@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 I類有哪些方法搜索票通過用戶名,當調用下面

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));和其他屬性如名字,姓氏是錯誤的?

回答

5

Criteria不起作用像ELJava方法或屬性,你不能引用內對象以點.

你必須在Ticket中創建一個限制,對吧? Ticket有什麼?一個User。然後......你必須創建一個新的User,設置username這個User,然後設置創建UserTicket的標準:

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

但是我沒有這個用戶id,我被'標準搜索.add(Restrictions.eq(「userId.username」,username))'有可能嗎? – Kapparino

+0

在用戶中使用'username'然後...檢查我的編輯,我添加了更多解釋 –

+0

問題是我的實體用戶沒有使用用戶名的構造函數。我想我需要改變邏輯... – Kapparino

相關問題