休眠和postgreSQL 与 Grails

发布于 2024-12-06 22:23:44 字数 228 浏览 0 评论 0原文

有一种简单的方法可以设置hibernate为postgres的每个表使用不同的主键id吗? 我尝试在数据源中使用 postgres 方言:

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

但它不起作用。 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻安 2024-12-13 22:23:44

简短的回答是否定的,没有简单的方法可以做到这一点。不过,我找到了一个可行的解决方案。基本上您需要实现自定义方言。这是一个实现(请注意注释中实现的原始来源)。

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);
            }
    }

}

上述实现应作为 TableNameSequencePostgresDialect.java 存储在 Grails 项目中的 src/java/com/my/custom 下。

接下来,更新您的 DataSource.groovy 以使用这个新的自定义方言。

dialect = com.my.custom.TableNameSequencePostgresDialect

差不多就是这样了。不容易容易,但可以做到。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文