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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:10:00+00:00 2026-05-25T23:10:00+00:00

There is an easy way to set hibernate to use different primary key ids

  • 0

There is an easy way to set hibernate to use different primary key ids for each table with postgres?
I tried to use postgres dialect in DataSource:

dialect = org.hibernate.dialect.PostgreSQLDialect 
or
dialect = net.sf.hibernate.dialect.PostgreSQLDialect

But it doesn’t work.
Thanks

  • 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-25T23:10:01+00:00Added an answer on May 25, 2026 at 11:10 pm

    The short answer is no, there isn’t a easy way to do this. However, I have found a solution that does work. Basically you need to implement a custom dialect. Here is an implementation (please note the original source of the implementation within the comments).

    package com.my.custom;
    
    import java.util.Properties;
    
    import org.hibernate.dialect.Dialect;
    import org.hibernate.dialect.PostgreSQLDialect;
    import org.hibernate.id.PersistentIdentifierGenerator;
    import org.hibernate.id.SequenceGenerator;
    import org.hibernate.type.Type;
    
    
    /**
     * Creates a sequence per table instead of the default behavior of one sequence.
     *
     * From <a href='http://www.hibernate.org/296.html'>http://www.hibernate.org/296.html</a>
     * @author Burt
     */
    public class TableNameSequencePostgresDialect extends PostgreSQLDialect {
    
        /**
         * Get the native identifier generator class.
         * @return TableNameSequenceGenerator.
         */
        @Override
        public Class<?> getNativeIdentifierGeneratorClass() {
                return TableNameSequenceGenerator.class;
        }
    
        /**
         * Creates a sequence per table instead of the default behavior of one sequence.
         */
        public static class TableNameSequenceGenerator
               extends SequenceGenerator {
    
                /**
                 * {@inheritDoc}
                 * If the parameters do not contain a {@link SequenceGenerator#SEQUENCE} name, we
                 * assign one based on the table name.
                 */
                @Override
                public void configure(
                                final Type type,
                                final Properties params,
                                final Dialect dialect) {
                        if (params.getProperty(SEQUENCE) == null
                                        || params.getProperty(SEQUENCE).length() == 0) {
                                String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE);
                                if (tableName != null) {
                                        params.setProperty(SEQUENCE, "seq_" + tableName);
                                }
                        }
                        super.configure(type, params, dialect);
                }
        }
    
    }
    

    The above implementation is should be stored as TableNameSequencePostgresDialect.java under src/java/com/my/custom within your Grails project.

    Next, update your DataSource.groovy to use this new custom dialect.

    dialect = com.my.custom.TableNameSequencePostgresDialect
    

    That’s pretty much about it. Not easy but it can be done.

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

Sidebar

Related Questions

Is there an easy way to set the volume from managed .net code?
By any chance, is there any easy way to set a default MasterView for
Is there a quick and easy way to set a connection string in Web.Config
Is there an easy way to use UIElement.Effect in WPF to shift the color
Is there an easy way to set a callback function to a new window
Is there any easy way to set the contents of a webview to an
Is there an easy way to set up a system which will copy data
Is there an easy way to set conditional breakpoints in Visual Studio? If I
I'm wondering if there's an easy way to set (or retrieve) an Objective-C property
Is there an easy way to create and Object and set properties in C#

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.