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 9265323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:02:37+00:00 2026-06-18T14:02:37+00:00

I have a new object. I want to know id before saving it. Is

  • 0

I have a new object. I want to know id before saving it. Is it possible? Or there is another way for this? I am using jpa as orm and oracle as database.

@Id
@Basic(optional = false)
@Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "woTypeSeq")
private Long id;

I have a code field in my entity. If the user doesn’t enter a value for the code field, I want to set code as entity’s id. If I persist entity, of course i can get id and set code value as id, but this is extra query database.

I want to do something like that

if(entity.getCode()==null) {
   entity.setCode(entity.getId);
   jpaDao.saveOrUpdate(entity);
}
  • 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-06-18T14:02:38+00:00Added an answer on June 18, 2026 at 2:02 pm

    After long researching about this, finally I found a solution.

    In fact, if you use sequence generator in jpa, certainly you cannot obtain entity’s id before saving it in database, because next id will be assigned by database sequence.

    There is one way to obtain the id if you use a custom generator, you can get the id before saving. Here is simple implementation:

    public class CustomGenerator extends IdentityGenerator implements Configurable {
    
        private IdentifierGenerator defaultGenerator;
    
        public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
            Long idValue = (Long)defaultGenerator.generate(session, object);
            //idValue will be assigned your entity id
            return idValue;
        }
    
        @Override
        public void configure(Type type, Properties params, Dialect d) throws MappingException {
            DefaultIdentifierGeneratorFactory dd = new DefaultIdentifierGeneratorFactory();
            dd.setDialect(d);
            defaultGenerator = dd.createIdentifierGenerator("sequence", type, params);
        }
    }
    

    Using CustomGenerator for id field:

    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GenericGenerator(name = "seq_id", strategy = "com.yoncabt.abys.core.listener.CustomGenerator", parameters = { @Parameter(name = "sequence", value = "II_FIRM_DOC_PRM_SEQ") })
    @GeneratedValue(generator = "seq_id")
    private Long id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible in jax-ws to have a webmethod that creates a new object
Is it possible to have Alpha component for object._colors ? var object = new
I have the follwoing Components: patientDiseasesStorage = new Object() patientDiseasesStorage['p158246547'] = [1, 3, 8,
I have to be able to store data in a new object, that I
I have to be able to store data in a new object, that I
I have an array an a timer that adds a new object to my
I have the following Powershell code: Add-Type -Assembly System.Configuration [ExeConfigurationFileMap] $configMap = New-Object ExeConfigurationFileMap
i have a simple if condition as follows... $site = new-object Microsoft.SharePoint.SPSite($url) $sitelists =
When adding a new Entity Framework object to a database, how could one have
Hello stackoverflow community. I want to know, if there is a possibility in OpenJPA

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.