Kap*_*ino 6 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;
}
Run Code Online (Sandbox Code Playgroud)
我从创建一个hibernate得到了这个例外Criteria.在我的TicketDao课堂上,我有一个按用户名搜索票证的方法,当我调用下面的代码时
Criteria criteria = session.createCriteria(Ticket.class);
criteria.add(Restrictions.eq("userId.username", username));
Run Code Online (Sandbox Code Playgroud)
它抛出异常:
could not resolve property: userId.username of: com.entities.Ticket
Run Code Online (Sandbox Code Playgroud)
但是,当我写下如下标准时:
criteria.add(Restrictions.eq("userId.id", userId));
Run Code Online (Sandbox Code Playgroud)
它没有显示任何异常并返回结果.知道为什么我的语法criteria.add(Restrictions.eq("userId.username", username));和其他属性如firstname,姓氏是错误的?
Criteria不起作用EL或Java方法或属性,你不能用点来引用内部对象..
你必须在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));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1111 次 |
| 最近记录: |