等效于 <generator class="native"></generator> 使用 MySQL 和 Hibernate3 注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1212535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:46:02  来源:igfitidea点击:

The equivalent of <generator class="native"></generator> using MySQL and Hibernate3 annotations

mysqlhibernateannotations

提问by Guy Gavriely

Starting a new project I'd like to use Hibernate annotations with MySQL instead of the configuration files I've used so far. And I can't seem to find the equivalent of:

开始一个新项目 我想在 MySQL 中使用 Hibernate 注释,而不是我到目前为止使用的配置文件。而且我似乎找不到相当于:

    <id name="id" type="long" >
        <generator class="native"></generator>
    </id>

I tried using:

我尝试使用:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "native")
private long id;

but got:

但得到:

org.hibernate.AnnotationException: Unknown Id.generator: native

or:

或者:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

Give me:

给我吗:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: PROCEDURE projectName.identity does not exist

Does anyone successfully deployed MySQL and Hibernate3 annotations for automatically generating ids?

有没有人成功部署 MySQL 和 Hibernate3 注释以自动生成 id?

采纳答案by hobodave

Prior to version 5.0, using the strategy AUTOwas the equivalent of using nativein a mapping. This used the LegacyFallbackInterpreter:

在 5.0 版之前,使用该策略AUTO相当于native在映射中使用。这使用了LegacyFallbackInterpreter

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
    return id;
}

Since Hibernate 5.0, the default interpreter is the FallbackInterpeterwhich will either use a SEQUENCE generator or TABLE generator depending on the underlying database.

从 Hibernate 5.0 开始,默认解释器是FallbackInterpeter根据底层数据库使用 SEQUENCE 生成器或 TABLE 生成器的解释器。

To use the LegacyFallbackInterpreter, set hibernate.id.new_generator_mappingsto false.

要使用LegacyFallbackInterpreter,请设置hibernate.id.new_generator_mappingsfalse

回答by wwadge

You might wish to have a look at: http://hibernatepojoge.sourceforge.net/

您可能希望查看:http: //hibernatepojoge.sourceforge.net/

回答by Rob Di Marco

Try using @GeneratedValue(strategy=GenerationType.AUTO). That should use the MySQL autonum functionality.

尝试使用@GeneratedValue(strategy=GenerationType.AUTO). 那应该使用 MySQL autonum 功能。