关于JPA 主键,自定义生成遇到的问题,愚蠢的笔记

第一次 报错: ids for this class must be manually assigned before calling save()

在报这个问题的时候,一定去代码中去查看自己代码set设ID的逻辑,ID的值没有被设置

下面是我写的代码,可以看出,在上面setId,可是下面BeanUtils工具引用dto对象将master原值给覆盖了

PS:  以下面说明->org.springframework.beans.BeanUtils工具可以将dto对象对应master对象的字段替换,免去了一堆无聊的setter

Master master = new Master();
Master.setId(id);
Master.setAmount(amount);
BeanUtils.copyProperties(dto, master); //此处是错误的,会覆盖前面的值
repository.save(master);

补充学习, 关于Spring Data JPA提供主键生成,@GeneratorValue 4种策略以及支持的数据库

GenerationType.TABLE            ---mysql, oracle, postgresql, kingbase
GenerationType.AUTO             ---mysql, oracle, postgresql, kingbase
GenerationType.IDENTITY        ---mysql, postgresql, kingbase
GenerationType.SEQUENCE     ---oracle, postgresql, kingbase

自定义生成主键,也有策略生成器,@GenericGenerator注解,该注解要配合@GeneratorValue一起使用

@GeneratedValue注解中的generator属性和@GenericGenerator注解中name属性保持一致,strategy属性表示hibernate的主键生成策略

jar : org.hibernate:hibernate-core:5.2.16.Final的DefaultIdentifierGeneratorFactory类中的14种方式

public DefaultIdentifierGeneratorFactory() {
this.register("uuid2", UUIDGenerator.class);
this.register("guid", GUIDGenerator.class);
this.register("uuid", UUIDHexGenerator.class);
this.register("uuid.hex", UUIDHexGenerator.class);
this.register("assigned", Assigned.class);
this.register("identity", IdentityGenerator.class);
this.register("select", SelectGenerator.class);
this.register("sequence", SequenceStyleGenerator.class);
this.register("seqhilo", SequenceHiLoGenerator.class);
this.register("increment", IncrementGenerator.class);
this.register("foreign", ForeignGenerator.class);
this.register("sequence-identity", SequenceIdentityGenerator.class);
this.register("enhanced-sequence", SequenceStyleGenerator.class);
this.register("enhanced-table", TableGenerator.class);
}

注意: 使用的时候必须使用他给的字符串,比如uuid,必须是小写,UUID大写是无效的

示例: ↓↓↓

@Id
@GeneratedValue(generator = "orderId")
@GenericGenerator(name = "orderId",strategy = "uuid")
private String orderId;

----------------------------------------------

assigned before calling save & JPA id生成策略的更多相关文章

  1. JPA ID生成策略(转---)

    尊重原创:http://tendyming.iteye.com/blog/2024985 JPA ID生成策略 @Table Table用来定义entity主表的name,catalog,schema ...

  2. Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.qingmu.seller.entity.OrderMaster

    org.springframework.orm.jpa.JpaSystemException: ids for this class must be manually assigned before ...

  3. org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()

    org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before ...

  4. ids for this class must be manually assigned before calling save():

    Caused by: javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: i ...

  5. hibernate(四)ID生成策略

    一.ID生成策略配置 1.ID生成方式在xml中配置方式: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping P ...

  6. Hibernate系列之ID生成策略

    一.概述 hibernate中使用两种方式实现主键生成策略,分别是XML生成id和注解方式(@GeneratedValue),下面逐一进行总结. 二.XML配置方法 这种方式是在XX.hbm.xml文 ...

  7. Rhythmk 学习 Hibernate 03 - Hibernate 之 延时加载 以及 ID 生成策略

    Hibernate 加载数据 有get,跟Load 1.懒加载: 使用session.load(type,id)获取对象,并不读取数据库,只有在使用返回对象值才正真去查询数据库. @Test publ ...

  8. 数据库主键ID生成策略

    前言: 系统唯一ID是我们在设计一个系统的时候常常会遇见的问题,下面介绍一些常见的ID生成策略. Sequence ID UUID GUID COMB Snowflake 最开始的自增ID为了实现分库 ...

  9. 可实现的全局唯一有序ID生成策略

    在博客园搜素全局唯一有序ID,罗列出来的文章大致讲述了以下几个问题,常见的生成全局唯一id的常见方法 :使用数据库自动增长序列实现 : 使用UUID实现:  使用redis实现: 使用Twitter的 ...

随机推荐

  1. Python学习之列表--自动超市购物车

    效果图: 实现代码: menu = [0,5000,500,9000,3000,30,50,7000,70,40]name = [0,"iphone","bicycle& ...

  2. python collections 模块 之namedtuple

    namedtuple collections.namedtuple(typename, filed_name, *, rename=False, module=None) 创建一个以 typename ...

  3. 装饰者模式(Decorator、Compoment)(早餐销售装饰,动态添加职责)

    适用于以下情况: (1)需要扩展一个类的功能,或给一个类添加附加职责. (2)需要动态的给·一个对象添加功能,这些功能可以再动态的撤销. (3)需要增加由一些基本功能的排列组合而产生的非常大量的功能, ...

  4. jQuery同步/异步调用后台方法

    $.ajax({ type: "Post", url: "UserManage.aspx/SubmitPage",//页面/方法名 data: "{' ...

  5. 使用C#反射实现用户控件调用父页面方法

    using System.Reflection; MethodInfo mi = this.Page.GetType().GetMethod("GetUserName"); //该 ...

  6. VS中warning MSB8004和error MSB4018解决方案

    问题如下: warning MSB8004: Output Directory does not end with a trailing slash.  This build instance wil ...

  7. 使用 windows 批处理指令(BAT文件)进行压缩文件(zip)解压操作

    以下指令包括文件删除.复制.zip文件解压操作.使用7z指令指令进行解压操作前,需要确保 windows 的 path 系统环境变量中存在7z的安装路径. 7z的下载地址:https://www.7- ...

  8. 如何把win10自带输入法改为简体中文

    win10设置为中文简体 先找到win10的设置,然后下面按照图示操作,很简单 点击每一个红色的方框既能够到达---------->>>中文简体  目的地 2 会了吗,你个小傻瓜

  9. Python eval()函数的用法

    Python eval()函数的用法 eval(str)函数很强大,官方解释为:将字符串str当成有效的表达式来求值并返回计算结果.所以,结合math当成一个计算器很好用. eval()函数常见作用有 ...

  10. [转]深入理解客户区尺寸client

    关于元素尺寸,一般地,有偏移大小offset.客户区大小client和滚动大小scroll.前文已经介绍过偏移属性,后文将介绍scroll滚动大小,本文主要介绍客户区大小client 客户区大小 客户 ...