From 986e74992426bc4a5f315c5875cafbcfda352eaa Mon Sep 17 00:00:00 2001 From: lidongy <1879087903@qq.com> Date: Fri, 18 Sep 2020 13:39:44 +0800 Subject: [PATCH] =?UTF-8?q?DEC-15106=20fix:=20mysql=E6=9C=89=E4=BA=9B?= =?UTF-8?q?=E5=B0=8F=E7=89=88=E6=9C=AC(=E5=A6=825.5.46)=E4=B8=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=88=9B=E5=BB=BAkey=E7=9A=84=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=88=9B=E5=BB=BA=E5=89=8D=E7=BC=80=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=EF=BC=8C=E6=AD=A4=E5=A4=84=E6=89=8B=E5=8A=A8=E6=8C=87?= =?UTF-8?q?=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/fr/third/org/hibernate/dialect/Dialect.java | 9 ++++++++- .../fr/third/org/hibernate/dialect/MySQL5Dialect.java | 7 +++++++ .../fr/third/org/hibernate/dialect/MySQL8Dialect.java | 4 ++++ .../com/fr/third/org/hibernate/mapping/PrimaryKey.java | 4 +++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/Dialect.java b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/Dialect.java index 7331c1219..402cfc459 100644 --- a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/Dialect.java +++ b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/Dialect.java @@ -2788,7 +2788,14 @@ public abstract class Dialect implements ConversionContext { * 获取主键sql字符串 * * @param columnList 主键列集合 - * @return int + * @return String */ public String getPrimaryKeyString(List columnList) { return "primary key";} + + /** + * 获取主键sql后缀 + * + * @return String + */ + public String getPrimaryKeyPostfix(Column column) { return "";} } diff --git a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL5Dialect.java b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL5Dialect.java index 0b8ee5e86..cb3f39a10 100644 --- a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL5Dialect.java +++ b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL5Dialect.java @@ -49,6 +49,13 @@ public class MySQL5Dialect extends MySQLDialect { return "primary key"; } + // mysql有些小版本(如5.5.46)不支持创建key的同时自动创建前缀索引,此处手动指定 + @Override + public String getPrimaryKeyPostfix(Column column) { + int columnLength = column.getLength(); + return columnLength > INDEX_LENGTH_LIMIT ? "(" + INDEX_LENGTH_LIMIT + ")" : ""; + } + public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() { return EXTRACTER; } diff --git a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java index 4e10003c2..c72256e0d 100644 --- a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java +++ b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java @@ -14,4 +14,8 @@ public class MySQL8Dialect extends MySQL5Dialect { public String getPrimaryKeyString(List columnList) { return "primary key"; } + + + @Override + public String getPrimaryKeyPostfix(Column column) { return "";} } diff --git a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/mapping/PrimaryKey.java b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/mapping/PrimaryKey.java index 4059b1474..d9674ebeb 100644 --- a/fine-hibernate/src/main/java/com/fr/third/org/hibernate/mapping/PrimaryKey.java +++ b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/mapping/PrimaryKey.java @@ -63,7 +63,9 @@ public class PrimaryKey extends Constraint { StringBuilder buf = new StringBuilder(dialect.getPrimaryKeyString(columnList)).append(" ("); Iterator iter = getColumnIterator(); while ( iter.hasNext() ) { - buf.append( ( (Column) iter.next() ).getQuotedName(dialect) ); + Column column = (Column) iter.next(); + buf.append( column.getQuotedName(dialect) ); + buf.append( dialect.getPrimaryKeyPostfix(column) ); if ( iter.hasNext() ) { buf.append(", "); }