fields = getAllDeclaredFields(clazz);
+ for (Field field : fields) {
+ if (field.getAnnotation(Id.class) != null) {
+ field.setAccessible(true);
+ return field.get(entity);
+ }
+ }
+ for (Method method : clazz.getDeclaredMethods()) {
+ if (method.getAnnotation(Id.class) != null) {
+ return method.invoke(entity, null);
+ }
+ }
+ return null;
+ }
+ /**
+ * Returns the entity's @{@link I18NTranslate} fields.
+ *
+ * These fields are made accessible.
+ */
+ private static Collection getLocalizedFields(Class> clazz) {
+ Collection fields = getAllDeclaredFields(clazz);
+ List localizedFields = new ArrayList<>();
+ fields
+ .stream()
+ .filter(field -> field.getAnnotation(I18NTranslate.class) != null)
+ .forEach(
+ field -> {
+ field.setAccessible(true);
+ localizedFields.add(field);
+ });
+ return localizedFields;
+ }
+
+ private static Collection getAllDeclaredFields(Class> clazz) {
+ List fields = new ArrayList<>();
+ Collections.addAll(fields, clazz.getDeclaredFields());
+ if (clazz.getSuperclass() != null) {
+ fields.addAll(getAllDeclaredFields(clazz.getSuperclass()));
+ }
+ return fields;
+ }
+}
diff --git a/src/main/java/org/ylhealth/ym/springtest/hibernate/I18NTranslate.java b/src/main/java/org/ylhealth/ym/springtest/hibernate/I18NTranslate.java
new file mode 100644
index 0000000000000000000000000000000000000000..49c7da61546340f7f7f0f116a392861f16f77e0b
--- /dev/null
+++ b/src/main/java/org/ylhealth/ym/springtest/hibernate/I18NTranslate.java
@@ -0,0 +1,34 @@
+package org.ylhealth.ym.springtest.hibernate;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * 利用 annotation 處理多國語系轉換
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.FIELD})
+public @interface I18NTranslate {
+ /**
+ * 對應的欄位
+ * @return
+ */
+ String column() default "";
+
+ /**
+ * 對應的 id
+ * @return
+ */
+ String id() default "";
+
+ /**
+ * 翻譯的表格
+ * @return
+ */
+ String table() default "";
+
+
+
+}
diff --git a/src/main/java/org/ylhealth/ym/springtest/hibernate/SqlServerDialectWithNvarchar.java b/src/main/java/org/ylhealth/ym/springtest/hibernate/SqlServerDialectWithNvarchar.java
new file mode 100644
index 0000000000000000000000000000000000000000..b9a641c57834921e5d414524b663bd58d70c9662
--- /dev/null
+++ b/src/main/java/org/ylhealth/ym/springtest/hibernate/SqlServerDialectWithNvarchar.java
@@ -0,0 +1,10 @@
+package org.ylhealth.ym.springtest.hibernate;
+
+import java.sql.Types;
+import org.hibernate.dialect.SQLServer2012Dialect;
+
+public class SqlServerDialectWithNvarchar extends SQLServer2012Dialect {
+ public SqlServerDialectWithNvarchar() {
+ registerHibernateType(Types.NVARCHAR, 4000, "string");
+ }
+}
diff --git a/src/main/java/org/ylhealth/ym/springtest/repo/CrmSystemL1basicRepo.java b/src/main/java/org/ylhealth/ym/springtest/repo/CrmSystemL1basicRepo.java
new file mode 100644
index 0000000000000000000000000000000000000000..1b53ffd743fbfef46ac32d254374efa8451657c6
--- /dev/null
+++ b/src/main/java/org/ylhealth/ym/springtest/repo/CrmSystemL1basicRepo.java
@@ -0,0 +1,6 @@
+package org.ylhealth.ym.springtest.repo;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.ylhealth.ym.springtest.entity.CrmSystemL1basic;
+
+public interface CrmSystemL1basicRepo extends JpaRepository {}
diff --git a/src/main/resources/META-INF/services/org.hibernate.integrator.spi.Integrator b/src/main/resources/META-INF/services/org.hibernate.integrator.spi.Integrator
new file mode 100644
index 0000000000000000000000000000000000000000..49ffa6f74cdda3b88651900f82e06cd5f9d0d69f
--- /dev/null
+++ b/src/main/resources/META-INF/services/org.hibernate.integrator.spi.Integrator
@@ -0,0 +1 @@
+org.ylhealth.ym.springtest.hibernate.I18NInterceptor
\ No newline at end of file
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 36c4de50c907122c2cff2e5e04a6381a574e196d..08684d98c3b4105bc4f3b8bfbbb54484e3d520b2 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -19,7 +19,7 @@ spring:
hibernate:
show_sql: true
format_sql: false
- dialect: org.hibernate.dialect.SQLServer2012Dialect
+ dialect: org.ylhealth.ym.springtest.hibernate.SqlServerDialectWithNvarchar
globally_quoted_identifiers: true
temp:
use_jdbc_metadata_defaults: false