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

建材 东莞网站建设茌平做网站

建材 东莞网站建设,茌平做网站,嘉兴推广网站,深圳大型网络科技公司现在的情况是,在MySQL中有db1和db2两个数据库。项目使用Hibernate,可同时访问db1和db2,默认数据库为db1。表table2在db2中。且table2的主键名为ids,是自增长字段(Auto Increment)。 table2和ids的定义为&a…

现在的情况是,在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")

})

http://www.yayakq.cn/news/105061/

相关文章:

  • 个人网站模板儿童哪个网站可以查当地建设项目
  • 网站开发人员工资水平企业没有网站怎么对外做公示
  • 上海如何优化网站制作网页简历
  • 广东网站设计费用网站布局的重要性
  • 环宇网站建设重庆建设造价信息网
  • 企业网站导航下拉菜单怎么做湘西 网站 建设 公司
  • 如何做网站对话框互联网网站建设情况统计表
  • 网站网页设计哪个好长沙百度搜索网站排名
  • 湖北专业网站建设维修电话国内网站排名
  • 江苏兴力建设集团有限公司网站建筑360网
  • 搭建网站的空间哪里买律师事务所东莞网站建设
  • 梅州网站优化公司网络推广服务内容
  • 广东建设工程中标公示网站自己做网站做外贸可以吗
  • 溧阳建设局网站网站怎样做才能有点击率
  • 上海网站建设价企业网站设计与实现论文
  • 国家级门户网站有哪些微信小程序开发价格
  • 做押韵句子的网站产业互联网公司排名
  • 西部数码网站管理助手v3.1网站被k的表现
  • 外贸网站建设哪家强建设工程合同的内容
  • 搭建网站复杂吗网络规划设计师考海明码吗
  • 四川省建设领域信用系统网站app制作软件手机版免费下载
  • 中国最好的网站建设廊坊seo管理
  • 怎么区分网站的好坏龙之向导外贸官方网站
  • 江苏专业网站制作公司discuz安装教程
  • 南阳市城乡和住房建设局网站做同行的旅游网站
  • 做的网站百度上可以搜到吗wordpress 图片 并排
  • 知己图书网站建设策划书wordpress 电子商务主题
  • 公司网站建设合规吗wordpress 加logo
  • 中卫网站建设报价微信公众平台开发软件
  • phpcms v9网站性能优化有网站做淘宝客