1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.persistence.dao.hibernate;
|
10
|
|
11
|
import org.apache.log4j.Logger;
|
12
|
import org.hibernate.HibernateException;
|
13
|
import org.hibernate.collection.spi.PersistentCollection;
|
14
|
import org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.CollectionProxy;
|
15
|
import org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.MapProxy;
|
16
|
import org.hibernate.proxy.HibernateProxy;
|
17
|
|
18
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
19
|
import eu.etaxonomy.cdm.persistence.dao.initializer.AbstractBeanInitializer;
|
20
|
|
21
|
/**
|
22
|
* @author a.kohlbecker
|
23
|
* @since 25.03.2009
|
24
|
*/
|
25
|
//@Component("defaultBeanInitializer")
|
26
|
public class HibernateBeanInitializer<CDM extends CdmBase> extends AbstractBeanInitializer<CDM>{
|
27
|
|
28
|
@SuppressWarnings("unused")
|
29
|
private static final Logger logger = Logger.getLogger(HibernateBeanInitializer.class);
|
30
|
|
31
|
@Override
|
32
|
public Object initializeInstance(Object bean) {
|
33
|
initialize(bean);
|
34
|
return HibernateProxyHelperExtended.getProxyTarget(bean);
|
35
|
}
|
36
|
|
37
|
public static void initialize(Object proxy) throws HibernateException {
|
38
|
if ( proxy == null ) {
|
39
|
return;
|
40
|
}
|
41
|
else if ( proxy instanceof HibernateProxy ) {
|
42
|
( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().initialize();
|
43
|
} else if ( proxy instanceof PersistentCollection ) {
|
44
|
( ( PersistentCollection ) proxy ).forceInitialization();
|
45
|
} else if(proxy instanceof CollectionProxy) {
|
46
|
( ( CollectionProxy<?,?> ) proxy ).isEmpty(); // checkInit is protected, unfortunately;
|
47
|
} else if(proxy instanceof MapProxy) {
|
48
|
( ( MapProxy<?,?> ) proxy ).isEmpty(); // checkInit is protected, unfortunately;
|
49
|
}
|
50
|
}
|
51
|
}
|