1 24 package org.riotfamily.riot.hibernate.support; 25 26 import java.beans.PropertyEditorSupport ; 27 import java.io.Serializable ; 28 29 import org.hibernate.SessionFactory; 30 import org.springframework.orm.hibernate3.HibernateTemplate; 31 import org.springframework.util.Assert; 32 33 public class HibernatePropertyEditor extends PropertyEditorSupport { 34 35 private SessionFactory sessionFactory; 36 37 private Class entityClass; 38 39 public HibernatePropertyEditor(SessionFactory sessionFactory) { 40 this.sessionFactory = sessionFactory; 41 } 42 43 public void setEntityClass(Class entityClass) { 44 this.entityClass = entityClass; 45 } 46 47 public String getAsText() { 48 Object entity = getValue(); 49 if (entity == null) { 50 return null; 51 } 52 return HibernateUtils.getIdAsString(sessionFactory, entity); 53 } 54 55 public void setAsText(String id) throws IllegalArgumentException { 56 if (id == null) { 57 setValue(null); 58 } 59 Assert.notNull(entityClass, "An entityClass must be set"); 60 61 Serializable sid = HibernateUtils.convertId( 62 entityClass, id, sessionFactory); 63 64 Object entity = new HibernateTemplate(sessionFactory).get( 65 entityClass, sid); 66 67 setValue(entity); 68 } 69 70 } 71 | Popular Tags |