Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4004524
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:17:02+00:00 2026-05-20T08:17:02+00:00

I defined a generator for a JPA class: <sequence-generator name=MY_SEQ allocation-size=-1 sequence-name=MY_SEQ initial-value=100000000 />

  • 0

I defined a generator for a JPA class:

<sequence-generator name="MY_SEQ" allocation-size="-1"
    sequence-name="MY_SEQ"
    initial-value="100000000" />

There are cases where I already have an ID for an entity but when I insert the Entity the Id gets generated using the generator.

Is it possible to define a generator that will only generate an Id when one does not exist?

I am using Hibernate as a JPA Provider.

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:17:02+00:00Added an answer on May 20, 2026 at 8:17 am

    I couldn’t find a way to do this in JPA so I used Hibernate EJB3 event listeners. I over rode the saveWithGeneratedId to use reflection to check the entity for an @Id annotation and then to check that field for a value. If it has a value then I call saveWithRequestedId instead. Other wise I let it generate the Id. This worked well because I can still use the sequence for Hibernate that is set up if I need an Id. The reflection might add overhead so I might change it a little. I was thinking of having a getId() or getPK() method in all entities so I don’t have to search for which field is the @Id.

    Before I used reflection I tried calling session.getIdentifier(entity) to check but I was getting TransientObjectException( “The instance was not associated with this session” ). I couldn;t figure out how to get the Entity into the session without saving it first so I gave up. Below is the listener code I wrote.

    public class MergeListener extends org.hibernate.ejb.event.EJB3MergeEventListener
     {
    
    
    
        @Override
        protected Serializable saveWithGeneratedId(Object entity, String entityName, Object anything, EventSource source, boolean requiresImmediateIdAccess) {
    
    
            Integer id = null;
    
    
                Field[] declaredFields = entity.getClass().getDeclaredFields();
    
                for (Field field : declaredFields) {
    
                    Id annotation = field.getAnnotation(javax.persistence.Id.class);
    
                    if(annotation!=null) {
    
    
                        try {
                            Method method = entity.getClass().getMethod("get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1));
                            Object invoke = method.invoke(entity);
    
                           id = (Integer)invoke;
    
    
                        } catch (Exception ex) {
                          //something failed (method not found..etc) , keep going anyway
                        }
    
                        break;
    
                    }
                }
    
    
    
           if(id == null ||
                    id == 0) {
            return super.saveWithGeneratedId(entity, entityName, anything, source, requiresImmediateIdAccess);
            } else {
    
                return super.saveWithRequestedId(entity, id, entityName, anything, source);
            }
        }
    }
    

    I then had to add the listener to my persistence.xml

     <property name="hibernate.ejb.event.merge" value="my.package.MergeListener"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NSInteger / NSUInteger are Cocoa-defined replacements for the regular built-in types. Is there any
I have defined an interface in C++, i.e. a class containing only pure virtual
In a user defined wizard page, is there a way to capture change or
I have an entity which has an ID field: @Id @Column(name = `U##ID_VOIE`) @GeneratedValue(generator
I'm using JPA 1, Hibernate and Oracle 10.2.0 and my entities are defined like
I have an entity defined with a sequence-generated primary key: @Id @GeneratedValue(strategy = GenerationType.SEQUENCE,
I have declareted the fallowing Mapping for NHibernate: <class name=Sales table=Sales lazy=false > <id
I'm trying to generate a CSS class that will help me define a page
I defined a record named log . I want to create an mnesia table
User-Defined Functions & Collating Sequences Full support for user-defined functions and collating sequences means

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.