Java org.h2.jdbc.JdbcSQLException:列";Id";在使用H2数据库进行测试期间未找到
我有一个类,它使用Hibernate会话通过JPA@NamedNativeQuery调用存储过程,使用内存中的H2数据库进行测试(实际的数据库是MySQL)。存储过程在数据库表中插入一条记录,然后返回该行 但是在测试过程中,在转换为JPA@Entity时,我看到一个H2数据库错误:org.H2.jdbc.JdbcSQLException:未找到列“Id” 我已经在下面记录了代码的简化版本 据我所知,我认为这和@Id注释的H2解释有关,但我不明白为什么,所以我非常感谢您的帮助 NB-我已经相当广泛地搜索了堆栈溢出,包括与列规范使用双引号有关的问题,但我不认为这与我的情况有关… 表格Java org.h2.jdbc.JdbcSQLException:列";Id";在使用H2数据库进行测试期间未找到,java,hibernate,jpa,annotations,h2,Java,Hibernate,Jpa,Annotations,H2,我有一个类,它使用Hibernate会话通过JPA@NamedNativeQuery调用存储过程,使用内存中的H2数据库进行测试(实际的数据库是MySQL)。存储过程在数据库表中插入一条记录,然后返回该行 但是在测试过程中,在转换为JPA@Entity时,我看到一个H2数据库错误:org.H2.jdbc.JdbcSQLException:未找到列“Id” 我已经在下面记录了代码的简化版本 据我所知,我认为这和@Id注释的H2解释有关,但我不明白为什么,所以我非常感谢您的帮助 NB-我已经相当广泛
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;
存储过程
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别名
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()));
}
}
要启用使用H2的测试,还需要一个文件来指定必要的别名:
CREATE SCHEMA IF NOT EXISTS History;
CREATE ALIAS IF NOT EXISTS Status_Reporting FOR "com.test.StoredProcs.statusReporting";
别名使用的测试类
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()));
}
}
以及用于从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()));
}
}
创建表格人员(
人员IDIDENTITY
主键,
命名为VARCHAR(20),
第一名VARCHAR(20),中间名VARCHAR(20),
姓氏VARCHAR(20),头衔VARCHAR(20),姓名后缀VARCHAR(20));
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name ="PERSON_ID")
private int personId;
请正确格式化您的问题,并请检查此项您好,您的代码格式有问题。良好的格式会增加有人阅读并回答你问题的可能性。