Missing sequence or table: hibernate_sequence

后端 未结 8 2266
礼貌的吻别
礼貌的吻别 2020-12-14 15:49

I am new to hibernate and postgres. Actually I am trying to map potgres database using Hibernate. This is my table stucture in postgresql

CREATE TABLE employ         


        
相关标签:
8条回答
  • 2020-12-14 16:18

    You haven't posted the important bit: the Employee class.

    But my guess is that your Employee class is using @GeneratedValue() without specifying the sequence to use. So, Hibernate uses its default name: hibernate_sequence.

    You can supply a sequence name as part of the GeneratedValue annotation. eg.

    @GeneratedValue(strategy=SEQUENCE, generator="employee_id_seq")
    
    0 讨论(0)
  • 2020-12-14 16:28

    In your domain or Model object annotate the id field as below and it should work. For me the GenerationType.AUTO failed

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    0 讨论(0)
  • 2020-12-14 16:32

    If you don't use annotation you should change YourClass.hbm.xml file.

    Your ID section should be:

    <id name="id" type="int" column="id">
         <generator class="sequence">
             <param name="sequence">employee_id_seq</param>
         </generator>
    </id>
    

    File sample:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
     "-//Hibernate/Hibernate Mapping DTD//EN"
     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
    
    <hibernate-mapping>
       <class name="Employee" table="EMPLOYEE">
          <meta attribute="class-description">
             This class contains the employee detail. 
          </meta>
          <id name="id" type="int" column="id">
             <generator class="sequence">
                 <param name="sequence">employee_id_seq</param>
             </generator>
          </id>
          <property name="firstName" column="first_name" type="string"/>
          <property name="lastName" column="last_name" type="string"/>
          <property name="salary" column="salary" type="int"/>
       </class>
    </hibernate-mapping>
    
    0 讨论(0)
  • 2020-12-14 16:34

    You can do two things. One is to just manually create a blank hibernate_sequence table postgresql. Two, most likely there is a conflict with the user account permissions not allowing grails to create that table.

    0 讨论(0)
  • 2020-12-14 16:34

    If you encounter this with Spring Boot or Spring boot/Hibernate Migration then potentially you can try the following

    https://mkyong.com/spring-boot/spring-boot-mysql-table-db_name-hibernate_sequence-doesnt-exist/
    
    0 讨论(0)
  • 2020-12-14 16:42

    Simple solution :

    create hibernate_sequence table as :

    "create sequence <schema>.hibernate_sequence"
    
    0 讨论(0)
提交回复
热议问题