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

  • SEARCH
  • Home
  • 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 3439384
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:18:13+00:00 2026-05-18T08:18:13+00:00

I’m migrating a legacy system over to use Hibernate 3. It currently generates its

  • 0

I’m migrating a legacy system over to use Hibernate 3. It currently generates its own identifiers. To keep with what the system currently does before I try and move it over to something a little better, how would I go about specifying (using annotations) my own class that will return the custom generated identifiers when an insert occurs?

Something like:

@Id
@CustomIdGenerator(Foo.class) // obviously this is not a real annotation
public String getId() { ... }

Where the Foo class has one method that generates the identifier.

Currently I’m just calling the setId(String id) method manually but was hoping for a better way to deal with this situation.

  • 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-18T08:18:14+00:00Added an answer on May 18, 2026 at 8:18 am

    I don’t think there is out-of-box support for generating custom Ids using custom annotations using pure JPA-2 API. But if you want to use provider specific API, then the job is pretty simple. Sample Example

    To be provider independent try any of following tricks….

    IdGeneratorHolder

    public abstract class IdGeneratorHolder {
        /* PersistentEntity is a marker interface */
        public static IdGenerator getIdGenerator(Class<? extends PersistentEntity> entityType) {
                 /* sample impelementation */
            if(Product.class.isAssignableFrom(entityType)) {
                return new ProductIdGenerator();
    
            }
            return null;
        }
    }
    

    General IdGenerator interface

    public interface IdGenerator {
        String generate();
    }
    

    Specific IdGenerator – Product Id Generator

    public class ProductIdGenerator implements IdGenerator {
        public String generate() {
                /* some complicated logic goes here */
            return ${generatedId};
        }
    }
    

    Now set the generated id either in no-arg constructor OR in @PrePersist method.

    Product.java

    public class Product implements PersistentEntity {
    
        private String id;
    
        public Product() {
            id = IdGeneratorHolder.getIdGenerator(getClass()).generate();
        }
    
        @PrePersist
        public void generateId() {
            id = IdGeneratorHolder.getIdGenerator(getClass()).generate();
        }
    
    }
    

    In above example all the ids are of the same type i.e. java.lang.String. If the persistent entities have ids of different types…..

    IdGenerator.java

    public interface IdGenerator {
        CustomId generate();
    }
    

    CustomId.java

       public class CustomId {
    
        private Object id;
    
        public CustomId(Object id) {
            this.id = id;
        }
    
        public String  toString() {
            return id.toString();
        }
        public Long  toLong() {
            return Long.valueOf(id.toString());
        }
    }
    

    Item.java

    @PrePersist
        public void generateId() {
            id = IdGeneratorHolder.getIdGenerator(getClass()).generate().toLong();
        }
    

    You can also use your custom annotation…

    CustomIdGenerator.java

    public @interface CustomIdGenerator {
        IdStrategy strategy();
    }
    

    IdStrategy.java

      enum IdStrategy {
            uuid, humanReadable,    
        }
    

    IdGeneratorHolder.java

    public abstract class IdGeneratorHolder {
        public static IdGenerator getIdGenerator(Class<? extends PersistentEntity> entityType) {
            try { // again sample implementation
                Method method = entityType.getMethod("idMethod");
                CustomIdGenerator gen = method.getAnnotation(CustomIdGenerator.class);
                IdStrategy strategy = gen.strategy();
                return new ProductIdGenerator(strategy);
            }
    

    One more thing…. If we set id in @PrePersist method, the equals() method cannot rely on id field (i.e. surrogate key), we have to use business/natural key to implement equals() method. But if we set id field to some unique value (uuid or “app-uid” unique within application) in no-arg constructor, it helps us to implement the equals() method.

    public boolean equals(Object obj) {
            if(obj instanceof Product) {
                Product that = (Product) obj;
                return this.id ==that.id;
            }
            return false;
        }
    

    If we or someone else call (intentionally or by mistake) the @PrePersist annotated method more than one times, the “unique id will be changed!!!” So setting id in no-arg constructor is preferable. OR to address this issue put a not null check…

      @PrePersist
        public void generateId() {
            if(id != null)
                id = IdGeneratorHolder.getIdGenerator(getClass()).generate();
        }
    }
    

    UPDATE

    If we put the id generation in a
    no-arg constructor, wouldn’t that
    cause a problem when loading entities
    from the database? because hibernate
    will call the no-arg constructor
    causing existing ids to be
    re-generated

    Yeah you are right, I missed that part. 🙁 Actually, I wanted to tell you that:- in my application every Entity object is associated with an Organization Entity; so I’ve created an abstract super class with two constructors, and every Entity (except Organization) extends this class.

        protected PersistentEntityImpl() {
        }
    
        protected PersistentEntityImpl(Organization organization) {
            String entityId = UUIDGenerator.generate();
            String organizationId = organization.getEntityId();
            identifier = new EntityIdentifier(entityId, organizationId);
        }
    

    The no-arg constructor is for JPA provider, we never invoke no-arg constructor, but the other organization based constructor. As you can see. id is assigned in Organization based constructor. (I really missed this point while writing the answer, sorry for that).

    See if you can implement this or similar strategy in your application.

    The second option was using the
    @PrePersist annotation. I put that in
    and the method never got hit and gave
    me an exception stating that I needed
    to set the id manually. Is there
    something else I should be doing?

    Ideally, JPA provider should invoke @PrePersist methods (one declared in class and also all the other methods that are declared in super-classes) before persisting the entity object. Can’t tell you what is wrong, unless you show some code and console.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.