1

I have implemented Generic DAO for all my bean classes. I am facing a problem in setting the entries in to the cache.

DAO.class

public abstract class DAO<T extends Serializable> extends Cache { save(Collection<T> list) { // batch process will store the list to db for(T t : list) { cache(t.getKey(), t); } } } 

Bean's DAO classes

public class BeanDAO1 extends DAO<Bean1> { public static set(Collection<Bean1> list) { super.save(list); } } public class BeanDAO2 extends DAO<Bean2> { public static set(Collection<Bean2> list) { super.save(list); } } 

Now the problem is, both Bean classes have same getter method getKey(). But in DAO class, as it is type referenced, i couldn't access the method.

Is it something i am doing wrong or I should do some other thing?

4 Answers 4

1

In your save method you access the method getKey() on objects of type T. For that you must constrain that type variable to be a subtype of a type that has this method. As it is, the only thing the compiler know that your T is a subtype of Serializable (which doesn't have this method).

That means you should have a supertype for all your beans:

public interface Bean extends Serializable { public ? getKey(); // I do not know what type your keys are } 

Your bean classes should implement this interface. Then you can do the following:

public abstract class DAO<T extends Bean> extends Cache { public save(Collection<T> list) { for(T t : list) { cache(t.getKey(), t); } } } 
Sign up to request clarification or add additional context in comments.

4 Comments

an interface can extend another interface and cannot implement it
While you are correct that type T of DAO class must be Serializable as well as extend Bean type,in practice interfaces should not be marked Serializable as it is a marker interface and classes implementing it will not know that there classes have been marked Serializable and fields may be missed from being marked as transient where they themselves cannot be Serialized
@KumarAbhinav I corrected the implements/extends mistake. Additional, I didn't want to make it too complicated and this was the easiest way to be conform to the original implementation.
I removed the downvoting
0

Your type declaration T extends Serializable is too broad. You must define a class implementing or an interface extending Serializable that has an abstract method getKey() and let T extend this new type.

Comments

0

The cleanest solution as @Ray suggested is having a common interface for Entity classes such as:

public abstract class DAO<T extends AbstractEntity> extends Cache { save(Collection<T> list) { // batch process will store the list to db for(T t : list) { cache(t.getKey(), t); } } } 

And the interface:

public interface AbstractEntity { } 

EDIT:

And having Bean1 and Bean2 implement the interface as such:

public class Bean1 implements AbstractEntity { } public class Bean2 implements AbstractEntity { } 

Comments

0

The marker interface Serializable doesn't define getKey() method.However,your type T can declare multiple upper bounds.Let your Bean1 and Bean2 extend a common super class or inteface which has a getKey() method

interface Bean{ Object getKey(); } class Bean1 implements Bean,Serializable { public String getKey(){ /// } } class Bean2 implements Bean,Serializable{ public String getKey(){ /// } } public abstract class DAO<T extends Bean & Serializable> extends Cache { save(Collection<T> list) { // batch process will store the list to db for(T t : list) { cache(t.getKey(), t); } } } public class BeanDAO1 extends DAO<Bean1> { public static set(Collection<Bean1> list) { super.save(list); } } public class BeanDAO2 extends DAO<Bean2> { public static set(Collection<Bean2> list) { super.save(list); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.