Announcement Announcement Module
Collapse
No announcement yet.
Tiger powered hibernate template Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Tiger powered hibernate template

    hello to all
    since 90% of my dao are sort of boiler plate codes,
    i'm trying to use Generics

    my attempt to add
    Code:
    	public K add(final T entity) throws DataIntegrityViolationException {
    		try {
    			final Serializable id = getHibernateTemplate().save(entity);
    			getHibernateTemplate().flush();
    			return (K) id;
    		} catch (InvalidStateException e) {
    			getHibernateTemplate().clear();
    			throw new DataIntegrityViolationException("failed validation", e);
    		}
    	}
    this looks ok to me.

    question is i'm having some problems with get
    Code:
    	public T get(final K key) {
    		final Class<T> entityClass = T;
    		return (T) getHibernateTemplate().get(entityClass, key);
    	}
    this does not compile

    note:
    T is Type of entity
    K is Key for entity

    for example
    T = Student
    K = String, (student's id class)

  • #2
    There is a JIRA issue about a 1.5 HibernateTemplate, it might be worth reading this however. Is it possible to see your exception?
    http://blog.interface21.com/main/200...e-of-required/
    Last edited by karldmoore; Aug 27th, 2007, 05:02 PM.

    Comment


    • #3
      Code:
      	public T get(final K key) {
      		final Class<T> entityClass = T;
      		return (T) getHibernateTemplate().get(entityClass, key);
      	}
      yeah T really means nothing. are you at all familiar with what generics really are and the concept of erasure? when you compile this, T and K get text substitued with Object. At runtime all generic information is erased, it's only a compile time facility. If the program compiles it must be safe so they throw out all that stuff. As a result you can't instiantate T or refer to it like that - it is not a Class, just a marker for an unknown (possibly bound) type reference.

      if you need to reference the class of T, you have two options: pass it in as a constructor or always subclass this thing like this

      option 1:
      Code:
      class GenericDAOImpl<E extends Entity> implements DAO<E>  {
          private Class<E> entityClass;
      
          public GenericDAOImpl(Class<E> entityClass)  {
              this.entityClass = entityClass;
          }
      
          public <PK extends Serializable> E get(PK pk)  {
              @SuppressWarnings("unchecked") // hibernates fault, they are still jdk 1.4
              E entity = (E) getHibernateTemplate().get(getEntityClass(), pk);
              return entity;
          }
      option 2, a clever little obscure hack, i'm not even sure why this works:

      Code:
      class GenericDAOImpl<E extends Entity> implements DAO<E>  {
          private Class<E> entityClass;
      
          public GenericDAOImpl(Class<E> entityClass)  {
              this.entityClass = ((ParameterizedType) getClass().getGenericSuperclass())
                       .getGenericTypeArguments()[0];  // pull it with reflection
          }
      
          public <PK extends Serializable> E get(PK pk)  {
              @SuppressWarnings("unchecked") // hibernates fault, they are still jdk 1.4
              E entity = (E) getHibernateTemplate().get(getEntityClass(), pk);
              return entity;
          }
      }
      
      class BookDAOImpl extends GenericDAOImpl<Book>  { }
      
      // you can also use anonymous classes but if you try to use it directly it will break
      new GenericDAOImpl<Book>()  { }
      have you seen this article http://www.ibm.com/developerworks/ja...enericdao.html

      if you search this & the hibernate forum for generic dao there is TONS of various implementations out there. we have ours very well figured out now it is great. Try to design it so that the client applications have to do minimal typing to talk to the database. also i have found the concept of java's generics absolutely fascinating and have embraced them a good bit. They are quite tricky but once you know what you are doing amazingly powerful. One of these days they will erase the concept of erasure and even more cool stuff will be possible. There's lots of good reading out there. This blog has some good stuff, lots of links to good reading too http://blogs.sun.com/ahe/
      Last edited by lloyd.mcclendon; Jul 15th, 2007, 12:13 PM.

      Comment


      • #4
        Here's the link to the JIRA issue.
        http://opensource.atlassian.com/proj...rowse/SPR-3128
        Last edited by karldmoore; Aug 27th, 2007, 05:02 PM.

        Comment

        Working...
        X