4

I'm trying to add a Generator on package-info.java so that I don't have to re-write same code on top of every @Entity class. But when I try to run the program Application I get an error.

Session Factory could not be created.org.hibernate.AnnotationException: 
Unknown Id.generator: ID_GENERATOR

If I apply @GenericGenerator on top on @Entity class it works just fine. Am I missing some key point here.

I found this tread but the answer posted there didn't work for me.


Configurations

package-info.java

@org.hibernate.annotations.GenericGenerator(
    name = "ID_GENERATOR",
    strategy = "enhanced-sequence",
    parameters = {
        @org.hibernate.annotations.Parameter(
            name = "sequence_name",
            value = "JPA_SEQUENCE"
        ),
        @org.hibernate.annotations.Parameter(
            name = "initial_value",
            value = "1"
        )
    }
)
package com.example.hibernate.model;

Student.java

package com.example.hibernate.model;

@Entity
@Table(name = "student")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {

    @Id
    @GeneratedValue(generator = "ID_GENERATOR")
    private int id;

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

    @Column(name = "last_name", nullable = false)
    private String lastName;
}

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">

    <persistence-unit name="persistence" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>


        <class>com.example.hibernate.model.Student</class>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.url" value="jdbc:h2:mem:test_mem" />
            <property name="hibernate.connection.user" value="sa" />
             <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.flushMode" value="FLUSH_AUTO" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        </properties>
    </persistence-unit>
</persistence>
Community
  • 1
  • 1
A0__oN
  • 8,740
  • 6
  • 40
  • 61

2 Answers2

3

You should simply add the package that consists in itself GenericGenerator in your persistence.xml.

The example

<class>com.example.hibernate.model</class>
<class>com.example.hibernate.model.Student</class>
Hasty Bit
  • 163
  • 1
  • 1
  • 9
  • After adding package in peristence.xml file, I am getting Caused by: java.lang.ClassNotFoundException: com.example.hibernate.model. Can you please suggest, If i am missing something – mksmanjit May 09 '23 at 10:39
1

One of the solution that I found is to use @javax.persistence.SequenceGenerator on every @Entity class like

@Entity
@SequenceGenerator(name = "MY_S", sequenceName = "ID_GENERATOR")
public class Student implements Serializable {

    @Id
    @GeneratedValue(generator = "MY_S")
    private int id;

}

That's little bit of less copy and pasting to do than, to define GenericGenerator on every @Entity class.


We can also eliminate use of @SequenceGenerator in each @Entity class but we need to work with XML in this case.

Create a orm.xml and add this file as mapping-file on persistence.xml

<mapping-file>META-INF/orm.xml</mapping-file>

Inside of orm.xml we define the sequence-generator

<sequence-generator name="MY_S"
                    sequence-name="ID_GENERATOR"/>
A0__oN
  • 8,740
  • 6
  • 40
  • 61