我如何才能执行此查询:
@NamedQuery(name = "Scuser.findFriends", query = "SELECT s FROM Scuser s, friends f WHERE f.firstid = :iduser and s.iduser = f.secondid")
在这堂课中:
@Entity
@Table(name = "scuser")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Scuser.findAll", query = "SELECT s FROM Scuser s"),
@NamedQuery(name = "Scuser.findByIduser", query = "SELECT s FROM Scuser s WHERE s.iduser = :iduser"),
@NamedQuery(name = "Scuser.findByUpassword", query = "SELECT s FROM Scuser s WHERE s.upassword = :upassword"),
@NamedQuery(name = "Scuser.findByUname", query = "SELECT s FROM Scuser s WHERE s.uname = :uname"),
@NamedQuery(name = "Scuser.findByTpoints", query = "SELECT s FROM Scuser s WHERE s.tpoints = :tpoints"),
// @NamedQuery(name = "Scuser.findFriends", query = "SELECT s FROM Scuser s, friends f WHERE f.firstid = :iduser and s.iduser = f.secondid"),
@NamedQuery(name = "Scuser.findByUnameUpassword", query = "SELECT s FROM Scuser s WHERE s.uname = :uname and s.upassword = :upassword")})
public class Scuser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 2147483647)
@Column(name = "iduser")
private String iduser;
@Size(max = 200)
@Column(name = "upassword")
private String upassword;
@Size(max = 200)
@Column(name = "uname")
private String uname;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Column(name = "tpoints")
private Double tpoints;
@JoinTable(name = "friends", joinColumns = {
@JoinColumn(name = "firstid", referencedColumnName = "iduser")}, inverseJoinColumns = {
@JoinColumn(name = "secondid", referencedColumnName = "iduser")})
@ManyToMany
private Collection<Scuser> scuserCollection;
@ManyToMany(mappedBy = "scuserCollection")
private Collection<Scuser> scuserCollection1;
@ManyToMany(mappedBy = "scuserCollection")
private Collection<Beach> beachCollection;
public Scuser() {
}
public Scuser(String iduser) {
this.iduser = iduser;
}
public String getIduser() {
return iduser;
}
public void setIduser(String iduser) {
this.iduser = iduser;
}
public String getUpassword() {
return upassword;
}
public void setUpassword(String upassword) {
this.upassword = upassword;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Double getTpoints() {
return tpoints;
}
public void setTpoints(Double tpoints) {
this.tpoints = tpoints;
}
@XmlTransient
public Collection<Scuser> getScuserCollection() {
return scuserCollection;
}
public void setScuserCollection(Collection<Scuser> scuserCollection) {
this.scuserCollection = scuserCollection;
}
@XmlTransient
public Collection<Scuser> getScuserCollection1() {
return scuserCollection1;
}
public void setScuserCollection1(Collection<Scuser> scuserCollection1) {
this.scuserCollection1 = scuserCollection1;
}
@XmlTransient
public Collection<Beach> getBeachCollection() {
return beachCollection;
}
public void setBeachCollection(Collection<Beach> beachCollection) {
this.beachCollection = beachCollection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (iduser != null ? iduser.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Scuser)) {
return false;
}
Scuser other = (Scuser) object;
if ((this.iduser == null && other.iduser != null) || (this.iduser != null && !this.iduser.equals(other.iduser))) {
return false;
}
return true;
}
@Override
public String toString() {
return "REST.Scuser[ iduser=" + iduser + " ]";
}
}
答案 0 :(得分:1)
如果我理解正确,您拥有用户的ID,并且您希望获得此ID标识的用户的朋友。最简单的方法是
Scuser user = em.find(User.class, userId);
Collection<Scuser> friends = user.getScuserCollection();
如果您想使用JPQL查询,只需要
select friend from Scuser user
inner join user.scuserCollection friend
where user.id = :userId
请注意,您的映射不正确:scuserCollection1和beachCollection都由相同的属性映射。您还应该为关联选择更好的名称(例如friends
,而不是scuserCollection
)。