org.h2.jdbc.JdbcSQLException:在使用H2数据库进行测试期间未找到列“Id”

时间:2015-09-28 11:14:12

标签: java hibernate jpa annotations h2

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

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

我已经记录了下面代码的缩减版本。

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

NB - 我已经相当广泛地搜索了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;

实体     package 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()));
    }
}

1 个答案:

答案 0 :(得分:0)

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

IDENTITY

The entity class must use any Generation strategy.

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)

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