这一次说一下hibernate tools这个工具的使用,ORM的R和O之间一直有一个先有鸡还是先有蛋的讨论,按照偶的习惯还是先有数据库设计。这个demo因为很多原因没有最终完成,先记录着以后完善。demo中类并不是实际通过hibernate tools生成的,是经过修改的,因为对hibernate tools不是很熟悉,所以生成的类需要修改的地方比较多。这里主要说下工具的使用。
1.数据库设计来源于springside的mini-web项目。
USE `test`; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*Table structure for table `t_authority` */ DROP TABLE IF EXISTS `t_authority`; CREATE TABLE `t_authority` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `display_name` varchar(255) DEFAULT NULL, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Table structure for table `t_group` */ DROP TABLE IF EXISTS `t_group`; CREATE TABLE `t_group` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(60) NOT NULL DEFAULT '', `description` varchar(250) DEFAULT NULL, `registeredtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `status` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; /*Table structure for table `t_group_topic` */ DROP TABLE IF EXISTS `t_group_topic`; CREATE TABLE `t_group_topic` ( `group_id` bigint(20) NOT NULL, `topic_id` bigint(20) NOT NULL, KEY `idx_t_group_topic_group_id` (`group_id`), KEY `idx_t_group_topic_topic_id` (`topic_id`), CONSTRAINT `fk_t_group_topic_group_id` FOREIGN KEY (`group_id`) REFERENCES `t_group` (`id`), CONSTRAINT `fk_t_group_topic_topic_id` FOREIGN KEY (`topic_id`) REFERENCES `t_topic` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Table structure for table `t_resource` */ DROP TABLE IF EXISTS `t_resource`; CREATE TABLE `t_resource` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `position` double NOT NULL, `resource_type` varchar(255) NOT NULL, `value` varchar(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `value` (`value`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Table structure for table `t_resource_authority` */ DROP TABLE IF EXISTS `t_resource_authority`; CREATE TABLE `t_resource_authority` ( `resource_id` bigint(20) NOT NULL, `authority_id` bigint(20) NOT NULL, KEY `idx_t_resource_authority_authority_id` (`authority_id`), KEY `idx_t_resource_authority_resource_id` (`resource_id`), CONSTRAINT `FK_t_resource_authority_authority_id` FOREIGN KEY (`resource_id`) REFERENCES `t_resource` (`id`), CONSTRAINT `FK_t_resource_authority_resource_id` FOREIGN KEY (`authority_id`) REFERENCES `t_authority` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Table structure for table `t_role` */ DROP TABLE IF EXISTS `t_role`; CREATE TABLE `t_role` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; /*Table structure for table `t_role_authority` */ DROP TABLE IF EXISTS `t_role_authority`; CREATE TABLE `t_role_authority` ( `role_id` bigint(20) NOT NULL, `authority_id` bigint(20) NOT NULL, KEY `idx_t_role_authority_role_id` (`role_id`), KEY `idx_t_role_authority_authority_id` (`authority_id`), CONSTRAINT `FK_t_role_authority_authority_id` FOREIGN KEY (`authority_id`) REFERENCES `t_authority` (`id`), CONSTRAINT `FK_t_role_authority_role_id` FOREIGN KEY (`role_id`) REFERENCES `t_role` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Table structure for table `t_topic` */ DROP TABLE IF EXISTS `t_topic`; CREATE TABLE `t_topic` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` char(50) NOT NULL, `status` tinyint(1) NOT NULL DEFAULT '1', `rule` varchar(255) NOT NULL, `creator` bigint(20) NOT NULL, `created` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; /*Table structure for table `t_user` */ DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `email` varchar(255) DEFAULT NULL, `login_name` varchar(255) NOT NULL, `name` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `login_name` (`login_name`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; /*Table structure for table `t_user_role` */ DROP TABLE IF EXISTS `t_user_role`; CREATE TABLE `t_user_role` ( `user_id` bigint(20) NOT NULL, `role_id` bigint(20) NOT NULL, KEY `idx_t_user_role_user_id` (`user_id`), KEY `idx_t_user_role_role_id` (`role_id`), CONSTRAINT `fk_t_user_role_role_id` FOREIGN KEY (`role_id`) REFERENCES `t_role` (`id`), CONSTRAINT `fk_t_user_role_user_id` FOREIGN KEY (`user_id`) REFERENCES `t_user` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
典型的基于角色和用户组的权限设计。
2.hibernate tools有很多使用的方法,个人感觉最简单直观的是安装hibernate tools的插件,安装方法个人倾向于采用link的方式,下图为插件的目录结构:
path=D:\\plug-in\\hibernate tools
然后重启Eclipse即可!
3.成功安装插件后,就可以新建各种配置文件了,如图:
配置文件hibernate.cfg.xml和hibernate.reveng.xml建立成功后,切换到hibernate视图,