Hibernate HSQLDB - 无法准备语句

时间:2016-07-17 14:24:46

标签: hibernate hsqldb

我在谷歌检查输出但没有任何效果。

我的Users.java

package com.serwis.model;


import javax.persistence.*;


@Entity
@Table(name="Users")
public class Users {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name="id")
    private int id;

    @Column(name="login")
    private String login;

    @Column(name="password")
    private String password;

    @Column(name="email")
    private String email;

    @Column(name="avatar")
    private byte[] avatar;

    public Users(){}

    public Users(int id, String login, String password, String email, byte[] avatar) {
        this.id = id;
        this.login = login;
        this.password = password;
        this.email = email;
        this.avatar = avatar;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", login='" + login + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

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

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public byte[] getAvatar() {
        return avatar;
    }

    public void setAvatar(byte[] avatar) {
        this.avatar = avatar;
    }


}

我的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- JDBC connection settings -->
        <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:mem://localhost</property>
        <property name="connection.username">SA</property>
        <property name="connection.password"></property>

        <!--https://www.progress.com/jdbc/resources/tutorials/connection-pooling/connection -->
        <property name="connection.pool_size">1</property>

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

        <!-- SHOW SQL OUTPUT -->
        <property name="show_sql">true</property>

        <property name="hibernate.current_session_context_class">thread</property>



    </session-factory>
</hibernate-configuration>

我测试连接的Test.java

package com.serwis;


import com.serwis.model.Users;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;



import java.sql.SQLException;


/**
 * Created by Jodanpotasu on 2016-07-17.
 */
public class TEST {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Users.class).buildSessionFactory();

        Session session = factory.getCurrentSession();



        try{
            Users user = new Users(1,"login","password","mail",null);


            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
            System.out.println("DONEEEEE");

        }catch (Exception e){
            System.out.println("blad\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

            System.out.println(e);
        } finally {
            System.out.println("zrobiono");
            session.close();
        }





    }
}

某些信息错误

Hibernate: insert into Users (id, avatar, email, login, password) values (default, ?, ?, ?, ?)
16:05:32.255 [main] DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper - could not prepare statement [insert into Users (id, avatar, email, login, password) values (default, ?, ?, ?, ?)]
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: USERS in statement [insert into Users (id, avatar, email, login, password) values (default, ?, ?, ?, ?)]
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$2.doPrepare(StatementPreparerImpl.java:124)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:122)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.prepare(IdentityGenerator.java:89)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:55)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:492)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:197)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:216)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:334)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:289)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
    at com.sun.proxy.$Proxy8.save(Unknown Source)
    at com.serwis.TEST.main(TEST.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: USERS
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.ParserDQL.readTableName(Unknown Source)
    at org.hsqldb.ParserDQL.readRangeVariableForDataChange(Unknown Source)
    at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
    at org.hsqldb.Session.compileStatement(Unknown Source)
    at org.hsqldb.StatementManager.compile(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 38 common frames omitted
16:05:32.259 [main] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: -5501, SQLState: 42501
16:05:32.259 [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - user lacks privilege or object not found: USERS in statement [insert into Users (id, avatar, email, login, password) values (default, ?, ?, ?, ?)]

org.hibernate.exception.SQLGrammarException:无法准备语句

16:05:32.259 [main] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Releasing JDBC connection
16:05:32.259 [main] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Released JDBC connection

Test.java另一个版本

public class TEST {

    public static void main(String[] args){
        Configuration configuration = new Configuration().configure(); //.addAnnotatedClass(Users.class); //with configure("hibernate.cfx.xml still not working
        ServiceRegistry serviceRegistry
                = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();

        // builds a session factory from the service registry
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);



        try{
            Users user = new Users(1,"login","password","mail",null);

            Session session = sessionFactory.openSession();
            session.beginTransaction();
            session.save(user);


        }catch (Exception e){
            System.out.println("blad\n\n");
            System.out.println(e);
        } finally {
            System.out.println("zrobiono");
        }
}}

有一个错误

org.hibernate.MappingException: Unknown entity: com.serwis.model.Users

但是

Configuration configuration = new Configuration().configure().addAnnotatedClass(Users.class);

再多一次

16:23:53.216 [main] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: -5501, SQLState: 42501
16:23:53.216 [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - user lacks privilege or object not found: USERS in statement [insert into Users (id, avatar, email, login, password) values (default, ?, ?, ?, ?)]
blad


org.hibernate.exception.SQLGrammarException: could not prepare statement

here

1 个答案:

答案 0 :(得分:0)

我不知道我必须告诉hibernate制作表格,我认为Hibernate会自动完成。

我添加到hibernate.cfg.xml

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