我想构建一个小型控制台应用程序,它是通过学习Hibernate目的的测试来执行的。不幸的是,hibernate返回时没有数据,当列表应该被实体填充时,它会抛出outofindex异常。我假设没有结果回来。我一直在阅读这里的教程和问题,但我找不到为什么会这样。
JDBC连接运行良好,它是从DataGrip复制的。
还应该检查更详细的其他内容?对不起,我在这个世界上完全缺乏经验。
请找到以下代码。
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL95Dialect</property>
<property name="connection.url" >jdbc:postgresql://localhost:5432/digitallibraryreports</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.username">postgres</property>
<property name="connection.password">postgres</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
来源:
public class ETL {
private static SessionFactory factory;
public ETL(){
try{
factory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex){
System.err.println("Failed to create sessionfactory" + ex);
throw new ExceptionInInitializerError(ex);
}
}
public SessionFactory getSessionFactory() {
return factory;
}
}
类提取内容:
public class FeatureFetcher {
private static SessionFactory sessionFactory;
public FeatureFetcher() {
ETL etl = new ETL();
sessionFactory = etl.getSessionFactory();
}
public void fetchFeatures() {
Session session = sessionFactory.openSession();
EntityManager em = sessionFactory.createEntityManager();
try {
em.getTransaction().begin();
List<Test> testEntityList = em.createQuery("FROM Test", Test.class).getResultList();
if (testEntityList.size() > 0) {
for (Iterator<Test> iterator = testEntityList.iterator(); iterator.hasNext(); ) {
Test testEntity = (Test) iterator.next();
System.out.println("Test Entity name: " + testEntity.getName());
System.out.println("Test Entity id: " + testEntity.getId());
}
em.getTransaction().commit();
em.close();
}
} catch (HibernateException e) {
em.getTransaction().rollback();
e.printStackTrace();
} finally {
em.close();
}
}
}
实体:
@Entity
@Table(name = "test", schema = "public")
public class Test {
@javax.persistence.Id
@GeneratedValue
@Column(name = "id")
public Integer Id;
@Column(name = "name")
public String Name;
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
SQL:
CREATE TABLE public.TEST
(
Id INT PRIMARY KEY,
Name VARCHAR(255)
);
日志:
May 14, 2017 9:20:30 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
May 14, 2017 9:20:30 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
May 14, 2017 9:20:31 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
May 14, 2017 9:20:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
May 14, 2017 9:20:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/digitallibraryreports]
May 14, 2017 9:20:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=postgres, password=****}
May 14, 2017 9:20:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
May 14, 2017 9:20:31 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
May 14, 2017 9:20:31 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
May 14, 2017 9:20:31 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
May 14, 2017 9:20:31 PM org.hibernate.type.BasicTypeRegistry register
INFO: HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@6253c26
May 14, 2017 9:20:31 PM org.hibernate.hql.internal.QuerySplitter concreteQueries
WARN: HHH000183: no persistent classes found for query class: FROM Test
May 14, 2017 9:20:31 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
答案 0 :(得分:1)
您需要在SessionFactory
中注册entites。
您可以通过以下方式编辑hibernate.cfg.xml
来完成此操作:
<session-factory>
...
<mapping class="some.pack.Test" />
...
</session-factory>
您也可以使用Spring
扫描包以添加entites。或者,出于测试目的,来自here的EntityScanner
。
请删除与EntityManager
相关的所有行并使用Session
。删除它:
EntityManager em = sessionFactory.createEntityManager();
答案 1 :(得分:1)
Hibernate SessionFactory不知道你的实体。您需要将以下资源添加到标记内的hibernate.cfg.xml文件中,并使用正确的hbm.xml文件名替换Entity。
<session-factory>
.
.
.
<mapping resource="Entity.hbm.xml"/>
</session-factory>