Revision cd02d8bd
Added by Andreas Müller over 8 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/HibernateBeanInitializer.java | ||
---|---|---|
1 |
// $Id$ |
|
2 |
/** |
|
3 |
* Copyright (C) 2009 EDIT |
|
4 |
* European Distributed Institute of Taxonomy |
|
5 |
* http://www.e-taxonomy.eu |
|
6 |
* |
|
7 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
8 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
9 |
*/ |
|
10 |
package eu.etaxonomy.cdm.persistence.dao.hibernate; |
|
11 |
|
|
12 |
import org.apache.log4j.Logger; |
|
13 |
import org.hibernate.HibernateException; |
|
14 |
import org.hibernate.collection.spi.PersistentCollection; |
|
15 |
import org.hibernate.envers.entities.mapper.relation.lazy.proxy.CollectionProxy; |
|
16 |
import org.hibernate.envers.entities.mapper.relation.lazy.proxy.MapProxy; |
|
17 |
import org.hibernate.proxy.HibernateProxy; |
|
18 |
|
|
19 |
import eu.etaxonomy.cdm.persistence.dao.initializer.AbstractBeanInitializer; |
|
20 |
|
|
21 |
/** |
|
22 |
* @author a.kohlbecker |
|
23 |
* @date 25.03.2009 |
|
24 |
* |
|
25 |
*/ |
|
26 |
//@Component("defaultBeanInitializer") |
|
27 |
public class HibernateBeanInitializer extends AbstractBeanInitializer{ |
|
28 |
|
|
29 |
@SuppressWarnings("unused") |
|
30 |
private static final Logger logger = Logger.getLogger(HibernateBeanInitializer.class); |
|
31 |
|
|
32 |
/* (non-Javadoc) |
|
33 |
* @see eu.etaxonomy.cdm.persistence.dao.AbstractBeanInitializer#initializeInstance(java.lang.Object) |
|
34 |
*/ |
|
35 |
@Override |
|
36 |
public Object initializeInstance(Object bean) { |
|
37 |
initialize(bean); |
|
38 |
return HibernateProxyHelperExtended.getProxyTarget(bean); |
|
39 |
} |
|
40 |
|
|
41 |
public static void initialize(Object proxy) throws HibernateException { |
|
42 |
if ( proxy == null ) { |
|
43 |
return; |
|
44 |
} |
|
45 |
else if ( proxy instanceof HibernateProxy ) { |
|
46 |
( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().initialize(); |
|
47 |
} else if ( proxy instanceof PersistentCollection ) { |
|
48 |
( ( PersistentCollection ) proxy ).forceInitialization(); |
|
49 |
} else if(proxy instanceof CollectionProxy) { |
|
50 |
( ( CollectionProxy ) proxy ).isEmpty(); // checkInit is protected, unfortunately; |
|
51 |
} else if(proxy instanceof MapProxy) { |
|
52 |
( ( MapProxy ) proxy ).isEmpty(); // checkInit is protected, unfortunately; |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
} |
|
1 |
// $Id$ |
|
2 |
/** |
|
3 |
* Copyright (C) 2009 EDIT |
|
4 |
* European Distributed Institute of Taxonomy |
|
5 |
* http://www.e-taxonomy.eu |
|
6 |
* |
|
7 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
8 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
9 |
*/ |
|
10 |
package eu.etaxonomy.cdm.persistence.dao.hibernate; |
|
11 |
|
|
12 |
import org.apache.log4j.Logger; |
|
13 |
import org.hibernate.HibernateException; |
|
14 |
import org.hibernate.collection.spi.PersistentCollection; |
|
15 |
import org.hibernate.envers.entities.mapper.relation.lazy.proxy.CollectionProxy; |
|
16 |
import org.hibernate.envers.entities.mapper.relation.lazy.proxy.MapProxy; |
|
17 |
import org.hibernate.proxy.HibernateProxy; |
|
18 |
|
|
19 |
import eu.etaxonomy.cdm.persistence.dao.initializer.AbstractBeanInitializer; |
|
20 |
|
|
21 |
/** |
|
22 |
* @author a.kohlbecker |
|
23 |
* @date 25.03.2009 |
|
24 |
* |
|
25 |
*/ |
|
26 |
//@Component("defaultBeanInitializer") |
|
27 |
public class HibernateBeanInitializer extends AbstractBeanInitializer{ |
|
28 |
|
|
29 |
@SuppressWarnings("unused") |
|
30 |
private static final Logger logger = Logger.getLogger(HibernateBeanInitializer.class); |
|
31 |
|
|
32 |
@Override |
|
33 |
public Object initializeInstance(Object bean) { |
|
34 |
initialize(bean); |
|
35 |
return HibernateProxyHelperExtended.getProxyTarget(bean); |
|
36 |
} |
|
37 |
|
|
38 |
public static void initialize(Object proxy) throws HibernateException { |
|
39 |
if ( proxy == null ) { |
|
40 |
return; |
|
41 |
} |
|
42 |
else if ( proxy instanceof HibernateProxy ) { |
|
43 |
( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().initialize(); |
|
44 |
} else if ( proxy instanceof PersistentCollection ) { |
|
45 |
( ( PersistentCollection ) proxy ).forceInitialization(); |
|
46 |
} else if(proxy instanceof CollectionProxy) { |
|
47 |
( ( CollectionProxy ) proxy ).isEmpty(); // checkInit is protected, unfortunately; |
|
48 |
} else if(proxy instanceof MapProxy) { |
|
49 |
( ( MapProxy ) proxy ).isEmpty(); // checkInit is protected, unfortunately; |
|
50 |
} |
|
51 |
} |
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
} |
Also available in: Unified diff
Fix bug for embeddable classes in AdvancedBeanInitializer #5383