From da0a9c47b20130a411053e27b85ea3dc323773e2 Mon Sep 17 00:00:00 2001 From: Zed Date: Thu, 1 Nov 2018 18:37:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?MOBILE-17909&MOBILE-17908=2010.0=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E3=80=8B=E5=AE=9A=E6=97=B6=E8=B0=83=E5=BA=A6=E6=9C=89?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E4=BB=BB=E5=8A=A1=E6=97=B6=E5=8D=B8=E8=BD=BD?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=8F=92=E4=BB=B6=EF=BC=8C=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/hibernate/tuple/PojoInstantiator.java | 7 +++- .../tuple/entity/DefaultEntityRegister.java | 41 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java index 8fbd1780b..ac3f255f4 100755 --- a/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java +++ b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java @@ -17,6 +17,7 @@ import com.fr.third.org.hibernate.internal.CoreLogging; import com.fr.third.org.hibernate.internal.CoreMessageLogger; import com.fr.third.org.hibernate.internal.util.ReflectHelper; import com.fr.third.org.hibernate.mapping.Component; +import com.fr.third.org.hibernate.tuple.entity.DefaultEntityRegister; /** * Defines a POJO-based instantiator for use from the tuplizers. @@ -72,7 +73,11 @@ public class PojoInstantiator implements Instantiator, Serializable { public Object instantiate() { if ( isAbstract ) { - throw new InstantiationException( "Cannot instantiate abstract class or interface: ", mappedClass ); + try { + return DefaultEntityRegister.getInstance().getDefaultEntityClass().newInstance(); + } catch (Exception e) { + throw new InstantiationException("class not found: ", mappedClass); + } } else if ( optimizer != null ) { return optimizer.newInstance(); diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java new file mode 100644 index 000000000..fe3932ea7 --- /dev/null +++ b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java @@ -0,0 +1,41 @@ +package com.fr.third.org.hibernate.tuple.entity; + +/** + * Created by Zed on 2018/11/1. + */ +public class DefaultEntityRegister { + + private static volatile DefaultEntityRegister instance; + private Class defaultEntityClass = null; + + public static DefaultEntityRegister getInstance() { + if (instance == null) { + synchronized (DefaultEntityRegister.class) { + if (instance == null) { + instance = new DefaultEntityRegister(); + } + } + } + return instance; + } + + private DefaultEntityRegister() { + + } + + public void registerDefaultClass(Class defaultEntityClass) { + this.defaultEntityClass = defaultEntityClass; + } + + public void resetDefaultClass() { + this.defaultEntityClass = null; + } + + public Class getDefaultEntityClass() { + return defaultEntityClass; + } + + public void setDefaultEntityClass(Class defaultEntityClass) { + this.defaultEntityClass = defaultEntityClass; + } +} From 88fad2be000f42d7d4bdada0edc93265a9009f83 Mon Sep 17 00:00:00 2001 From: Zed Date: Fri, 2 Nov 2018 09:40:22 +0800 Subject: [PATCH 2/2] fix --- .../org/hibernate/tuple/PojoInstantiator.java | 2 +- .../tuple/entity/DefaultEntityRegister.java | 21 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java index ac3f255f4..bc9758cd6 100755 --- a/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java +++ b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java @@ -74,7 +74,7 @@ public class PojoInstantiator implements Instantiator, Serializable { public Object instantiate() { if ( isAbstract ) { try { - return DefaultEntityRegister.getInstance().getDefaultEntityClass().newInstance(); + return DefaultEntityRegister.getInstance().getDefaultEntityClass(mappedClass).newInstance(); } catch (Exception e) { throw new InstantiationException("class not found: ", mappedClass); } diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java index fe3932ea7..ec7de7b57 100644 --- a/fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java +++ b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java @@ -1,12 +1,15 @@ package com.fr.third.org.hibernate.tuple.entity; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + /** * Created by Zed on 2018/11/1. */ public class DefaultEntityRegister { private static volatile DefaultEntityRegister instance; - private Class defaultEntityClass = null; + private static Map MAP = new ConcurrentHashMap(); public static DefaultEntityRegister getInstance() { if (instance == null) { @@ -23,19 +26,15 @@ public class DefaultEntityRegister { } - public void registerDefaultClass(Class defaultEntityClass) { - this.defaultEntityClass = defaultEntityClass; - } - - public void resetDefaultClass() { - this.defaultEntityClass = null; + public void registerDefaultClass(Class abstractEntityClass, Class defaultEntityClass) { + MAP.put(abstractEntityClass, defaultEntityClass); } - public Class getDefaultEntityClass() { - return defaultEntityClass; + public void removeDefaultClass(Class abstractEntityClass) { + MAP.remove(abstractEntityClass); } - public void setDefaultEntityClass(Class defaultEntityClass) { - this.defaultEntityClass = defaultEntityClass; + public Class getDefaultEntityClass(Class abstractEntityClass) { + return MAP.get(abstractEntityClass); } }