Я новичок в спящем режиме, и я сталкиваюсь с ошибкой при попытке использовать Hibernate для выбора данных из таблицы. Я попробовал два подхода для этого и столкнулся со следующими ошибками.
Подход 1
private static void queryPerson(Session session) {
String SQL_QUERY ="Select person.ID as ID,person.NAME as NAME ,person.SURNAME AS SURNAME,person.ADDRESS as ADDRESS From person";
Query query = session.createQuery(SQL_QUERY);
for(Iterator it=query.iterate();it.hasNext();){
Object[] row = (Object[]) it.next();
System.out.println("ID: " + row[0]);
System.out.println("Name: " + row[1]);
System.out.println("Amount: " + row[2]);
session.getTransaction().commit();
}
При этом выдается следующая ошибка:
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: person is not mapped [Select person.ID as ID,person.NAME as NAME ,person.SURNAME AS SURNAME,person.ADDRESS as ADDRESS From person]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:327)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3441)
Подход 2:
private static void queryPerson(Session session) {
Query query = session.createQuery("from Person");
List <Person>list = query.list();
java.util.Iterator<Person> iter = list.iterator();
while (iter.hasNext()) {
Person person = iter.next();
System.out.println("Person: \"" + person.getName() +"\", " + person.getSurname() +"\", " +person.getAddress());
session.getTransaction().commit();
}
Выдает следующую ошибку:
Caused by: org.postgresql.util.PSQLException: ERROR: column person0_.id does not exist
Position: 8
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
Itбыло бы здорово, если бы кто-то мог указать, где я иду не так.Спасибо!РЕДАКТИРОВАТЬ: Person.hbm.xml
<class name="com.sample.Person" table="Person">
<id name="id" column="ID">
<generator class="native" />
</id>
<property name="name">
<column name="NAME" length="16" not-null="false" />
</property>
<property name="surname">
<column name="SURNAME" length="16" not-null="false" />
</property>
<property name="address">
<column name="ADDRESS" length="16" not-null="false" />
</property>
</class>
hibernate.cfg.xml
<!-- hibernate dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/testDB</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.show_sql">true</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- Automatic schema creation (begin) === -->
<property name="hibernate.hbm2ddl.auto">validate</property>
<!-- Simple memory-only cache -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- ############################################ -->
<!-- # mapping files with external dependencies # -->
<!-- ############################################ -->
<mapping resource="com/sample/Person.hbm.xml" />
</session-factory>
РЕДАКТИРОВАТЬ: Вот класс Person, который я использую:
package com.sample;
public class Person {
Long id;
String name;
String surname;
String address;
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}