简体   繁体   English

如何自定义使用Hibernate的hbm2ddl生成的DDL(使用批注)?

[英]How can I customize the DDL generated with Hibernate's hbm2ddl (using annotations)?

Here's what I want to do: 这是我想做的:
I'm using Hibernate (3.3.2) to map my Ingres 10 database. 我正在使用Hibernate(3.3.2)映射我的Ingres 10数据库。 My java entities are generated after a meta model, so we decided to use annotations to ease things. 我的Java实体是在元模型之后生成的,因此我们决定使用注释来简化操作。
What we want to do after the code generation is generate the DDL instructions to create the database so we use the hbm2ddl tool and we have something like: 代码生成之后,我们要执行的操作是生成DDL指令以创建数据库,因此我们使用hbm2ddl工具,并且具有类似以下内容:

drop table xxx;
create table xxx ...;

What I miss here are extra SQL statement to eg add privileges on tables, something like: 我在这里想念的是多余的SQL语句,例如在表上添加特权,例如:

drop table xxx;
create table xxx ...;
grant xxx on table xxx;

I know I could use something called database-object to generate such statements but I think it's only available with the XML mapping. 我知道我可以使用称为数据库对象的东西来生成这样的语句,但是我认为它仅适用于XML映射。 Can you confirm this? 你能确认吗?
If that is confirmed, do you see a better solution for doing such a thing? 如果证实了这一点,您是否认为这样做的更好解决方案?
Many thanks to all. 非常感谢所有人。

A little late, and not the prettiest solution, but here's a quick and dirty bash script to run in conjunction with maven. 有点晚了,不是最漂亮的解决方案,但这是一个快速且肮脏的bash脚本,可与maven一起运行。 Hopefully it helps someone else grappling with this issue. 希望它可以帮助其他人解决这个问题。

#!/bin/bash

SQL_FILE=$1
GROUP=$2
COMPILED=$1.tmp
SCHEMA_DROP=$3
if [ "$3" == "" ]; then
    SCHEMA_DROP="true"
fi


mvn hibernate3:hbm2ddl -Dschema.file=$SQL_FILE -Dschema.drop=$SCHEMA_DROP

if [ "$SQL_FILE" == "" ] || [ "$GROUP" == "" ] ; then
    echo "Usage: $0 {SQL_FILE} {GROUP} [DROP_SCHEMA]"
    echo "Where: "
    echo "SQL_FILE: path to sql file relative to the root of the project"
    echo "GROUP: the predefined database group on which to grant access"
    echo "DROP_SCHEMA: true|false per the hbm2ddl 'drop' parameter.  Defaults to true"
    echo "NOTE: In order for this to work properly, the pom configuration of the hibernatetool should be parameterized.  For example:"
    echo '<hbm2ddl outputfilename="../../../${schema.file}" format="true" drop="${schema.drop}" />'
    exit;
fi

echo "" > $COMPILED
GRANT=""
while read line
do
    echo $line >> $COMPILED
    if [[ $line =~ .*create.table.([A-Za-z_-]*) ]]; then
        GRANT="$GRANT\ngrant all on table ${BASH_REMATCH[1]} to group $GROUP;" >> $COMPILED 
    fi
done < $SQL_FILE

echo -e $GRANT >> $COMPILED

mv $COMPILED $SQL_FILE

I like to put my schema file into a src directory for check-in, such as src/main/db/myschema.sql, hence the dir up values in the outputfilename attribute (see the NOTE in the usage comments). 我喜欢将架构文件放入src目录中以进行签入,例如src / main / db / myschema.sql,因此将outputfilename属性中的dir up值(请参阅用法注释中的NOTE)。 To run this script with file name "genSchema.sh" from the root of the project in cygwin: 要从cygwin中的项目根目录以文件名“ genSchema.sh”运行此脚本:

./genSchema.sh src/main/db/myschema.sql mygroup

The regexp and generated grant lines are for the postgresql dialect. regexp和生成的授权行用于PostgreSQL方言。 They'd probably need a slight modification for other dialects. 他们可能需要对其他方言稍作修改。

I don't know Hibernate (only NHibernate) that much and I don't know these annotations stuff very well. 我不太了解Hibernate(只有NHibernate),也不十分了解这些注释。 But I'm pretty sure that this is only available in XML mapping files. 但是我很确定这仅在XML映射文件中可用。

You may find a way to combine annotations and XML mappings. 您可能会找到一种组合注释和XML映射的方法。 If this is not possible, consider to switch to XML entirely, it is generally more powerful and you get more control. 如果无法做到这一点,请考虑完全转换为XML,它通常功能更强大,您可以获得更多控制权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM