Revision a3e7482e
Added by Katja Luther over 8 years ago
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/cache/CdmModelCacher.java | ||
---|---|---|
17 | 17 |
|
18 | 18 |
import org.eclipse.core.runtime.FileLocator; |
19 | 19 |
import org.eclipse.core.runtime.Platform; |
20 |
import org.hibernate.SessionFactory; |
|
21 |
import org.hibernate.boot.MetadataSources; |
|
22 |
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl; |
|
23 |
import org.hibernate.boot.registry.StandardServiceRegistry; |
|
24 |
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
|
20 | 25 |
import org.hibernate.cfg.Configuration; |
21 | 26 |
import org.hibernate.mapping.PersistentClass; |
22 | 27 |
import org.hibernate.mapping.Property; |
23 |
import org.hibernate.property.Getter; |
|
28 |
import org.hibernate.metadata.ClassMetadata; |
|
29 |
import org.hibernate.property.access.spi.Getter; |
|
24 | 30 |
import org.osgi.framework.Bundle; |
25 | 31 |
|
32 |
import org.hibernate.boot.Metadata; |
|
33 |
|
|
26 | 34 |
|
27 | 35 |
public class CdmModelCacher { |
28 | 36 |
|
... | ... | |
63 | 71 |
|
64 | 72 |
|
65 | 73 |
public Map<String, CdmModelFieldPropertyFromClass> generateModelClassMap() { |
66 |
|
|
67 |
Configuration configuration = buildConfiguration(HB_CONFIG_FILE_PATH); |
|
68 |
Iterator<PersistentClass> classMappingIterator = configuration.getClassMappings(); |
|
69 |
|
|
70 |
Map<String, CdmModelFieldPropertyFromClass> modelClassMap = new HashMap<String, CdmModelFieldPropertyFromClass>(); |
|
71 |
|
|
72 |
while(classMappingIterator.hasNext()) { |
|
73 |
PersistentClass persistentClass = classMappingIterator.next(); |
|
74 |
Class mappedClass = persistentClass.getMappedClass(); |
|
75 |
String mappedClassName = mappedClass.getName(); |
|
76 |
|
|
77 |
CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName); |
|
78 |
System.out.println("Adding class : " + mappedClassName + " to cache"); |
|
79 |
addGetters(persistentClass, cmgmfc); |
|
80 |
modelClassMap.put(mappedClassName, cmgmfc); |
|
81 |
} |
|
74 |
|
|
75 |
// A SessionFactory is set up once for an application! |
|
76 |
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() |
|
77 |
.configure(HB_CONFIG_FILE_PATH) // configures settings from hibernate.cfg.xml |
|
78 |
.build(); |
|
79 |
SessionFactory sessionFactory = null; |
|
80 |
Map<String, CdmModelFieldPropertyFromClass> modelClassMap = new HashMap<String, CdmModelFieldPropertyFromClass>(); |
|
81 |
try { |
|
82 |
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); |
|
83 |
Configuration configuration = buildConfiguration(HB_CONFIG_FILE_PATH); |
|
84 |
Map<String, ClassMetadata> classMetaDataMap = sessionFactory.getAllClassMetadata(); |
|
85 |
Metadata metadata = new MetadataSources( registry ).getMetadataBuilder().applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE ).build(); |
|
86 |
|
|
87 |
|
|
88 |
for(ClassMetadata classMetaData :classMetaDataMap.values()) { |
|
89 |
Class mappedClass = classMetaData.getMappedClass(); |
|
90 |
|
|
91 |
String mappedClassName = mappedClass.getName(); |
|
92 |
|
|
93 |
PersistentClass persistentClass =metadata.getEntityBinding(mappedClassName); |
|
94 |
CdmModelFieldPropertyFromClass cmgmfc = new CdmModelFieldPropertyFromClass(mappedClassName); |
|
95 |
System.out.println("Adding class : " + mappedClassName + " to cache"); |
|
96 |
addGetters(persistentClass, cmgmfc); |
|
97 |
modelClassMap.put(mappedClassName, cmgmfc); |
|
98 |
} |
|
99 |
} |
|
100 |
catch (Exception e) { |
|
101 |
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory |
|
102 |
// so destroy it manually. |
|
103 |
StandardServiceRegistryBuilder.destroy( registry ); |
|
104 |
} |
|
105 |
|
|
106 |
|
|
82 | 107 |
return modelClassMap; |
83 | 108 |
} |
84 | 109 |
|
Also available in: Unified diff
adapt dependencies, not finished yet