休眠& PostgreSQL与Grails [英] Hibernate & postgreSQL with Grails
问题描述
有一种简单的方法可以设置hibernate为每个使用postgres的表使用不同的主键ID?
我试图在DataSource中使用postgres方言:
pre $ diag = org.hibernate.dialect.PostgreSQLDialect
或
dialect = net.sf.hibernate.dialect.PostgreSQLDialect
但它没有工作。
感谢
简短的回答是否定的,没有简单的方式去做这个。但是,我找到了一个可行的解决方案。基本上你需要实现一个自定义的方言。这是一个实现(请注意注释中实现的原始来源)。
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;
/ **
*为每个表创建一个序列,而不是一个序列的默认行为。
*
* From< a href ='http://www.hibernate.org/296.html'> http://www.hibernate.org/296.html< / a>
* @author Burt
* /
public class TableNameSequencePostgresDialect extends PostgreSQLDialect {
$ b $ / **
*获取本地标识符生成器类。
* @return TableNameSequenceGenerator。
* /
@Override
public Class<?> getNativeIdentifierGeneratorClass(){
return TableNameSequenceGenerator.class;
}
/ **
*为每个表格创建一个序列,而不是一个序列的默认行为。
* /
public static class TableNameSequenceGenerator
extends SequenceGenerator {
$ b $ / **
* {@inheritDoc}
*如果参数不包含序列发生器序列名称,我们
*根据表名称分配一个名称。
* /
@Override
public void configure(
final类型,
最终属性params,
最终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);
}
}
}
以上在您的Grails项目中,实现应该作为 TableNameSequencePostgresDialect.java
存储在 src / java / com / my / custom
下。
接下来,更新您的 DataSource.groovy
以使用这种新的自定义方言。
dialect = com.my.custom.TableNameSequencePostgresDialect
这很重要。不是轻松,但可以完成。
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
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.
这篇关于休眠& PostgreSQL与Grails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!