为什么冬眠中的AddScalar不起作用?

发布于 2025-02-06 20:11:34 字数 1908 浏览 2 评论 0原文

我试图使用本机标语来创建查询,但是当我尝试添加到DTO中时。它不认识DTO。

public List<AbsenceResponse> findAllByUserId1(Long id) {
        List<AbsenceResponse> res = null;
        StringBuilder sql = new StringBuilder();
        sql.append(
                "SELECT  a.absence_id, a.create_date, a.end_date,a.date_off,a.title,a.description,a.status,a.type,a.enable_date, u.full_name as fullNameOfDepartment");
        sql.append(" FROM absence a ");
        sql.append(" INNER JOIN user u ON u.user_id = a.absence_by ");
        sql.append(" WHERE a.user_id = "+id);

        res = entityManager.unwrap(org.hibernate.Session.class).createNativeQuery(sql.toString())
                .addScalar("absenceId", LongType.INSTANCE)
                .addScalar("createDate", DateType.INSTANCE)
                .addScalar("end_date", DateType.INSTANCE)
                .addScalar("dateOff", IntegerType.INSTANCE)
                .addScalar("title", StringType.INSTANCE)
                .addScalar("description", StringType.INSTANCE)
                .addScalar("status", IntegerType.INSTANCE)
                .addScalar("type", IntegerType.INSTANCE)
                .addScalar("enable_date", DateType.INSTANCE)
                .addScalar("fullNameOfDepartment", StringType.INSTANCE)

                .setResultTransformer(Transformers.aliasToBean(AbsenceResponse.class))
                .list();
        return res;
    }

这是我的缺陷(DTO),

public class AbsenceResponse {

    private Long absenceId;
    private Date startDate;
    private Date endDate;
    private String title;
    private String description;
    private int status;
    private int type;
    private Date enableDate;
    private String fullNameOfDepartment
}

轮到这个错误了,

java.sql.SQLException: Column 'absenceId' not found.

这是我第一次使用本地标语。如果我有不良或缺少的东西,请在下面发表评论。您的评论对我有很多帮助。玩的很开心

Im trying to create a query using NativeQuery but when i'm trying to add into a DTO. It's not recognize DTO.

public List<AbsenceResponse> findAllByUserId1(Long id) {
        List<AbsenceResponse> res = null;
        StringBuilder sql = new StringBuilder();
        sql.append(
                "SELECT  a.absence_id, a.create_date, a.end_date,a.date_off,a.title,a.description,a.status,a.type,a.enable_date, u.full_name as fullNameOfDepartment");
        sql.append(" FROM absence a ");
        sql.append(" INNER JOIN user u ON u.user_id = a.absence_by ");
        sql.append(" WHERE a.user_id = "+id);

        res = entityManager.unwrap(org.hibernate.Session.class).createNativeQuery(sql.toString())
                .addScalar("absenceId", LongType.INSTANCE)
                .addScalar("createDate", DateType.INSTANCE)
                .addScalar("end_date", DateType.INSTANCE)
                .addScalar("dateOff", IntegerType.INSTANCE)
                .addScalar("title", StringType.INSTANCE)
                .addScalar("description", StringType.INSTANCE)
                .addScalar("status", IntegerType.INSTANCE)
                .addScalar("type", IntegerType.INSTANCE)
                .addScalar("enable_date", DateType.INSTANCE)
                .addScalar("fullNameOfDepartment", StringType.INSTANCE)

                .setResultTransformer(Transformers.aliasToBean(AbsenceResponse.class))
                .list();
        return res;
    }

And this is my AbsenceResponse(DTO)

public class AbsenceResponse {

    private Long absenceId;
    private Date startDate;
    private Date endDate;
    private String title;
    private String description;
    private int status;
    private int type;
    private Date enableDate;
    private String fullNameOfDepartment
}

it's turn this error

java.sql.SQLException: Column 'absenceId' not found.

This is the first time i used NativeQuery. If i have something bad or missing, please comment below. Your comment is helped me alot. Have a good time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半窗疏影 2025-02-13 20:11:35

Select-Query返回的列被命名为haberence_id(而不是habenceid)。对于所有其他与不匹配的其他列的错误 - 创建例如(应该为create_date)。

它是本机查询,因此您需要使用本机SQL查询返回的名称(而不是类中属性的名称)。

您还可以将查询更改为:

SELECT  a.absence_id as absenceId, a.create_date as createDate, a.end_date as endDate, a.date_off as dateOff, ...

这样,查询结果将与BEAN中的属性匹配,并应将问题与结果变压器解决。

The column returned by the select-query is named absence_id (and not absenceId). You will have a similar error for all the other columns not matching - createDate for example (it should be create_date).

It's a native query, so you need to use the named returned by the native SQL query (and not the name of the attribute in the class).

You can also change the query to:

SELECT  a.absence_id as absenceId, a.create_date as createDate, a.end_date as endDate, a.date_off as dateOff, ...

This way the result of the query will match the attributes in the bean and should fix the issue with the result transformer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文