PostgreSQL
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> PostgreSQL

Hibernate &postgreSQL với Grails

Câu trả lời ngắn gọn là không, không có gì dễ dàng cách để làm điều này. Tuy nhiên, tôi đã tìm thấy một giải pháp có hiệu quả. Về cơ bản, bạn cần triển khai một phương ngữ tùy chỉnh. Đây là cách triển khai (vui lòng ghi rõ nguồn gốc của quá trình triển khai trong phần nhận xét).

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

}

Việc triển khai ở trên phải được lưu trữ dưới dạng TableNameSequencePostgresDialect.java trong src/java/com/my/custom trong dự án Grails của bạn.

Tiếp theo, cập nhật DataSource.groovy của bạn để sử dụng phương ngữ tùy chỉnh mới này.

dialect = com.my.custom.TableNameSequencePostgresDialect

Đó là khá nhiều về nó. Không dễ dàng nhưng nó có thể được thực hiện.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Làm cách nào để truy cập postgresql trong Docker bằng sqlalchemy?

  2. Heroku Rails Rake Task để Sync Production &Local DB

  3. Làm cách nào để tránh điều kiện chủng tộc khi sử dụng phương thức find_or_create của DBIx ::Class ::ResultSet?

  4. Trình kích hoạt PostgreSQL không trả về bất kỳ thứ gì

  5. Nhà cung cấp OleDB / ODBC tốt cho PostgreSQL