1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.util.objectwrapper.impl;
18
19 import java.io.Serializable;
20
21 import org.hibernate.EntityMode;
22 import org.hibernate.SessionFactory;
23 import org.hibernate.metadata.ClassMetadata;
24
25 import ch.elca.el4j.util.objectwrapper.ObjectWrapperRTException;
26 import ch.elca.el4j.util.objectwrapper.interfaces.KeyedVersioned;
27
28
29 /**
30 * Implementation of keyed/versioned.
31 *
32 * @svnLink $Revision: 3879 $;$Date: 2009-08-04 15:13:46 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/hibernate/src/main/java/ch/elca/el4j/util/objectwrapper/impl/KeyedVersionedHibernateImpl.java $
33 *
34 * @author David Bernhard (DBD)
35 */
36 public class KeyedVersionedHibernateImpl extends AbstractWrapper implements KeyedVersioned {
37
38 /** The session factory - source of hibernate metadata. */
39 private final SessionFactory m_sessionFactory;
40
41 /** The class metadata, retrieved once from the session factory on create(). */
42 private ClassMetadata m_meta;
43
44 /**
45 * Create a prototype instance.
46 * @param sessionFactory The session factory.
47 */
48 public KeyedVersionedHibernateImpl(SessionFactory sessionFactory) {
49 m_sessionFactory = sessionFactory;
50 }
51
52 /** {@inheritDoc} */
53 public Serializable getKey() {
54 return m_meta.getIdentifier(m_target, EntityMode.POJO);
55 }
56
57 /** {@inheritDoc} */
58 public Serializable getVersion() {
59 return (Serializable) m_meta.getVersion(m_target, EntityMode.POJO);
60 }
61
62 /** {@inheritDoc} */
63 public void setKey(Serializable key) {
64 m_meta.setIdentifier(m_target, key, EntityMode.POJO);
65 }
66
67 /** {@inheritDoc} */
68 public void setVersion(Serializable version) {
69 // This is a workaround.
70 // We do not have a setVersion method on hibernate's metadata,
71 // so we need to get the version property's index, look up its name
72 // with the index in the list of names and set it by name as a property.
73 String versionName = m_meta.getPropertyNames()[m_meta.getVersionProperty()];
74 m_meta.setPropertyValue(m_target, versionName, version, EntityMode.POJO);
75 }
76
77 /** {@inheritDoc} */
78 public Class<?> getKeyClass() {
79 return m_meta.getIdentifierType().getReturnedClass();
80 }
81
82 /** {@inheritDoc} */
83 public Class<?> getVersionClass() {
84 String versionName = m_meta.getPropertyNames()[m_meta.getVersionProperty()];
85 return m_meta.getPropertyType(versionName).getReturnedClass();
86 }
87
88 /** {@inheritDoc} */
89 @Override
90 public void create() {
91 m_meta = m_sessionFactory.getClassMetadata(m_target.getClass());
92 if (m_meta == null || m_meta.getIdentifierPropertyName() == null) {
93 throw new ObjectWrapperRTException("Failed to create hibernate metadata.");
94 }
95 }
96
97 }