1

我怎么能做这个查询: @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 + " ]";
}

}

4

1 回答 1

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

于 2013-01-27T19:17:30.260 回答