java restful webservice中的mysql查询

时间:2014-04-26 10:50:58

标签: java jpa

我在netbeans中完成了一个安静的网络服务。我有以下类实体和查询。

@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
        @NamedQuery(name = "Users.findAll",
                query = "SELECT u FROM Users u"),
        @NamedQuery(name = "Users.findById",
                query = "SELECT u FROM Users u WHERE u.id = :id"),
        @NamedQuery(name = "Users.findByName",
                query = "SELECT u FROM Users u WHERE u.name = :name"),
        @NamedQuery(name = "Users.findByTelephone",
                query = "SELECT u FROM Users u WHERE u.telephone = :telephone"),
        @NamedQuery(name = "Users.findByYear",
                query = "SELECT u FROM Users u WHERE u.year = :year")})

public class Users implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Integer id;
    @Size(max = 25)
    @Column(name = "name")
    private String name;
    @Column(name = "telephone")
    private Integer telephone;
    @Column(name = "year")
    private Integer year;

    public Users() {
    }

    public Users(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getTelephone() {
        return telephone;
    }

    public void setTelephone(Integer telephone) {
        this.telephone = telephone;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.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 Users)) {
            return false;
        }
        Users other = (Users) object;
        if ((this.id == null && other.id != null)
                || (this.id != null && !this.id.equals(other.id))) {

            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "glee.Users[ id=" + id + " ]";
    }

好吧,我想从这个类中使用这样的查询。例如:

@NamedQuery(name = "Padres.findByPass", query = "SELECT p FROM Padres p WHERE p.pass = :pass"),

以下是我的疑惑: 我创建了另一个类来尝试这个查询:

@Stateless
@Path("ws.gleegoo")
public class aaa extends AbstractFacade<Padres> {
    @PersistenceContext(unitName = "WsBbaPU")
    private EntityManager em;

    public aaa() {
        super(Padres.class);
    }

    @GET
    @Path("/consulta")

    @Produces(MediaType.TEXT_HTML)
    public String returnTitle() {
        Query query = em.createNamedQuery("Padres.findByPass")
                .setParameter("pass", 2);
        return "<p>" + query + "<p>";
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
}

这不起作用......

我的想法是不要显示通行证。这只是学习咨询bd。因为在我尝试从android应用程序接收传递和名称的方法并与bd的那些进行比较后,我返回true或false。

如果有人知道比告诉我更好。谢谢

0 个答案:

没有答案