我们有一个最初从https://github.com/arun-gupta/lambda-rds-mysql克隆的代码。使用PostgreSQL RDS进行更改。当我们在AWS上运行此代码时,它会出现以下错误
错误消息
{
"errorMessage": "Unknown entity: org.sample.serverless.aws.rds.Employee",
"errorType": "org.hibernate.MappingException",
"stackTrace": [
"org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)",
"org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1639)",
"org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)",
"org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)",
"org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)",
"org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)",
"org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)",
"org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)",
"org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:694)",
"org.hibernate.internal.SessionImpl.save(SessionImpl.java:686)",
"org.hibernate.internal.SessionImpl.save(SessionImpl.java:681)",
"org.sample.serverless.aws.rds.EmployeeHandler.handleRequest(EmployeeHandler.java:21)",
"org.sample.serverless.aws.rds.EmployeeHandler.handleRequest(EmployeeHandler.java:11)"
]
}
以下是代码文件,您能否帮助我们理解为什么会收到此错误:
员工处理程序
public class EmployeeHandler implements RequestHandler<Request, String> {
@Override
public String handleRequest(Request request, Context context) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
try (Session session = sessionFactory.openSession()) {
session.beginTransaction();
Employee employee = new Employee();
employee.setId(request.id);
employee.setName(request.name);
session.save(employee);
session.getTransaction().commit();
}
return String.format("Added %s %s.", request.id, request.name);
}
}
的HibernateUtil
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (null != sessionFactory)
return sessionFactory;
Configuration configuration = new Configuration();
String jdbcUrl = "jdbc:postgresql://"
+ System.getenv("RDS_HOSTNAME")
+ "/"
+ System.getenv("RDS_DB_NAME");
configuration.setProperty("hibernate.connection.url", jdbcUrl);
configuration.setProperty("hibernate.connection.username", System.getenv("RDS_USERNAME"));
configuration.setProperty("hibernate.connection.password", System.getenv("RDS_PASSWORD"));
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
try {
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (HibernateException e) {
System.err.println("Initial SessionFactory creation failed." + e);
throw new ExceptionInInitializerError(e);
}
return sessionFactory;
}
员工实体
package org.sample.serverless.aws.rds;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Employee")
public class Employee implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
public Employee() {
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employee{" + "id=" + id + ", name=" + name + '}';
}
}
Hibernate CFG XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="show_sql">true</property>
<property name="connection.pool_size">1</property>
<mapping class="org.sample.serverless.aws.rds.Employee"/>
</session-factory>
</hibernate-configuration>
答案 0 :(得分:1)
在HibernateUtil类中添加以下行(configuration.configure();
之前)。
configuration.addAnnotatedClass(Employee.class);
答案 1 :(得分:0)
我尝试了这段代码并且工作了:
/*
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build()
*/
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).configure().build();
try {
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (HibernateException e) {
System.err.println("Initial SessionFactory creation failed." + e);
throw new ExceptionInInitializerError(e);
}