前段时间用hibernate的时候,看到用范型加ParameterizedType进行设计的例子,使用的比较巧。用ParameterizedType进行切面编程十分轻巧。
下面是一个利用ParameterizedType进行切面编程的例子:
例子中有一个抽象的JpaDaoImpl.java类,所以继承该类的XXXJpaDAOImpl,都对应于一个数据库表(table)。<E extends AbstractEntity, I>中的‘E’就表示该数据库表对应于Java中的entity。JpaDaoImpl类本身也implements了Dao<E, I>的接口。Dao<E, I>定义了JpaDaoImpl所需要实现的基本方法(基于数据库表的CUID方法)。
通过“(Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]”,就可以得到E在实际的XXXJpaDaoImpl.java类中是什么具体的entity。得到E,我们就可以通过hibernate实现基本的CUID操作了。
JpaDaoImpl.java类的代码
Java代码
1./**
2. *
3. * JPA implementation of DAO.
4. *
5. * @param <E> entity type
6. * @param <I> primary key type
7. */
8.public class JpaDaoImpl<E extends AbstractEntity, I> extends JpaDaoSupport implements Dao<E, I> {
9. protected Class<E> entityClass;
10.
11. @SuppressWarnings("unchecked")
12. public JpaDaoImpl() {//very tricky here
13. entityClass = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
14. }
15.
16. public E create(E entity) {
17. getJpaTemplate().persist(entity);
18. return entity;
19. }
20.
21. public void delete(E entity) {
22. getJpaTemplate().remove(entity);
23. }
24.
25. public void delete(I id) {
26. getJpaTemplate().remove((find(id)));
27. }
28.
29. public boolean exists(I id) {
30. return find(id) != null;
31. }
32.
33. public E find(I id) {//entityClass is used
34. return getJpaTemplate().find(entityClass, id);
35. }
36.
37. public E update(E entity) {
38. return getJpaTemplate().merge(entity);
39. }
40.
41. public List<E> query() {//entityClass is used
42. return query("from " + entityClass.getSimpleName());
43. }
44.......
45.}
/**
*
* JPA implementation of DAO.
*
* @param <E> entity type
* @param <I> primary key type
*/
public class JpaDaoImpl<E extends AbstractEntity, I> extends JpaDaoSupport implements Dao<E, I> {
protected Class<E> entityClass;
@SuppressWarnings("unchecked")
public JpaDaoImpl() {//very tricky here
entityClass = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public E create(E entity) {
getJpaTemplate().persist(entity);
return entity;
}
public void delete(E entity) {
getJpaTemplate().remove(entity);
}
public void delete(I id) {
getJpaTemplate().remove((find(id)));
}
public boolean exists(I id) {
return find(id) != null;
}
public E find(I id) {//entityClass is used
return getJpaTemplate().find(entityClass, id);
}
public E update(E entity) {
return getJpaTemplate().merge(entity);
}
public List<E> query() {//entityClass is used
return query("from " + entityClass.getSimpleName());
}
......
}
Dao<E, I>接口的定义
Java代码
1.public interface Dao<E extends AbstractEntity, I> {
2. E create(E entity);
3.
4. E update(E entity);
5.
6. void delete(E entity);
7.
8. void delete(I id);
9.
10. E find(I id);
11.
12. boolean exists(I id);
13.
14. ......
15.}
public interface Dao<E extends AbstractEntity, I> {
E create(E entity);
E update(E entity);
void delete(E entity);
void delete(I id);
E find(I id);
boolean exists(I id);
......
}
继承JpaDaoImpl.java类的两个具体实现,分别为User表和Privilege表提供基本的CUID操作。
Java代码
1.public class UserDaoImpl extends JpaDaoImpl<User, Long>{
2. //No method needs
3.}
4.
5.public class PrivilegeDaoImpl extends JpaDaoImpl<Privilege, String>{
6. //No method needs
7.}
public class UserDaoImpl extends JpaDaoImpl<User, Long>{
//No method needs
}
public class PrivilegeDaoImpl extends JpaDaoImpl<Privilege, String>{
//No method needs
}
Hibernate用范型和ParameterizedType进行的设计真的很轻巧,以后在其他的设计上,也要借鉴。
下面是一个利用ParameterizedType进行切面编程的例子:
例子中有一个抽象的JpaDaoImpl.java类,所以继承该类的XXXJpaDAOImpl,都对应于一个数据库表(table)。<E extends AbstractEntity, I>中的‘E’就表示该数据库表对应于Java中的entity。JpaDaoImpl类本身也implements了Dao<E, I>的接口。Dao<E, I>定义了JpaDaoImpl所需要实现的基本方法(基于数据库表的CUID方法)。
通过“(Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]”,就可以得到E在实际的XXXJpaDaoImpl.java类中是什么具体的entity。得到E,我们就可以通过hibernate实现基本的CUID操作了。
JpaDaoImpl.java类的代码
Java代码
1./**
2. *
3. * JPA implementation of DAO.
4. *
5. * @param <E> entity type
6. * @param <I> primary key type
7. */
8.public class JpaDaoImpl<E extends AbstractEntity, I> extends JpaDaoSupport implements Dao<E, I> {
9. protected Class<E> entityClass;
10.
11. @SuppressWarnings("unchecked")
12. public JpaDaoImpl() {//very tricky here
13. entityClass = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
14. }
15.
16. public E create(E entity) {
17. getJpaTemplate().persist(entity);
18. return entity;
19. }
20.
21. public void delete(E entity) {
22. getJpaTemplate().remove(entity);
23. }
24.
25. public void delete(I id) {
26. getJpaTemplate().remove((find(id)));
27. }
28.
29. public boolean exists(I id) {
30. return find(id) != null;
31. }
32.
33. public E find(I id) {//entityClass is used
34. return getJpaTemplate().find(entityClass, id);
35. }
36.
37. public E update(E entity) {
38. return getJpaTemplate().merge(entity);
39. }
40.
41. public List<E> query() {//entityClass is used
42. return query("from " + entityClass.getSimpleName());
43. }
44.......
45.}
/**
*
* JPA implementation of DAO.
*
* @param <E> entity type
* @param <I> primary key type
*/
public class JpaDaoImpl<E extends AbstractEntity, I> extends JpaDaoSupport implements Dao<E, I> {
protected Class<E> entityClass;
@SuppressWarnings("unchecked")
public JpaDaoImpl() {//very tricky here
entityClass = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public E create(E entity) {
getJpaTemplate().persist(entity);
return entity;
}
public void delete(E entity) {
getJpaTemplate().remove(entity);
}
public void delete(I id) {
getJpaTemplate().remove((find(id)));
}
public boolean exists(I id) {
return find(id) != null;
}
public E find(I id) {//entityClass is used
return getJpaTemplate().find(entityClass, id);
}
public E update(E entity) {
return getJpaTemplate().merge(entity);
}
public List<E> query() {//entityClass is used
return query("from " + entityClass.getSimpleName());
}
......
}
Dao<E, I>接口的定义
Java代码
1.public interface Dao<E extends AbstractEntity, I> {
2. E create(E entity);
3.
4. E update(E entity);
5.
6. void delete(E entity);
7.
8. void delete(I id);
9.
10. E find(I id);
11.
12. boolean exists(I id);
13.
14. ......
15.}
public interface Dao<E extends AbstractEntity, I> {
E create(E entity);
E update(E entity);
void delete(E entity);
void delete(I id);
E find(I id);
boolean exists(I id);
......
}
继承JpaDaoImpl.java类的两个具体实现,分别为User表和Privilege表提供基本的CUID操作。
Java代码
1.public class UserDaoImpl extends JpaDaoImpl<User, Long>{
2. //No method needs
3.}
4.
5.public class PrivilegeDaoImpl extends JpaDaoImpl<Privilege, String>{
6. //No method needs
7.}
public class UserDaoImpl extends JpaDaoImpl<User, Long>{
//No method needs
}
public class PrivilegeDaoImpl extends JpaDaoImpl<Privilege, String>{
//No method needs
}
Hibernate用范型和ParameterizedType进行的设计真的很轻巧,以后在其他的设计上,也要借鉴。