object.class 子类想得到真实的父类

zhangwuji154 2014-05-23 02:16:32
某种数据库操作,有这样的函数

datastore.find(ABC.class)

java代码里写一个ABC类,对应的是数据库里一张表的结构.所有的表的类(包括ABC类)都继承自一个Abstract类,
我现在想写一个统一点的函数.

fun(Abstract a)
{
datastore.find(a.class)
}

不管传进来的是哪个数据表的类,都能通过这个函数,找到自己对应的表的数据.现在这种写法肯定是不行的,因为数据库里没有
Abstract 这个类对应的表,肯定是找不出来数据的.
...全文
114 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
whos2002110 2014-05-23
  • 打赏
  • 举报
回复
通过主键查询倒是可以,hibernate提供接口了

/**
	 * Return the persistent instance of the given entity class with the given identifier,
	 * or null if there is no such persistent instance. (If the instance is already associated
	 * with the session, return that instance. This method never returns an uninitialized instance.)
	 *
	 * @param clazz a persistent class
	 * @param id an identifier
	 *
	 * @return a persistent instance or null
	 */
	public Object get(Class clazz, Serializable id);
如果是要查询表的所有数据,必需写sql,配合泛型才能实现统一查询接口。
S117 2014-05-23
  • 打赏
  • 举报
回复
你所有的表都有相同的表结构吗?不然怎么定义Abstract类!你是想利用Java的多态特性吧!
LinY_X 2014-05-23
  • 打赏
  • 举报
回复
偶水平太菜 听不懂

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧