Java generics and multiple parameterized class with inheritance
Tech-Today

Java generics and multiple parameterized class with inheritance


This tutorial will try to explain how to use multiple generic parameters in Java and also along the way the advantage of using this feature.

Normally I use this when I have a method in a base class that returns a generic type. For example:
public class Parent<E extends BaseType> {
public E e;

public E getType() {
return e;
}
}

public class Child extends Parent {
public Child() {
super(Type.class);
}
}

public class Type extends BaseType {
}

The example doesn't make sense in business but if you will look at it as a piece of code you can see that you can get the dynamic type specified in Child in Parent class.

Now a more logical example :-) Assume that have the following:
entity User, Role;
service: UserService, RoleService
bean: UserBean, RoleBean

Often we do a CRUD for these types of entities and so we need dynamic page generation and extendable entity, bean and service. That will do common things like for example in service: find, create, delete, update.

Let's start with the entity User and Role, both of them should extend a BaseEntity. I'll omit the annotation since I'm lame :-).
public interface IEntity {
public Long getId();
}

public class BaseEntity implements Serializable, IEntity {
private Long id;
}

public class User extends BaseEntity {
private String username;
}

public class Role extends BaseEntity {
private String roleName;
}
Now recently I encountered a problem where I want to parameterized the entity and service in a baseBean. So I first configure the base service:
public interfaces IPersistenceService {
public void create();
public void remove();
//...
}

public abstract class PersistenceService<E extends IEntity> implements IPersistenceService<E> {
public void create() { //.. }
public void remove() { //.. }
}

//Why extends? because whether the IEntity is a class or interface, E always extends it.
Now the much complicated part, how do I pass the entity and service dynamically to a base bean?
public class BaseBean<E extends IEntity, T extends PersistenceService<E>> {
private E entity;
@Inject
private T persistenceService;

public void create() {
persistenceService.create();
}
}

public class UserBean<User, UserService> extends BaseBean {
public UserBean() {
super(User.class, UserService.class);
}
}

public class RoleBean<User, RoleService> extends BaseBean {
public RoleBean() {
super(Role.class, RoleService.class);
}
}
Also take note that I was successful in injecting a generic service :-).

Now what if I have an unknown action and I want to get the BaseBean with the correct type? Here's how I did it:
public BaseBean<? extends IEntity, ? extends PersistenceService<?>> getBaseBean() {
return null;
}




- Hibernate - Get Classes That Referenced A Given Entity
There are times when we want to list the entities that referenced (via foreign keys) a given entity x. For example, when deleting entity x that is being referenced, it will throw a generic ConstraintViolationException. Oftentimes, we need to display this...

- How To Implement An Image Streamer For Primefaces Graphicimage Component
This page provide code samples for using primefaces graphic image component. In this case we are assuming that the byte data of an image is stored in an entity and that entity is access by its id. See the param id in #1. The param id is read by the streamer...

- How To Create A Custom Bean Validation In Javaee6
Bean validation has been mentioned on many articles online but I found few that explain the actual class that implements the constraint validator. For example: the bean validation article from  oracle javaee6 documentation: http://docs.oracle.com/javaee/6/tutorial/doc/gkfgx.html....

- Create A Primary Key Column With Sequence Generated Value In Hibernate
For example you have a class User: package org.kalidad.seamexercises.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.SequenceGenerator; ...

- Android Bundle Sqlite Database On Application. Create Sqlite Database On First Application Invoke.
How to setup a default sqlite database structure with data in an android application. Add this class and initialize it in your main class that extends Activity. import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;...



Tech-Today








.