简体   繁体   English

全局配置Hibernate以使用GenerationType.IDENTITY

[英]Configure Hibernate globally to use GenerationType.IDENTITY

Since Spring Boot 2 use Hibernate 5 the @GeneratedValue default strategy GenerationType.AUTO for my MySQL 5.7 database results in GenerationType.SEQUENCE emulated in a separate table, since MySQL 5.7 doesn't support sequences. 由于Spring Boot 2使用了Hibernate 5,我的MySQL 5.7数据库的@GeneratedValue默认策略GenerationType.AUTO导致GenerationType.SEQUENCE在单独的表中模拟,因为MySQL 5.7不支持序列。

I want all of my tables' primary ids to be generated using GenerationType.IDENTITY . 我希望所有表的主要ID均使用GenerationType.IDENTITY

Is there a global way to set this to be the default strategy, so I don't explicitly have to select the GenerationType.IDENTITY strategy each time I use @GeneratedValue on a field? 是否有全局方法将其设置为默认策略,所以我不必每次在字段上使用@GeneratedValue时都必须明确选择GenerationType.IDENTITY策略?

An easy way to use the same generator by all your entities is to have a @MappedSuperclass that defines the @Id field and uses what generation strategy you wish, then extending that class in your entities. 所有实体使用同一生成器的一种简单方法是拥有一个@MappedSuperclass ,它定义@Id字段并使用所需的生成策略,然后在您的实体中扩展该类。

You can additionally define other attributes besides the primary key you want your entities to have. 除了您想要实体拥有的主键之外,您还可以定义其他属性。 You can also define additional @MappedSuperclass classes if you want to have different "types" of entities, ie ones with just the pk defined, or with additional fields like created or updated . 您也可以定义附加@MappedSuperclass如果你想有不同的实体的“类型”类,即那些只pk的定义,或像其他字段createdupdated

@MappedSuperclass
public class PKEntity {

    @Id
    @GenericGenerator(name="universal", etc. etc. etc.)
    @GeneratedValue(generator="universal")
    private Long id;

    // Possibly more common columns your entities have
}

You could try creating your own annotation and using it instead of @GeneratedValue 您可以尝试创建自己的注释并使用它代替@GeneratedValue

import javax.persistence.GenerationType;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomGeneratedValue {
    GenerationType strategy() default GenerationType.IDENTITY;

    String generator() default "";
}

暂无
暂无

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

相关问题 Hibernate GenerationType.IDENTITY 不生成序列 ID - Hibernate GenerationType.IDENTITY not generating sequence ids 休眠中的 GenerationType.AUTO 与 GenerationType.IDENTITY - GenerationType.AUTO vs GenerationType.IDENTITY in hibernate Hibernate GenerationType.IDENTITY vs GenerationType.SEQUENCE - Hibernate GenerationType.IDENTITY vs GenerationType.SEQUENCE Hibernate和PostgreSQL:是否可以配置GenerationType.IDENTITY,其中每个id列的默认值来自共享序列? - Hibernate and PostgreSQL: Can I configure GenerationType.IDENTITY, where each id column's default value comes from a shared sequence? 注释@Id 和@GeneratedValue(strategy = GenerationType.IDENTITY) 有什么用? 为什么世代类型是身份? - what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity? 为什么在使用策略GenerationType.IDENTITY时Hibernate尝试访问hibernate_sequence? - Why is Hibernate trying to access hibernate_sequence when using strategy GenerationType.IDENTITY? @ GenerationType.IDENTITY失败10以上 - @GenerationType.IDENTITY failing above 10 Hibernate 1.0 + JPA + Netbeans 7.01 + SQL Server 2005 =使用GenerationType.IDENTITY插入表中的问题 - Hibernate 1.0 + JPA + Netbeans 7.01 + Sql server 2005 = Problems to insert into table with GenerationType.IDENTITY JPA GenerationType.IDENTITY 不考虑 MS SQL 中具有自动增量的列 - JPA GenerationType.IDENTITY not considering column with auto increment in MS SQL JPA 和 MS SQL GenerationType.Identity 始终为空 - JPA and MS SQL GenerationType.Identity always null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM