- 浏览: 3362498 次
- 性别:
- 来自: 珠海
文章分类
- 全部博客 (1633)
- Java (250)
- Android&HTML5 (111)
- Struts (10)
- Spring (236)
- Hibernate&MyBatis (115)
- SSH (49)
- jQuery插件收集 (55)
- Javascript (145)
- PHP (77)
- REST&WebService (18)
- BIRT (27)
- .NET (7)
- Database (105)
- 设计模式 (16)
- 自动化和测试 (19)
- Maven&Ant (43)
- 工作流 (36)
- 开源应用 (156)
- 其他 (16)
- 前台&美工 (119)
- 工作积累 (0)
- OS&Docker (83)
- Python&爬虫 (28)
- 工具软件 (157)
- 问题收集 (61)
- OFbiz (6)
- noSQL (12)
最新评论
-
HEZR曾嶸:
你好博主,这个不是很理解,能解释一下嘛//左边+1,上边+1, ...
java 两字符串相似度计算算法 -
天使建站:
写得不错,可以看这里,和这里的这篇文章一起看,有 ...
jquery 遍历对象、数组、集合 -
xue88ming:
很有用,谢谢
@PathVariable映射出现错误: Name for argument type -
jnjeC:
厉害,困扰了我很久
MyBatis排序时使用order by 动态参数时需要注意,用$而不是# -
TopLongMan:
非常好,很实用啊。。
PostgreSQL递归查询实现树状结构查询
问题描述:
使用自定义表来构建主键,在每次重启之后的新id总是比定义表里面的gen_val大allocationSize倍。
Model的定义
问题原因:
org.hibernate.id.MultipleHiLoPerTableGenerator的configure()方法后面:
当maxLo大于1的时候,就调用new OptimizerFactory.LegacyHiLoAlgorithmOptimizer( returnClass, maxLo ),
这个方法会调用到org.hibernate.id.enhanced.OptimizerFactory
在这个代码运行之后就到:
方法multiplyBy调用到org.hibernate.id.IdentifierGeneratorHelper
=======================
在org.hibernate.id.MultipleHiLoPerTableGenerator
maxLo = ConfigurationHelper.getInt(MAX_LO, params, Short.MAX_VALUE);可以看到,param里面的MAX_LO值,才出现这个问题。
问题追溯:
那么继续找什么时候初始化这个param?
org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory
上面是被org.hibernate.mapping.SimpleValue
在这个方法里面,identifierGeneratorProperties保存有max_lo信息,什么时候设定?
这个类里面有个
被org.hibernate.cfg.BinderHelper类的
最后参考文档:Hibernate Annotations参考文档 http://www.okrs.cn/blog/news/?1027.html, 知道注解加上
来设定这个max_lo值,就解决了问题。
使用自定义表来构建主键,在每次重启之后的新id总是比定义表里面的gen_val大allocationSize倍。
Model的定义
@Entity @TableGenerator(name = "rh_sys_portlet", table = "rh_sys_tb_generator", pkColumnName = "gen_name", valueColumnName = "gen_value", pkColumnValue = "rh_sys_portlet", allocationSize = 5 ) @Table(name = "rh_sys_portlet") public class RhSysPortlet extends BaseObject implements java.io.Serializable{ ...... }
问题原因:
org.hibernate.id.MultipleHiLoPerTableGenerator的configure()方法后面:
//hilo config maxLo = ConfigurationHelper.getInt(MAX_LO, params, Short.MAX_VALUE); returnClass = type.getReturnedClass(); if ( maxLo >= 1 ) { hiloOptimizer = new OptimizerFactory.LegacyHiLoAlgorithmOptimizer( returnClass, maxLo ); }
当maxLo大于1的时候,就调用new OptimizerFactory.LegacyHiLoAlgorithmOptimizer( returnClass, maxLo ),
这个方法会调用到org.hibernate.id.enhanced.OptimizerFactory
public LegacyHiLoAlgorithmOptimizer(Class returnClass, int incrementSize) { super( returnClass, incrementSize ); if ( incrementSize < 1 ) throw new HibernateException( "increment size cannot be less than 1" ); if ( LOG.isTraceEnabled() ) { LOG.tracev( "Creating hilo optimizer (legacy) with [incrementSize={0}; returnClass={1}]", incrementSize, returnClass.getName() ); } maxLo = incrementSize; lo = maxLo+1; }
在这个代码运行之后就到:
@Override public synchronized Serializable generate(AccessCallback callback) { if ( lo > maxLo ) { lastSourceValue = callback.getNextValue(); lo = lastSourceValue.eq( 0 ) ? 1 : 0; hi = lastSourceValue.copy().multiplyBy( maxLo+1 ); } value = hi.copy().add( lo++ ); return value.makeValue(); }
方法multiplyBy调用到org.hibernate.id.IdentifierGeneratorHelper
public IntegralDataTypeHolder multiplyBy(long factor) { checkInitialized(); value *= factor; return this; }
=======================
在org.hibernate.id.MultipleHiLoPerTableGenerator
maxLo = ConfigurationHelper.getInt(MAX_LO, params, Short.MAX_VALUE);可以看到,param里面的MAX_LO值,才出现这个问题。
问题追溯:
那么继续找什么时候初始化这个param?
org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory
public IdentifierGenerator createIdentifierGenerator(String strategy, Type type, Properties config);
上面是被org.hibernate.mapping.SimpleValue
public IdentifierGenerator createIdentifierGenerator()方法调用
在这个方法里面,identifierGeneratorProperties保存有max_lo信息,什么时候设定?
这个类里面有个
public void setIdentifierGeneratorProperties(Properties identifierGeneratorProperties) { this.identifierGeneratorProperties = identifierGeneratorProperties; }
被org.hibernate.cfg.BinderHelper类的
public static void makeIdGenerator()调用
IdGenerator gen = mappings.getGenerator( generatorName, localGenerators );gen里面设定params,然后传给这个方法的params。
最后参考文档:Hibernate Annotations参考文档 http://www.okrs.cn/blog/news/?1027.html, 知道注解加上
@Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "rh_sys_portlet") @GenericGenerator(name="rh_sys_portlet", strategy = "seqhilo", parameters = { @Parameter(name="max_lo", value = "1") } ) @Column(name = "portlet_id", unique = true, nullable = false) public Long getPortletId() { return this.portletId; }
来设定这个max_lo值,就解决了问题。
发表评论
-
spring配置事物的方式:注解和aop配置
2016-05-14 00:26 4057参考: Spring AOP中pointcut express ... -
写个mybatis的拦截插件,实现将所有执行的sql写入文件里
2016-05-12 15:59 5058原文 http://3131854.blog.51cto.co ... -
Mybatis分库分表扩展插件
2016-05-12 15:47 1586http://fangjialong.iteye.com/bl ... -
spring+mybatis+atomikos 实现JTA事务
2016-05-11 22:00 5470sping配置多个数据源 不同用户操作不同数据库 http:/ ... -
ibatis扩展支持主键生成的方法(非数据库方式)
2016-04-13 22:01 939http://blog.csdn.net/warison200 ... -
ibatis 动态 Mapped Statement
2016-03-31 17:02 819http://www.cnblogs.com/lcngu/p/ ... -
分布式事务管理
2016-03-31 16:43 993http://my.oschina.net/pingpangk ... -
ibatis中动态查询表返回用resultClass="java.util.HashMap" 的问题
2016-03-11 10:49 1079http://blog.csdn.net/dyllove98/ ... -
Mybatis传多个参数(三种解决方案)
2016-03-07 17:46 1401http://my.oschina.net/ydsakyclg ... -
mybatis --mapper配置文件中大于小于怎么处理
2016-03-04 14:21 2324http://blog.csdn.net/nich002/ar ... -
Mybatis 数据库物理分页插件 PageHelper
2015-11-28 01:09 2743http://www.cnblogs.com/digdeep/ ... -
Druid 的SQL翻译功能如何使用
2015-11-27 21:40 1926Druid是阿里巴巴开源平台上的一个项目,整个项目由数据库连接 ... -
[MyBatis]mapperLocations属性通配符的使用
2015-11-26 10:49 6162http://blog.csdn.net/szwangdf/a ... -
Hibernate Validation使用示例及讲解
2015-11-22 21:06 2702http://wdmcygah.iteye.com/blog/ ... -
MyBatis排序时使用order by 动态参数时需要注意,用$而不是#
2015-11-17 17:54 20008http://blog.csdn.net/weibing_hu ... -
SpringMVC + Mybatis + Shiro 权限整合
2015-11-14 14:06 8697详细见参考文章: 基于Spring + Spring MVC ... -
MyBatis3 的Mapper XML 文件详解
2015-11-13 11:54 1310MyBatis3 的Mapper XML 文件http://m ... -
HibernateDao.java
2015-11-10 15:55 1128http://my.oschina.net/lizy/blog ... -
Hibernate envers开发指南
2015-10-24 00:44 1192http://www.blogjava.net/xmatthe ... -
Spring+Ibatis数据库水平分库
2015-09-13 21:15 1257Spring+Ibatis数据库水平分库 http://blo ...
相关推荐
NULL 博文链接:https://mqyl1.iteye.com/blog/1098358
JPA的基本注解主要包括@Entity、@Table、@Id、@GeneratedValue、@Basic、@Column、@Transient、@Temporal和@TableGenerator等。
这是JPA标签配置手册,从ORACLE官方得到的资料,里面对JPA所有的标签都进行详细的说明。 Index of Annotations ...o @TableGenerator o @Temporal o @Transient • U o @UniqueConstraint • V o @Version
JPA注解1.1 @Entity1.2 @Table1.3 @Id1.4 @GeneratedValue1.5 @Basic1.6 @Column1.7 @Transient1.8 @Temporal1.9 @TableGenerator 1. JPA注解 1.1 @Entity 标注在实体类上,表示此类对应一个数据表。 1.2 @Table 与...
AUTO 生成器,适用与可移值的应用,多个@Id可以共享同一个 identifier生成器,只要把generator属性设成相同的值就可以。通过@SequenceGenerator 和 @TableGenerator 可以配置不同的 identifier 生成器。 table=...
探索 Hibernate 新 TableGenerator 机制 服务好“最后一公里”,高效CDN架构经验 探索推荐引擎内部的秘密 一起 select 引起的崩溃 课堂上传纸条如何防范中间人攻击? 程序人生 《9Tech访谈录》揭秘游戏王子白泽的...
javax.persistence.TableGenerator.class javax.persistence.Temporal.class javax.persistence.TemporalType.class javax.persistence.TransactionRequiredException.class javax.persistence.Transient.class ...
表格生成器-ST3插件甲崇高文本3插件用于将CSV状文本到其它可读表格格式在适当位置。 转换器内部的功劳 。...然后执行install.sh脚本以下载依赖包: $ sh install.sh用法用CSV之类的文字选择目标区域。...