简体   繁体   中英

Exception on persist new object

Using JPA with Hibernate and HSQL 2.4.1 in-process mode, I got exception when try persist\\merge are new client. But when i try select client from db everything goes well. The table is created and populated when the application starts.

Client client = new Client();
client.setFirstName("test2");
client.setSecondName("test2");
client.setPatronymic("test2");
client.setNumber("test2");
System.out.println(client);
entityManager.persist(client);

With this logs and stacktrace:

2019-01-30T18:25:36.247+0400  WARN  SQL Error: -5501, SQLState: 42501
2019-01-30T18:25:36.248+0400  ERROR  user lacks privilege or object not found: HIBERNATE_SEQUENCE in statement [call next value for hibernate_sequence]
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
    at com.haulmont.testtask.Test.main(Test.java:21)

Here my entity class:

@Entity
@Table(name = "client")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;

@Column(name = "first_name")
private String firstName;

@Column(name = "second_name")
private String secondName;

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

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

/**getters and setters**/
}

My persistance.xml

<persistence-unit name="mainPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.haulmont.testtask.data.entity.Client</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:db-data/test_task;shutdown=true"/>
        <property name="javax.persistence.jdbc.user" value="SA"/>
        <property name="javax.persistence.jdbc.password" value=""/>

        <property name="javax.persistence.schema-generation.database.action" value="create"/>
        <property name="javax.persistence.schema-generation.create-source" value="script"/>
        <property name="javax.persistence.schema-generation.create-script-source" value="sql/create.sql"/>
        <property name="javax.persistence.sql-load-script-source" value="sql/import.sql"/>

        <property name="hibernate.hbn2ddl.import_files" value="sql/import.sql"/>
        <property name="hibernate.hbm2ddl.import_files_sql_extractor"
        value="org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>


    </properties>
</persistence-unit> 

I find solution . Just need replace generate strategy from AUTO to IDENTITY.

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