使用外键的jpa命名查询无法正常工作

时间:2013-03-21 03:58:34

标签: jpa named-query

MY Entity class

@Entity
@Table(catalog = "", schema = "MYIS")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Answers.findAll", query = "SELECT a FROM Answers a"),
    @NamedQuery(name = "Answers.findByAid", query = "SELECT a FROM Answers a WHERE a.aid = :aid"),
    @NamedQuery(name ="Anaswers.findByqid", query ="SELECT a FROM Answers a WHERE a.answerQid.qid = :x"),
    @NamedQuery(name = "Answers.findByAnsValue", query = "SELECT a FROM Answers a WHERE a.ansValue = :ansValue"),
    @NamedQuery(name = "Answers.findByAnsDate", query = "SELECT a FROM Answers a WHERE a.ansDate = :ansDate")})
public class Answers implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    private Integer aid;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 4000)
    @Column(name = "ANS_VALUE", nullable = false, length = 4000)
    private String ansValue;
    @Basic(optional = false)
    @NotNull
    @Column(name = "ANS_DATE", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date ansDate;
    @JoinColumn(name = "A_USERID", referencedColumnName = "USERID", nullable = false)
    @ManyToOne(optional = false)
    private Users aUserid;
    @JoinColumn(name = "ANSWER_QID", referencedColumnName = "QID", nullable = false)
    @ManyToOne(optional = false)
    private Questions answerQid;
    @JoinColumn(name = "A_GROUPID", referencedColumnName = "GID", nullable = false)
    @ManyToOne(optional = false)
    private Groups aGroupid;

    public Answers() {
    }

    public Answers(Integer aid) {
        this.aid = aid;
    }

    public Answers(Integer aid, String ansValue, Date ansDate) {
        this.aid = aid;
        this.ansValue = ansValue;
        this.ansDate = ansDate;
    }

    public Integer getAid() {
        return aid;
    }

    public void setAid(Integer aid) {
        this.aid = aid;
    }

    public String getAnsValue() {
        return ansValue;
    }

    public void setAnsValue(String ansValue) {
        this.ansValue = ansValue;
    }

    public Date getAnsDate() {
        return ansDate;
    }

    public void setAnsDate(Date ansDate) {
        this.ansDate = ansDate;
    }

    public Users getAUserid() {
        return aUserid;
    }

    public void setAUserid(Users aUserid) {
        this.aUserid = aUserid;
    }

    public Questions getAnswerQid() {
        return answerQid;
    }

    public void setAnswerQid(Questions answerQid) {
        this.answerQid = answerQid;
    }

    public Groups getAGroupid() {
        return aGroupid;
    }

    public void setAGroupid(Groups aGroupid) {
        this.aGroupid = aGroupid;
    }

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

    @Override
    public String toString() {
        return "com.entity.Answers[ aid=" + aid + " ]";
    }

}

我的会议

import com.entity.Answers;
import com.entity.Groups;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author krishna teja
 */
@Stateless
public class AnswersFacade extends AbstractFacade<Answers> implements AnswersFacadeLocal {
    @PersistenceContext(unitName = "My_communityPU")
    private EntityManager em;

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

    public AnswersFacade() {
        super(Answers.class);
    }

 public List<Answers> getdataByQid(Long qid){


     Query query=em.createNamedQuery("Answers.findByqid");
     query.setParameter(1, qid);
     List<Answers> a =query.getResultList();
        return a;

 }


}

我的托管bean

     @PostConstruct
      public void init(){

            questions = questionsFacade.findAll();
            ansList = answersFacade.getdataByQid(g);

            }

我遇到了异常

at com.ejb.AnswersFacade.getdataByQid(AnswersFacade.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)

我已经在sessionfacade中创建了外键属性answerQid和创建方法的命名查询,并尝试在托管bean中访问它,默认方法工作正常,但我的查询方法不起作用请帮帮我

1 个答案:

答案 0 :(得分:1)

看起来像一个简单的拼写错误。命名查询定义为Anaswers.findByqid,但用作Answers.findByqid