简体   繁体   中英

Problems with Hibernate ORM in H2

I have a project which uses Hibernate and H2 Database. There's an H2-database file in the project directory. This DB file contains a table, Person. I can insert or remove data to/from this table using the H2 console, But the problem is that when I try to get an object from this table by Hibernate framework I will get this error:

Caused by: org.h2.jdbc.JdbcSQLException: Table "PERSON" not found; SQL statement:
select person0_.ID as ID1_0_0_, person0_.NAME as NAME2_0_0_, person0_.PHONE_NUMBER as PHONE_NU3_0_0_ from person person0_ where person0_.ID=? [42102-197]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.command.Parser.readTableOrView(Parser.java:5920)
    at org.h2.command.Parser.readTableFilter(Parser.java:1430)
    at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:2138)
    at org.h2.command.Parser.parseSelectSimple(Parser.java:2287)
    at org.h2.command.Parser.parseSelectSub(Parser.java:2133)
    at org.h2.command.Parser.parseSelectUnion(Parser.java:1946)
    at org.h2.command.Parser.parseSelect(Parser.java:1919)
    at org.h2.command.Parser.parsePrepared(Parser.java:463)
    at org.h2.command.Parser.parse(Parser.java:335)
    at org.h2.command.Parser.parse(Parser.java:307)
    at org.h2.command.Parser.prepareCommand(Parser.java:278)
    at org.h2.engine.Session.prepareLocal(Session.java:611)
    at org.h2.engine.Session.prepareCommand(Session.java:549)
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1247)
    at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:76)
    at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:304)

Here is the hibernate.cfg.xml file:

<hibernate-configuration xmlns="http://www.hibernate.org/xsd/orm/cfg">
    <session-factory>
        <property name="connection.url">jdbc:h2:C:\Users\Amir\Desktop\hibernate\src\main\resources\test</property>
        <property name="connection.driver_class">org.h2.Driver</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>
        <property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

, Person POJO:

@Entity
@Table(name = "person")
public class Person {

    @Id
    @Column(name = "ID")
    private String id;
    @Column(name = "NAME")
    private String name;
    @Column(name = "PHONE_NUMBER")
    private String phoneNumber;

    public Person() {}

    public Person(String name, String id, String phoneNumber) {
        this.name = name;
        this.id = id;
        this.phoneNumber = phoneNumber;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return this.id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; }

    public String getPhoneNumber() {
        return this.phoneNumber;
    }

    @Override
    public String toString() {
        return "Person {Name= " + name + ", ID= " + id + ", Phone number= " + phoneNumber + "}";
    }

}

And finally, hibernate connection configuration class:

public class PersonDAO {
    private static SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new Configuration().configure().addAnnotatedClass(Person.class).buildSessionFactory();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public PersonDAO() {

    }

    public static void insertRecord(Person person) {
        try (Session session = sessionFactory.openSession()) {
            session.beginTransaction();
            session.save(person);
            session.getTransaction().commit();
        }
    }

    public static Person getRecord(String id) {
        try (Session session = sessionFactory.openSession()) {
            return session.find(Person.class, id);
        }
    }
}

Any idea about this problem? Thanks.

Check capitalization of your table name. You annotated @Table(name = "person") and you report that table Person exists. As far as I remember, H2 uses case sensitive table names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM