持久性新对象异常

时间:2019-01-30 15:01:04

标签: java hibernate exception

在Hibernate和HSQL 2.4.1 进程内模式下使用JPA,当我尝试将persist \ merge作为新客户端时出现异常。但是,当我尝试从数据库中选择客户端时,一切进展顺利。在应用程序启动时将创建并填充该表。

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

使用此日志和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)

这是我的实体类:

@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**/
}

我的 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> 

1 个答案:

答案 0 :(得分:0)

我找到了solution。只需从AUTO到IDENTITY替换生成策略。