From e90c1be9157dacafbbcf0525dae0c55aed189916 Mon Sep 17 00:00:00 2001 From: lidongy <1879087903@qq.com> Date: Thu, 17 Sep 2020 17:53:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?KERNEL-5345=20refactor:=20hibernate?= =?UTF-8?q?=E7=89=B9=E6=AE=8A=E5=A4=84=E7=90=86=EF=BC=8C=E4=BD=BF=E5=85=B6?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=89=A9=E5=B1=95=E5=BB=BA=E8=A1=A8=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E4=B8=BB=E9=94=AE=E8=AE=BE=E7=BD=AE=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/third/org/hibernate/dialect/Dialect.java | 12 ++++++++++-- .../fr/third/org/hibernate/mapping/PrimaryKey.java | 4 +++- 2 files changed, 13 insertions(+), 3 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 2f4d45d07..7331c1219 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 @@ -1601,11 +1601,11 @@ public abstract class Dialect implements ConversionContext { * Build an instance of a {@link SQLExceptionConversionDelegate} for * interpreting dialect-specific error or SQLState codes. *

- * When {@link #buildSQLExceptionConverter} returns null, the default + * When {@link #buildSQLExceptionConverter} returns null, the default * {@link SQLExceptionConverter} is used to interpret SQLState and * error codes. If this method is overridden to return a non-null value, * the default {@link SQLExceptionConverter} will use the returned - * {@link SQLExceptionConversionDelegate} in addition to the following + * {@link SQLExceptionConversionDelegate} in addition to the following * standard delegates: *

    *
  1. a "static" delegate based on the JDBC 4 defined SQLException hierarchy;
  2. @@ -2783,4 +2783,12 @@ public abstract class Dialect implements ConversionContext { public boolean supportsNationalizedTypes() { return true; } + + /** + * 获取主键sql字符串 + * + * @param columnList 主键列集合 + * @return int + */ + public String getPrimaryKeyString(List columnList) { return "primary key";} } 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 13a9693f2..4059b1474 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 @@ -6,6 +6,7 @@ */ package com.fr.third.org.hibernate.mapping; import java.util.Iterator; +import java.util.List; import com.fr.third.org.hibernate.dialect.Dialect; import com.fr.third.org.hibernate.internal.util.StringHelper; @@ -58,7 +59,8 @@ public class PrimaryKey extends Constraint { } public String sqlConstraintString(Dialect dialect) { - StringBuilder buf = new StringBuilder("primary key ("); + List columnList = getColumns(); + StringBuilder buf = new StringBuilder(dialect.getPrimaryKeyString(columnList)).append(" ("); Iterator iter = getColumnIterator(); while ( iter.hasNext() ) { buf.append( ( (Column) iter.next() ).getQuotedName(dialect) ); From 9039b3a65492087c79cd0cd2f8d9619146234046 Mon Sep 17 00:00:00 2001 From: lidongy <1879087903@qq.com> Date: Fri, 18 Sep 2020 09:22:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?KERNEL-5345=20refactor:=20=E5=AF=B9mysql5?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=89=B9=E6=AE=8A=E5=A4=84=E7=90=86?= =?UTF-8?q?=20=E6=94=B9=E5=9C=A8third?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/hibernate/dialect/MySQL5Dialect.java | 19 ++++++++++++++++++- .../org/hibernate/dialect/MySQL8Dialect.java | 17 +++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java 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 beeec4c7b..0b8ee5e86 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 @@ -9,9 +9,11 @@ package com.fr.third.org.hibernate.dialect; import com.fr.third.org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter; import com.fr.third.org.hibernate.exception.spi.ViolatedConstraintNameExtracter; import com.fr.third.org.hibernate.internal.util.JdbcExceptionHelper; +import com.fr.third.org.hibernate.mapping.Column; import java.sql.SQLException; import java.sql.Types; +import java.util.List; /** * An SQL dialect for MySQL 5.x specific features. @@ -19,6 +21,9 @@ import java.sql.Types; * @author Steve Ebersole */ public class MySQL5Dialect extends MySQLDialect { + + private static final int INDEX_LENGTH_LIMIT = 255; + @Override protected void registerVarcharTypes() { registerColumnType( Types.VARCHAR, "longtext" ); @@ -31,7 +36,19 @@ public class MySQL5Dialect extends MySQLDialect { public boolean supportsColumnCheck() { return false; } - + + //utf8编码下,mysql5.7以下版本主键长度不能超过255,故特殊处理,去掉它的primary。 + // 它默认会生成一个长度为255的前缀索引 + @Override + public String getPrimaryKeyString(List columnList) { + for (Column column : columnList) { + if (column.getLength() > INDEX_LENGTH_LIMIT) { + return "key"; + } + } + return "primary key"; + } + 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 new file mode 100644 index 000000000..4e10003c2 --- /dev/null +++ b/fine-hibernate/src/main/java/com/fr/third/org/hibernate/dialect/MySQL8Dialect.java @@ -0,0 +1,17 @@ +package com.fr.third.org.hibernate.dialect; + +import com.fr.third.org.hibernate.mapping.Column; + +import java.util.List; + +/** + * @author lidongy + * @version 10.0 + * Created by lidongy on 2020/9/18 + */ +public class MySQL8Dialect extends MySQL5Dialect { + @Override + public String getPrimaryKeyString(List columnList) { + return "primary key"; + } +}