首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DDLUtils和自动增量值

DDLUtils和自动增量值
EN

Stack Overflow用户
提问于 2009-10-15 17:20:28
回答 2查看 558关注 0票数 0

当尝试使用DDLUtils时,它似乎总是接管设置为自动增量的列的id值。我如何防止这种情况发生?

例如,我有一个Dogs表,其中有一个名为ownerID的列。列ownerID被设置为自动递增。然而,我的所有者列表不是连续的,有差距(例如,ownerID的2、4、5、6、7、10存在,但不存在1、3、8、9,因为它们已经被删除)。问题是在恢复DdlToDatabase时,所有者is被重置为1、2、3、4,依此类推。这意味着“我的狗”表中通过ownerID的链接现在都是不正确的。

如何让DDlUtils正确导入自动递增字段的值?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-10 03:09:16

看起来您无法从自动增量行导入这些值,这意味着如果它们在其他表中被引用,您就有麻烦了。相反,如果你想走这条路,你应该做的是使用UUID。

票数 0
EN

Stack Overflow用户

发布于 2013-06-24 21:26:28

这是一段时间以前的事了--但是我也有同样的问题,解决方案是:首先,你选择的平台必须支持identityOverrideAllowed。

代码语言:javascript
复制
Platform mySqlPlatform = PlatformFactory.createNewPlatformInstance(mysqlDataSource);
mySqlPlatform.getPlatformInfo().setIdentityOverrideAllowed(true);

您还必须为isIdentityOverrideOn设置一个示例,如何在您可以在源代码中找到的处设置它。DDLUtils

代码语言:javascript
复制
org.apache.ddlutils.platform.mssql.MsSqlPlatform...

/**
 * Determines whether we need to use identity override mode for the given table.
 * 
 * @param table The table
 * @return <code>true</code> if identity override mode is needed
 */
private boolean useIdentityOverrideFor(Table table)
{
    return isIdentityOverrideOn() &&
           getPlatformInfo().isIdentityOverrideAllowed() &&
           (table.getAutoIncrementColumns().length > 0);
}

/**
 * {@inheritDoc}
 */
protected void beforeInsert(Connection connection, Table table) throws SQLException
{
    if (useIdentityOverrideFor(table))
    {
        MSSqlBuilder builder = (MSSqlBuilder)getSqlBuilder();
     connection.createStatement().execute(builder.getEnableIdentityOverrideSql(table));
    }
}

/**
 * {@inheritDoc}
 */
protected void afterInsert(Connection connection, Table table) throws SQLException
{
    if (useIdentityOverrideFor(table))
    {
        MSSqlBuilder builder = (MSSqlBuilder)getSqlBuilder();
    connection.createStatement().execute(builder.getDisableIdentityOverrideSql(table));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1573839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档