01: /*
02: * Created on 13-Jan-2006
03: */
04: package uk.org.ponder.rsf.hibernate3;
05:
06: import java.io.Serializable;
07:
08: import org.hibernate.EntityMode;
09: import org.hibernate.SessionFactory;
10: import org.hibernate.metadata.ClassMetadata;
11:
12: import uk.org.ponder.beanutil.BeanResolver;
13: import uk.org.ponder.conversion.StaticLeafParser;
14: import uk.org.ponder.util.UniversalRuntimeException;
15:
16: /** For any Hibernate-persisted bean, return its ID value. The value
17: * will be returned as a String, through rendering via the current
18: * mappingcontext. */
19:
20: public class HibernateIDResolver implements BeanResolver {
21:
22: private SessionFactory sessionfactory;
23: private StaticLeafParser leafparser;
24:
25: public void setSessionFactory(SessionFactory sessionfactory) {
26: this .sessionfactory = sessionfactory;
27: }
28:
29: public void setLeafParser(StaticLeafParser leafparser) {
30: this .leafparser = leafparser;
31: }
32:
33: public String resolveBean(Object bean) {
34: try {
35: ClassMetadata cm = sessionfactory.getClassMetadata(bean
36: .getClass());
37: Serializable id = cm.getIdentifier(bean, EntityMode.POJO)
38: .toString();
39: return leafparser.render(id);
40: } catch (Exception e) {
41: throw UniversalRuntimeException.accumulate(e,
42: "Error resolving ID for bean " + bean);
43: }
44: }
45:
46: }
|