我试图从数据库中检索一组结果(13个标记),将只调用1行,然后我想对查询中的13个结果取平均值,然后返回值

我创建了查询:

public String markingAvg(Marking id) {
    System.out.println("id = " + id);
    Query m = em.createQuery("SELECT (m.markSectionOne + m.markSectionTwo + m.markSectionThree + m.markSectionFour + m.markSectionFive + m.markSectionSix + m.markSectionSeven + m.markSectionEight + m.markSectionNine + m.markSectionTen + m.markSectionEleven + m.markSectionTwelve + m.markSectionThirteen) FROM MARKING m WHERE m.id = :id", Double.class);
    m.setParameter("id", id);

    Object avg = m.getSingleResult();
    return null;
}

不确定这是否正确,所以欢迎任何建议

我通过测试按钮调用它:

在里面

            String marking_id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("edit_id");
    System.out.println(marking_id);
    if (marking_id != null) {
        this.markToEdit = this.markingFacade.find(Long.parseLong(marking_id));
    }

    marksByMarkingId = markingFacade.markingAvg(markToEdit);
}

该按钮调用:

public void markSearch() {
    searchAvgResults = markingFacade.markingAvg(markToEdit);

}

但是在初始化时(marksByMarkingId在初始化中完成)

我得到错误

java.lang.IllegalArgumentException: You have attempted to set a value of type class sws.entities.Marking for parameter id with expected type of class java.lang.Long from query string SELECT (m.markSectionOne + m.markSectionTwo + m.markSectionThree + m.markSectionFour + m.markSectionFive + m.markSectionSix + m.markSectionSeven + m.markSectionEight + m.markSectionNine + m.markSectionTen + m.markSectionEleven + m.markSectionTwelve + m.markSectionThirteen) FROM MARKING m WHERE m.id = :id.

有什么想法的人还是有更好的方法做到这一点?

多谢你们

这是标记实体

@Entity(name =“ MARKING”)公共类标记实现了Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ManyToOne
private Person marker;

@ManyToOne
private Project project;

// @ManyToOne
//  private Project project;
//@ManyToMany(mappedBy = "title")
//private Project projectBeMarked;
;

private String markingCompleted, thirdMarker, plagorism, markSectionOne, markSectionTwo, markSectionThree, markSectionFour,
        markSectionFive, markSectionSix, markSectionSeven, markSectionEight,
        markSectionNine, markSectionTen, markSectionEleven, markSectionTwelve, markSectionThirteen, markAdjust, overalMark;

我想做的是检索一个id(我通过从网址传递它来完成),然后想对markSectionOne-13取平均值

ID的获取器/设置器

 /**
     *
     * @return
     */
    public Long getId() {
        return id;
    }

    /**
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

现在修复,感谢一个伟大的家伙在这里

但是,当我按测试按钮运行查询时,在控制台中出现以下错误

Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: The '+' operator with a left operand type of 'VARCHAR' and a right operand type of 'VARCHAR' is not supported.
Error Code: -1
Call: SELECT (MARKSECTIONONE + (MARKSECTIONTWO + (MARKSECTIONTHREE + (MARKSECTIONFOUR + (MARKSECTIONFIVE + (MARKSECTIONSIX + (MARKSECTIONSEVEN + (MARKSECTIONEIGHT + (MARKSECTIONNINE + (MARKSECTIONTEN + (MARKSECTIONELEVEN + (MARKSECTIONTWELVE + MARKSECTIONTHIRTEEN)))))))))))) FROM MARKING WHERE (ID = 48)
Query: ReportQuery(referenceClass=Marking sql="SELECT (MARKSECTIONONE + (MARKSECTIONTWO + (MARKSECTIONTHREE + (MARKSECTIONFOUR + (MARKSECTIONFIVE + (MARKSECTIONSIX + (MARKSECTIONSEVEN + (MARKSECTIONEIGHT + (MARKSECTIONNINE + (MARKSECTIONTEN + (MARKSECTIONELEVEN + (MARKSECTIONTWELVE + MARKSECTIONTHIRTEEN)))))))))))) FROM MARKING WHERE (ID = ?)")

目前收到错误

Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: TYPE 'UNSIGNED' does not exist.
Error Code: -1
Call: SELECT (CAST(MARKSECTIONONE AS UNSIGNED) + (CAST(MARKSECTIONTWO AS UNSIGNED) + (CAST(MARKSECTIONTHREE AS UNSIGNED) + (CAST(MARKSECTIONFOUR AS UNSIGNED) + (CAST(MARKSECTIONFIVE AS UNSIGNED) + (CAST(MARKSECTIONSIX AS UNSIGNED) + (CAST(MARKSECTIONSEVEN AS UNSIGNED) + (CAST(MARKSECTIONEIGHT AS UNSIGNED) + (CAST(MARKSECTIONNINE AS UNSIGNED) + (CAST(MARKSECTIONTEN AS UNSIGNED) + (CAST(MARKSECTIONELEVEN AS UNSIGNED) + (CAST(MARKSECTIONTWELVE AS UNSIGNED) + CAST(MARKSECTIONTHIRTEEN AS UNSIGNED))))))))))))) FROM MARKING WHERE (ID = 48)
Query: ReportQuery(referenceClass=Marking sql="SELECT (CAST(MARKSECTIONONE AS UNSIGNED) + (CAST(MARKSECTIONTWO AS UNSIGNED) + (CAST(MARKSECTIONTHREE AS UNSIGNED) + (CAST(MARKSECTIONFOUR AS UNSIGNED) + (CAST(MARKSECTIONFIVE AS UNSIGNED) + (CAST(MARKSECTIONSIX AS UNSIGNED) + (CAST(MARKSECTIONSEVEN AS UNSIGNED) + (CAST(MARKSECTIONEIGHT AS UNSIGNED) + (CAST(MARKSECTIONNINE AS UNSIGNED) + (CAST(MARKSECTIONTEN AS UNSIGNED) + (CAST(MARKSECTIONELEVEN AS UNSIGNED) + (CAST(MARKSECTIONTWELVE AS UNSIGNED) + CAST(MARKSECTIONTHIRTEEN AS UNSIGNED))))))))))))) FROM MARKING WHERE (ID = ?)")

关于查询:

public String markingAvg(Marking id) {
    System.out.println("In MarkingAvg");
    System.out.println("id = " + id);
    System.out.println("id =" + id.getId());
    Query m = em.createQuery("SELECT (CAST(m.markSectionOne AS UNSIGNED) + CAST(m.markSectionTwo AS UNSIGNED) + CAST(m.markSectionThree AS UNSIGNED) + CAST(m.markSectionFour AS UNSIGNED) + CAST(m.markSectionFive AS UNSIGNED) + CAST(m.markSectionSix AS UNSIGNED) + CAST(m.markSectionSeven AS UNSIGNED) + CAST(m.markSectionEight AS UNSIGNED) + CAST(m.markSectionNine AS UNSIGNED) + CAST(m.markSectionTen AS UNSIGNED) + CAST(m.markSectionEleven AS UNSIGNED) + CAST(m.markSectionTwelve AS UNSIGNED) + CAST(m.markSectionThirteen AS UNSIGNED)) FROM MARKING m WHERE m.id = :id", Double.class);
    m.setParameter("id", id.getId()); // Note the getId()

    Object avg = m.getSingleResult();
    return null; // Do you really want to be returning null here?
}

===============>>#1 票数:1 已采纳

您的应用程序在那里给您非常有用的错误消息:

java.lang.IllegalArgumentException: You have attempted to set a value of type class sws.entities.Marking for parameter id with expected type of class java.lang.Long from query string

重要的一点是: expected type of class java.lang.Long

您需要的Long参数包含在Marking对象的id属性中,因此,不要将Marking对象本身设置为查询参数,而只需设置其id属性即可:

public String markingAvg(Marking id) {
    System.out.println("id = " + id);
    Query m = em.createQuery("SELECT (m.markSectionOne + m.markSectionTwo + m.markSectionThree + m.markSectionFour + m.markSectionFive + m.markSectionSix + m.markSectionSeven + m.markSectionEight + m.markSectionNine + m.markSectionTen + m.markSectionEleven + m.markSectionTwelve + m.markSectionThirteen) FROM MARKING m WHERE m.id = :id", Double.class);
    m.setParameter("id", id.getId()); // Note the getId()

    Object avg = m.getSingleResult();
    return null; // Do you really want to be returning null here?
}

解决第二个问题; 您正在尝试将VARCHAR列的内容添加在一起(或者至少是我对您的意图的假设)。 如果需要将这些列加在一起,则需要将它们转换为支持加法的数据类型。 对于每个VARCHAR列,您都需要执行以下操作:

CAST(COLUMN AS INTEGER)

应用于您的查询,它将变为:

"SELECT (CAST(m.markSectionOne AS INTEGER) + CAST(m.markSectionTwo AS INTEGER) + CAST(m.markSectionThree AS INTEGER) + CAST(m.markSectionFour AS INTEGER) + CAST(m.markSectionFive AS INTEGER) + CAST(m.markSectionSix AS INTEGER) + CAST(m.markSectionSeven AS INTEGER) + CAST(m.markSectionEight AS INTEGER) + CAST(m.markSectionNine AS INTEGER) + CAST(m.markSectionTen AS INTEGER) + CAST(m.markSectionEleven AS INTEGER) + CAST(m.markSectionTwelve AS INTEGER) + CAST(m.markSectionThirteen AS INTEGER)) FROM MARKING m WHERE m.id = :id"

作为对这个最后一点,我个人不喜欢的SQL查询做一些事情不仅仅是带回原始数据更多的想法。 数据库不需要知道您需要将所有这些列的值加起来,只需要知道您需要这些值并将其提供给您即可。 如果是我,我会在Java层中进行添加,而不是让查询来完成。

  ask by user2061913 translate from so

未解决问题?本站智能推荐: