-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Adding more levels of inheritance in generic DAO
PostPosted: Wed Dec 26, 2007 4:48 am 
Newbie

Joined: Wed Dec 26, 2007 4:24 am
Posts: 4
Hi,
I am implementing a group of DAOs for a group of domain objects, following the 'Generic Data Access Objects' example from http://www.hibernate.org/328.html.

My question is: How can I add more levels of inheritance?
or by the example: How can I add domain object X that extends Item, and XDao that extends ItemDao in same generic way?

Regards,
Uri.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 2:40 pm 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
Don't know if this is what you are looking for, but watch this example:

Code:
public interface ItemDAO<T extends Item> extends GenericDAO<T, Long> {

    public static final String QUERY_MAXBID = "ItemDAO.QUERY_MAXBID";
    public static final String QUERY_MINBID = "ItemDAO.QUERY_MINBID";

    Bid getMaxBid(Long itemId);
    Bid getMinBid(Long itemId);

}


Code:
public interface SuperItemDao extends ItemDAO<SuperItem> {

....Methods for SuperItem...

}


In this case, you have an ItemDAOinterface for all instances of Item, and a SuperItemDao, specific for the SuperItem class (which must extend Item)[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 27, 2007 2:54 am 
Newbie

Joined: Wed Dec 26, 2007 4:24 am
Posts: 4
Thanks but it not solves it yet...

You showed an example for DAO interface inheritance.

If I apply this example and also make my SuperItemDaoImpl to extend ItemDaoImpl and implement the SuperItemDao interface, then I get a compilation error:
Quote:
The interface IGenericDao cannot be implemented more than once with different arguments: IGenericDao<Item,Long> and IGenericDao<SuperItem,Long>


The code:
Code:
public interface IGenericDao<T, ID extends Serializable> {
   public T doSomethingGeneric(ID id);
}
public interface IItemDao<T extends Item> extends IGenericDao<T, Long>{
   public T doSomethingItem(Long id);
}
public interface ISuperItemDao<T extends SuperItem> extends IGenericDao<T, Long>{
   public SuperItem doSomethingMoreSuperItem(Long id);
}

public class GenericDao<T, ID extends Serializable> implements IGenericDao<T, ID> {
   public T doSomethingGeneric(ID id) {
...
   }
}
public class ItemDao extends GenericDao<Item, Long> implements IItemDao<Item>{
   public Item doSomethingItem(Long id) {
      ...
   }
}
public class SuperItemDao extends ItemDao implements ISuperItemDao<SuperItem>{

   public SuperItem doSomethingMoreSuperItem(Long id) {
      ...
   }
}


public class SuperItemDao extends ItemDao implements ISuperItemDao<SuperItem>{
the red color is the compilation error.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 27, 2007 4:58 pm 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
Code:
import java.io.Serializable;

public interface IGenericDao<T, ID extends Serializable> {
   public T doSomethingGeneric(ID id);
}

public interface IItemDao<T extends Item> extends IGenericDao<T, Long>{
   public T doSomethingItem(Long id);
}

public interface ISuperItemDao<T extends SuperItem> extends IItemDao<T>{
   public SuperItem doSomethingMoreSuperItem(Long id);
}

public class GenericDao<T, ID extends Serializable> implements IGenericDao<T, ID> {
   public T doSomethingGeneric(ID id) {
      return null;
   }
}

public class ItemDao<T extends Item> extends GenericDao<T, Long> implements IItemDao<T>{
   public T doSomethingItem(Long id) {
      return null;
   }
}

public class SuperItemDao<T extends SuperItem> extends ItemDao<T> implements ISuperItemDao<T> {

   public SuperItem doSomethingMoreSuperItem(Long id) {
      return null;
   }
}



In fact, there is no need to use inheritence amongst interfaces. As SuperItemDao, extends from ItemDao<SuperItem>, it already implements IItemDao<SuperItem>. That way, interfaces might be simplified to:

Code:

public interface IItemDao<T extends Item> {
   public T doSomethingItem(Long id);
}

public interface ISuperItemDao<T extends SuperItem> {
   public SuperItem doSomethingMoreSuperItem(Long id);
}


And of course:

Code:
public class SuperItem extends Item {

}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 30, 2007 9:04 am 
Beginner
Beginner

Joined: Sat Oct 20, 2007 8:28 am
Posts: 28
hi , what would you use to init daos constructors in the 2nd option ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.