当前位置: 首页 > news >正文

如何在@GenericGenerator中显式指定schema

现在的情况是,在MySQL中有db1和db2两个数据库。项目使用Hibernate,可同时访问db1和db2,默认数据库为db1。表table2在db2中。且table2的主键名为ids,是自增长字段(Auto Increment)。

table2和ids的定义为:

@Entity
@Table(name = "table2", schema = "db2")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Table2 implements java.io.Serializable {private static final long serialVersionUID = 48L;@Id@Column(name = "ids")@GeneratedValue(generator = "idGenerator", strategy = GenerationType.IDENTITY)@GenericGenerator(name = "idGenerator", strategy = "increment")private Integer ids;

当向table2中保存数据时,会报错。原因是生成ids时,系统会查询table2中ids的最大值。语句是:

select max(ids) from table2

由于默认数据库是db1,因此查询的是db1.table2表。但table2表实际上在db2中,所以系统找不到该表,从而报错。

这就需要在@GenericGenerator中指定max查询语句的schema。通过检查错误信息,发现对应代码在org.hibernate.id.IncrementGenerator.configure()方法中,如下:

	public void configure(Type type, Properties params, Dialect dialect) throws MappingException {returnClass = type.getReturnedClass();ObjectNameNormalizer normalizer =( ObjectNameNormalizer ) params.get( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER );String column = params.getProperty( "column" );if ( column == null ) {column = params.getProperty( PersistentIdentifierGenerator.PK );}column = dialect.quote( normalizer.normalizeIdentifierQuoting( column ) );String tableList = params.getProperty( "tables" );if ( tableList == null ) {tableList = params.getProperty( PersistentIdentifierGenerator.TABLES );}String[] tables = StringHelper.split( ", ", tableList );final String schema = dialect.quote(normalizer.normalizeIdentifierQuoting(params.getProperty( PersistentIdentifierGenerator.SCHEMA )));final String catalog = dialect.quote(normalizer.normalizeIdentifierQuoting(params.getProperty( PersistentIdentifierGenerator.CATALOG )));StringBuilder buf = new StringBuilder();for ( int i=0; i < tables.length; i++ ) {final String tableName = dialect.quote( normalizer.normalizeIdentifierQuoting( tables[i] ) );if ( tables.length > 1 ) {buf.append( "select max(" ).append( column ).append( ") as mx from " );}buf.append( Table.qualify( catalog, schema, tableName ) );if ( i < tables.length-1 ) {buf.append( " union " );}}if ( tables.length > 1 ) {buf.insert( 0, "( " ).append( " ) ids_" );column = "ids_.mx";}sql = "select max(" + column + ") from " + buf.toString();}

在初始化Table2实体类时,该方法就会执行。作用是生成对应数据库的select max语句。

在IncrementGenerator的注释中,有一段话:

Mapping parameters supported, but not usually needed: tables, column. (The tables parameter specified a comma-separated list of table names.)

说明在@GenericGenerator中,可以设置参数。在IncrementGenerator.configure()方法中,可以将这些参数读出来。读取参数的方法为params.getProperty("参数名")。例如:

params.getProperty( "column" )

就是读取column参数的值。对应读取schema的语句为:

final String schema = dialect.quote(normalizer.normalizeIdentifierQuoting(params.getProperty( PersistentIdentifierGenerator.SCHEMA ))
);

schema的参数名就是PersistentIdentifierGenerator.SCHEMA,也就是"schema"。其他预置保留参数的值大都在org.hibernate.id.PersistentIdentifierGenerator中定义。如:

public interface PersistentIdentifierGenerator extends IdentifierGenerator {/*** The configuration parameter holding the schema name*/public static final String SCHEMA = "schema";/*** The configuration parameter holding the table name for the* generated id*/public static final String TABLE = "target_table";/*** The configuration parameter holding the table names for all* tables for which the id must be unique*/public static final String TABLES = "identity_tables";/*** The configuration parameter holding the primary key column* name of the generated id*/public static final String PK = "target_column";/*** The configuration parameter holding the catalog name*/public static final String CATALOG = "catalog";/*** The key under whcih to find the {@link org.hibernate.cfg.ObjectNameNormalizer} in the config param map.*/public static final String IDENTIFIER_NORMALIZER = "identifier_normalizer";

在@GenericGenerator中设置parameter的方法为:

@Id
@Column(name = "ids")
@GeneratedValue(generator = "idGenerator", strategy = GenerationType.IDENTITY)
@GenericGenerator(name = "idGenerator", strategy = "increment", parameters = @Parameter(name = PersistentIdentifierGenerator.SCHEMA, value = "db2"))
private Integer ids;

这样就能在table2前加上正确的schema名称(db2),生成正确的查询语句:

select max(ids) from db2.table2

如果有多个参数,可以写为:

@GenericGenerator(name = "idGenerator", strategy = "increment", parameters = {
            @Parameter(name = PersistentIdentifierGenerator.SCHEMA, value = "db2"),
            @Parameter(name=PersistentIdentifierGenerator.CATALOG, value = "db2")

})

相关文章:

如何在@GenericGenerator中显式指定schema

现在的情况是&#xff0c;在MySQL中有db1和db2两个数据库。项目使用Hibernate&#xff0c;可同时访问db1和db2&#xff0c;默认数据库为db1。表table2在db2中。且table2的主键名为ids&#xff0c;是自增长字段&#xff08;Auto Increment&#xff09;。 table2和ids的定义为&a…...

感知器神经网络

1、原理 感知器是一种前馈人工神经网络&#xff0c;是人工神经网络中的一种典型结构。感知器具有分层结构&#xff0c;信息从输入层进入网络&#xff0c;逐层向前传递至输出层。根据感知器神经元变换函数、隐层数以及权值调整规则的不同&#xff0c;可以形成具有各种功能特点的…...

【C++】——继承详解

目录 1、继承的概念与意义 2、继承的使用 2.1继承的定义及语法 2.2基类与派生类间的转换 2.3继承中的作用域 2.4派生类的默认成员函数 <1>构造函数 <2>拷贝构造函数 <3>赋值重载函数 <4析构函数 <5>总结 3、继承与友元 4、继承与静态变…...

RocketMQ 消费方式

在消息传递系统中&#xff0c;“推&#xff08;Push&#xff09;”和“拉&#xff08;Pull&#xff09;”是两种不同的消息消费方式&#xff0c;RocketMQ 也支持这两种模式。下面是对这两种模式的详细解释&#xff1a; 1. 推模式&#xff08;Push Model&#xff09; 模式简介…...

初始爬虫7

针对数据提取的项目实战&#xff1a; 补充初始爬虫6的一个知识点&#xff1a; etree.tostring能够自动补全html缺失的标签&#xff0c;显示原始的HTML结构 # -*- coding: utf-8 -*- from lxml import etreetext <div> <ul> <li class"item-1">…...

深入理解Appium定位策略与元素交互

深入理解Appium定位策略与元素交互 在移动应用测试领域&#xff0c;Appium作为一款流行的跨平台自动化测试工具&#xff0c;其强大而灵活的元素定位能力对于构建稳定、高效的测试脚本至关重要。本文将深入探讨Appium支持的各种定位方法&#xff0c;并分享如何通过高级技巧和最…...

java基础面试题总结

java基础面试题总结 目录 前言 1. JVM vs JDK vs JRE的了解 2. 谈谈你对编程、编译、运行的理解 3. 什么是字节码?采用字节码的好处是什么? 5. java中的注解有几种&#xff0c;分别是什么&#xff1f; 6. 字符型常量和字符串常量 7.标识符和关键字的认识 8. 泛型&#xff…...

Typescript 的类型断言

类型断言&#xff08;Type Assertion&#xff09;是 TypeScript 中的一种机制&#xff0c;允许开发者手动指定某个值的类型&#xff0c;而不是让 TypeScript 自动推断类型。类型断言通常用于在编译时告诉 TypeScript 编译器某个值的具体类型&#xff0c;以便在后续代码中进行类…...

【设计模式】单例模式详解及应用实例

单例模式&#xff08;Singleton Pattern&#xff09;是一种创建型设计模式&#xff0c;保证一个类在整个程序的生命周期中只有一个实例&#xff0c;并提供一个全局访问点。单例模式广泛用于需要全局唯一实例的场景&#xff0c;比如数据库连接池、日志对象、线程池等。 单例模式…...

学习图解算法 使用C语言

图解算法 使用C语言 也就是通过C语言实现各种算法 链接&#xff1a;百度云盘 提取码&#xff1a;1001...

基于Netty实现TCP客户端:封装断线重连、连接保持

文章目录 引言I 基于Netty实现TCP客户端基于 Netty 创建客户端 时序图封装思路NettyClient 封装II 客户端的断线重连本质使用过程中断线重连重试策略III 心跳机制心跳检测处理器心跳机制实现逻辑IV 同步等待消息返回V 工具ForkJoinPoolByteConvertUtilsee also处理假死把handle…...

基于形状记忆聚合物的折纸超结构

​ 公众号端文章&#xff1a; 基于SMP的折纸超结构https://mp.weixin.qq.com/s?__bizMzkwMjc0MTE3Mw&mid2247484016&idx4&sn16f8d4aaaff76d776cec19bc0adbdd3b&chksmc0a1afaaf7d626bc0457d9cc4ba1b38424c2aad71ffec548715e47f5611cf00f10d5a511f3b3#rd 折…...

前端用html写excel文件直接打开

源码 <html xmlns:o"urn:schemas-microsoft-com:office:office" xmlns:x"urn:schemas-microsoft-com:office:excel" xmlns"http://www.w3.org/TR/REC-html40"> <head><meta charset"UTF-8"><!--[if gte mso 9]&…...

FastText 和 Faiss 的初探了解

概览 大模型目前已经是如火如荼的程度&#xff0c;各个大厂都有推出面向大众的基础大模型&#xff0c;同时诸多行业也有在训练专有大模型&#xff0c;而大模型的发展由来却是经过多年从文本检索生成、深度学习、自然语言处理&#xff0c;在Transformer架构出来后&#xff0c;才…...

微服务保护学习笔记(五)Sentinel授权规则、获取origin、自定义异常结果、规则持久化

文章目录 前言4 授权规则4.1 基本原理4.2 获取origin4.3 配置授权规则 5 自定义异常结果6 规则持久化 前言 微服务保护学习笔记(一)雪崩问题及解决方案、Sentinel介绍与安装 微服务保护学习笔记(二)簇点链路、流控操作、流控模式(关联、链路) 微服务保护学习笔记(三)流控效果(…...

YOLOv8目标检测模型——遥感小目标检测经验分享

小目标检测——YOLOV8 一、引言 背景介绍 &#xff08;1&#xff09;目标检测的重要性 目标检测在许多领域都具有极其重要的作用。在自动驾驶中&#xff0c;目标检测能够识别道路上的障碍物和行人&#xff0c;确保行车安全。在视频监控中&#xff0c;目标检测能够实时发现异…...

构建响应式 Web 应用:Vue.js 基础指南

构建响应式 Web 应用&#xff1a;Vue.js 基础指南 一 . Vue 的介绍1.1 介绍1.2 好处1.3 特点 二 . Vue 的快速入门2.1 案例 1 : 快速搭建 Vue 的运行环境 , 在 div 视图中获取 Vue 中的数据2.2 案例 2 : 点击按钮执行 vue 中的函数输出 vue 中 data 的数据2.3 小结 三 . Vue 常…...

计算机毕业设计选题推荐-在线投票系统-Java/Python项目实战

✨作者主页&#xff1a;IT研究室✨ 个人简介&#xff1a;曾从事计算机专业培训教学&#xff0c;擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Python…...

【C/C++】程序的构建(编译)过程概述

&#x1f984;个人主页:小米里的大麦-CSDN博客 &#x1f38f;所属专栏:C_小米里的大麦的博客-CSDN博客 &#x1f381;代码托管:C: 探索C编程精髓&#xff0c;打造高效代码仓库 (gitee.com) ⚙️操作环境:Visual Studio 2022 目录 一、前言 二、预处理&#xff08;Preprocessi…...

ElasticSearch-2-核心语法集群高可用实战-Week2

ES批量操作 1.批量获取文档数据 这里多个文档是指&#xff0c;批量操作多个文档&#xff0c;搜索查询文档将在之后的章节讲解 批量获取文档数据是通过_mget的API来实现的 (1)在URL中不指定index和type 请求方式&#xff1a;GET 请求地址&#xff1a;_mget 功能说明 &#…...

linux之kylin系统nginx的安装

一、nginx的作用 1.可做高性能的web服务器 直接处理静态资源&#xff08;HTML/CSS/图片等&#xff09;&#xff0c;响应速度远超传统服务器类似apache支持高并发连接 2.反向代理服务器 隐藏后端服务器IP地址&#xff0c;提高安全性 3.负载均衡服务器 支持多种策略分发流量…...

树莓派超全系列教程文档--(62)使用rpicam-app通过网络流式传输视频

使用rpicam-app通过网络流式传输视频 使用 rpicam-app 通过网络流式传输视频UDPTCPRTSPlibavGStreamerRTPlibcamerasrc GStreamer 元素 文章来源&#xff1a; http://raspberry.dns8844.cn/documentation 原文网址 使用 rpicam-app 通过网络流式传输视频 本节介绍来自 rpica…...

在rocky linux 9.5上在线安装 docker

前面是指南&#xff0c;后面是日志 sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo sudo dnf install docker-ce docker-ce-cli containerd.io -y docker version sudo systemctl start docker sudo systemctl status docker …...

如何在看板中体现优先级变化

在看板中有效体现优先级变化的关键措施包括&#xff1a;采用颜色或标签标识优先级、设置任务排序规则、使用独立的优先级列或泳道、结合自动化规则同步优先级变化、建立定期的优先级审查流程。其中&#xff0c;设置任务排序规则尤其重要&#xff0c;因为它让看板视觉上直观地体…...

Leetcode 3577. Count the Number of Computer Unlocking Permutations

Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接&#xff1a;3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯&#xff0c;要想要能够将所有的电脑解锁&#x…...

ffmpeg(四):滤镜命令

FFmpeg 的滤镜命令是用于音视频处理中的强大工具&#xff0c;可以完成剪裁、缩放、加水印、调色、合成、旋转、模糊、叠加字幕等复杂的操作。其核心语法格式一般如下&#xff1a; ffmpeg -i input.mp4 -vf "滤镜参数" output.mp4或者带音频滤镜&#xff1a; ffmpeg…...

Ascend NPU上适配Step-Audio模型

1 概述 1.1 简述 Step-Audio 是业界首个集语音理解与生成控制一体化的产品级开源实时语音对话系统&#xff0c;支持多语言对话&#xff08;如 中文&#xff0c;英文&#xff0c;日语&#xff09;&#xff0c;语音情感&#xff08;如 开心&#xff0c;悲伤&#xff09;&#x…...

【论文阅读28】-CNN-BiLSTM-Attention-(2024)

本文把滑坡位移序列拆开、筛优质因子&#xff0c;再用 CNN-BiLSTM-Attention 来动态预测每个子序列&#xff0c;最后重构出总位移&#xff0c;预测效果超越传统模型。 文章目录 1 引言2 方法2.1 位移时间序列加性模型2.2 变分模态分解 (VMD) 具体步骤2.3.1 样本熵&#xff08;S…...

css3笔记 (1) 自用

outline: none 用于移除元素获得焦点时默认的轮廓线 broder:0 用于移除边框 font-size&#xff1a;0 用于设置字体不显示 list-style: none 消除<li> 标签默认样式 margin: xx auto 版心居中 width:100% 通栏 vertical-align 作用于行内元素 / 表格单元格&#xff…...

使用 SymPy 进行向量和矩阵的高级操作

在科学计算和工程领域&#xff0c;向量和矩阵操作是解决问题的核心技能之一。Python 的 SymPy 库提供了强大的符号计算功能&#xff0c;能够高效地处理向量和矩阵的各种操作。本文将深入探讨如何使用 SymPy 进行向量和矩阵的创建、合并以及维度拓展等操作&#xff0c;并通过具体…...