简体   繁体   中英

GenericGenerator with package-info.java

I have an entity named as Tasit as shown below.

package tr.com.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import tr.com.MyGenerator;
    
@Entity
public class Tasit {
    
  @Id
  @GeneratedValue(generator = "ID_GENERATOR")
  private Long id;
    
  @Column(name = "TASIT_ADI")
  private String tasitAdi;
    
  public String getTasitAdi() {
     return tasitAdi;
  }
    
  public void setTasitAdi(String tasitAdi) {
    this.tasitAdi = tasitAdi;
  }
    
  public Tasit(Long tasitId, String tasitAdi) {
    super();
    this.id = tasitId;
    this.tasitAdi = tasitAdi;
  }
    
  public Tasit(String tasitAdi) {
    super();
    this.tasitAdi = tasitAdi;
  }
    
  public Long getId() {
    return id;
  }
    
  public Tasit() {
    super();
  }
}

As you see in Tasit entity there is @GeneratedValue(generator = "ID_GENERATOR") .

l add in tr.com.entity package package-info.java class. My goal was to write one Generator and to use for all entities in tr.com.entity package

My package-info.java class is also below

@org.hibernate.annotations.GenericGenerator(
   name = "ID_GENERATOR",
   strategy =  "enhanced-sequence",
   parameters = {
      @org.hibernate.annotations.Parameter(
           name = "sequence_name",
           value = "JPWH_SEQUENCE"
      ),
      @org.hibernate.annotations.Parameter(
           name = "initial_value",
           value = "1000"
      )
    }
)
package tr.com.entity;

by doing this I try to insert any data in TASIT table. But when I run the appropriate runner code to insert data to the TASIT table getting below exception.

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.hibernate.AnnotationException: Unknown Id.generator: ID_GENERATOR
    at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:639)
    at org.hibernate.cfg.AnnotationBinder.processId(AnnotationBinder.java:2287)
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2193)
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:895)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:728)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3625)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3579)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1381)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1786)
    at tr.com.hibernate.test.TasitTest.<clinit>(TasitTest.java:47)

Any help will be appreciated.

Hibernate config is below

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.id.new_generator_mappings" >true</property>
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@HP-BILGISAYAR:1521:ORCL</property>
        <property name="connection.username">hr</property>
        <property name="connection.password">hr</property>


        <property name="connection.pool_size">1</property>

         <property name="dialect">org.hibernate.dialect.Oracle8iDialect</property>

         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
         <property name="cache.use_second_level_cache">true</property>   
         <property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> 
        
        <property name="show_sql">true</property>

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


<mapping class="tr.com.entity.Tasit"/> 

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

You need to check your persistence.xml and make sure that you set

<exclude-unlisted-classes>false</exclude-unlisted-classes>

This will enable JPA to scan your package-info.java

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