0
votes

I'm quite new to hibernate so my problem can be obvious to you. I created database in H2 Console with 3 tables (data.mv.db), insert some values and then copied it to my database folder in project path. Now I'm trying to read one of them for tests, but always have error that table not exists. I've tried to add properties to connection url like connection delay or not changing letter to upper case but still the same.

Error:

Caused by: org.h2.jdbc.JdbcSQLException: Tabela "LOGIN_DATA" nie istnieje Table "LOGIN_DATA" not found; SQL statement: select login_data0_.ID as ID1_0_, login_data0_.USERNAME as USERNAME2_0_, login_data0_.PASSWORD as PASSWORD3_0_, login_data0_.FAVOURITE as FAVOURIT4_0_ from PUBLIC.LOGIN_DATA login_data0_ [42102-192] at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) at org.h2.message.DbException.get(DbException.java:179) at org.h2.message.DbException.get(DbException.java:155) at org.h2.schema.Schema.getTableOrView(Schema.java:437) at org.h2.command.Parser.readTableOrView(Parser.java:5371) at org.h2.command.Parser.readTableFilter(Parser.java:1257) at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1896) at org.h2.command.Parser.parseSelectSimple(Parser.java:2044) at org.h2.command.Parser.parseSelectSub(Parser.java:1890) at org.h2.command.Parser.parseSelectUnion(Parser.java:1709) at org.h2.command.Parser.parseSelect(Parser.java:1697) at org.h2.command.Parser.parsePrepared(Parser.java:445) at org.h2.command.Parser.parse(Parser.java:317) at org.h2.command.Parser.parse(Parser.java:289) at org.h2.command.Parser.prepareCommand(Parser.java:254) at org.h2.engine.Session.prepareLocal(Session.java:560) at org.h2.engine.Session.prepareCommand(Session.java:501) at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1201) at org.h2.jdbc.JdbcPreparedStatement.(JdbcPreparedStatement.java:73) at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:289) at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:145) at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:171) ... 17 more

Hibernate Config:

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.h2.Driver</property>
        <property name="connection.url">jdbc:h2:file:~/database/data</property>
        <property name="connection.username">admin</property>
        <property name="connection.password">1234</property>

        <property name="hibernate.default_schema">PUBLIC</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <mapping class="data.Login_data"/>
    </session-factory>

</hibernate-configuration>

Login_data class:

@Entity
@Table(name = "LOGIN_DATA")
public class Login_data {

    private int id;
    private String name;
    private String password;
    private String values;

    public Login_data() {
        // this form used by Hibernate
    }

    public Login_data(int id, String name, String password, String values) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.values = values;
    }

    @Id
    @GeneratedValue
    @Column(name = "ID")
    public int getId() {
        return id;
    }

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

    @Column(name = "USERNAME")
    public String getName() {
        return name;
    }

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

    @Column(name = "PASSWORD")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "FAVOURITE")
    public String getValues() {
        return values;
    }

    public void setValues(String values) {
        this.values = values;
    }
}

Main class:

public static void main(String[] args){
            Login_data user = new Login_data();
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            Session session = sessionFactory.openSession();



                /*session.beginTransaction();
                session.save(user);
                session.getTransaction().commit();*/

                // now lets pull events from the database and list them
                session.beginTransaction();
                List result = session.createQuery("from Login_data").list();
                for (Login_data event : (List<Login_data>) result) {
                    System.out.println(event.getName());
                }
                System.out.print(result);
                session.getTransaction().commit();
                session.close();

        }

I've tried to edit configuration. So I add:

 <property name="hibernate.hbm2ddl.auto">update</property>

And make empty database. What is strange is database and table created by Java after opening in H2 Console is not visible. So that can be the problem. I wanted to put a lot of data, so how to make it not from java to be visible there?

1
Don't you think that showing actual error would be helpful? - Antoniossss
Caused by: org.h2.jdbc.JdbcSQLException: Tabela "LOGIN_DATA" nie istnieje Table "LOGIN_DATA" not found; SQL statement: and long list of exceptions - kondi006
You copied it to my database folder in project path, but expect to find the data in ~/database/data. That's not your "project path". That's the directory database under your home directory. h2database.com/html/features.html#database_url - JB Nizet
Yes. You're right. My mistake. Thank you very much ! Does url with "mem" means that I can use relative location? I want to have this database in runnable application, so the best for me would if I can set location inside "src" folder. - kondi006
mem means "in memory". So your data would never be loaded from a file or saved to a file. The src folder is for your source files. Data is not source files. It has nothing to do in your src folder. Not to mention that, once you'll actually install your application on end users machines, they won't have any src folder. The documentation explains what kind of path you can provide, and has examples. Read it. - JB Nizet

1 Answers

0
votes

Thanks to @JB Nizet I see my error. I was copying database to project path, where my path "~/database" shows to database in user folder, and I was copying it to project folder so I should use "./database" . Still if I want to make runnable I would need to use user location to have it rightly working on other machines.