我有一个使用Hibernate会话通过JPA @NamedNativeQuery使用内存中的H2数据库进行测试(实际数据库为MySQL)的存储过程的类。该存储过程将一条记录插入数据库表中,然后返回该行。

但是,在测试期间,在转换为JPA @Entity时,我看到一个H2数据库错误:org.h2.jdbc.JdbcSQLException:找不到列“ Id”。

我已经记录了以下代码的简化版本。

据我所知,我认为它与@Id注释的H2解释有关,但不知道为什么,所以将不胜感激任何帮助...

注意:我已经相当广泛地搜索了Stack溢出,包括与在列说明中使用双引号有关的问题,但是不要认为这与我的情况有关...



CREATE TABLE History.Status_Report (
Id INT NOT NULL AUTO_INCREMENT,
Unique_Users INT NOT NULL,
PRIMARY KEY (Id)
);


储存程序

CREATE PROCEDURE History.Status_Reporting(reporting_date DATE)

BEGIN

INSERT INTO history.status_report (Unique_Users) VALUES (10);
SELECT *
     FROM History.Status_Report WHERE Id = LAST_INSERT_ID();
END;


实体
    打包com.test;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedNativeQuery;
import javax.persistence.Table;
import java.io.Serializable;

@NamedNativeQuery(name = "callStatusReporting", query = "CALL Status_Reporting(:reporting_date)", resultClass = StatusReportEntity.class)
@Entity
@Table(name = "Status_Report", catalog = "History")
public class StatusReportEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    @Column(name = "Id")
    protected Integer id;
    @Column(name = "Unique_Users")
    protected int uniqueUsers;

    public Integer getId() {
        return this.id;
    }

    public int getUniqueUsers() {
        return this.uniqueUsers;
    }
}


被测课程

package com.test;

import org.hibernate.Query;
import org.hibernate.Session;

public class MyListener {

    public StatusReportEntity doRequest(Date reportDate) {
        Session session = HibernateUtil.openSession(); // returns a MySQL session or H2 if testing…
        try {
            Query query = session.getNamedQuery("callStatusReporting").setParameter("reporting_date", reportDate);;
            StatusReportEntity statusReportEntity = (StatusReportEntity) query.uniqueResult();
            return statusReportEntity;
        } catch (Exception e) {
            System.err.println(e);
        } finally {
            session.close();
            return null;
        }
    }


H2别名

要启用使用H2进行测试,还有一个文件可以指定必要的别名:

CREATE SCHEMA IF NOT EXISTS History;
CREATE ALIAS IF NOT EXISTS Status_Reporting FOR "com.test.StoredProcs.statusReporting";


Alias使用的测试类

还有一个测试类,可以从SP调用中返回默认结果:

package com.test;

import com.test.StatusReportEntity;

public class StoredProcs {

    public static StatusReportEntity statusReporting(Date reportingDate) {
        StatusReportEntity statusReportEntity = StatusReportEntity.builder().withId(1).withUniqueUsers(10).build();
        return statusReportEntity;
    }
}


测试班

package com.test;

import com.test.MyListener;
import java.util.Calendar;
import org.junit.Test;
import static org.junit.Assert.*;

public class MyListenerTest {

    private MyListener listener;

    @Test
    public void listenerReturnsLatestData() throws Exception {
        MyListener myListener = new MyListener();
        assertNotNull(myListener.statusReporting(Calendar.getInstance().getTime()));
    }
}

最佳答案

CREATE TABLE PERSON( PERSON_ID IDENTITY主键,
        GIVEN_NAME VARCHAR(20),
        FIRST_NAME VARCHAR(20),MIDDLE_NAME VARCHAR(20),
        LAST_NAME VARCHAR(20),TITLE VARCHAR(20),NAME_SUFFIX VARCHAR(20));


The entity class must use any Generation strategy.


ojit_pre

10-06 13:59