【问题标题】:org.h2.jdbc.JdbcSQLException: Column "Id" not found during testing with H2 Databaseorg.h2.jdbc.JdbcSQLException:在使用 H2 数据库进行测试期间未找到列“Id”
【发布时间】:2015-12-25 14:44:35
【问题描述】:

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

但是在测试期间,在转换为 JPA @Entity 时,我看到 H2 数据库错误:org.h2.jdbc.JdbcSQLException: Column "Id" not found

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

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

注意 - 我已经相当广泛地搜索了堆栈溢出,包括与使用双引号进行列规范有关的问题,但不要认为这与我的情况有关......

表格

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";

别名使用的测试类

还有一个从 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()));
    }
}

【问题讨论】:

标签: java hibernate jpa annotations h2


【解决方案1】:

CREATE TABLE PERSON( PERSON_ID IDENTITY PRIMARY KEY, 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.

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)

@Column(name ="PERSON_ID")
private int personId;

【讨论】:

    猜你喜欢
    • 2013-05-24
    • 2016-02-10
    • 2018-12-31
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 2018-06-02
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多