1.c3p0设置,在hibernate.cfg.xml文件中设置
<session-factory name="sessionFactory">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">111111</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/testdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">120</property>
</session-factory>
2.设置映射文件路径
- 在hibernate.cfg.xml里面设置,参见文章
<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
- 在spring的配置文件里面设置
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name="mappingLocations"> <list> <value>classpath*:com/sysware/testsflex/app/orm/pojo/*.hbm.xml</value> </list> </property> </bean>
3.使用hibernate tools,参见文章
- eclipse里面install new software里面updatesite http://download.jboss.org/jbosstools/updates/stable/indigo/下载
- 选择工程下的SRC目录,然后右键New->Other->Hibernate->Hibernate Configuration File(cfg.xml),在弹出的窗口中选择Next,配置 hibernate 所关联的 数据库的信息
- 在hibernate.cfg.xml里面增加缺省Schema,
<property name="hibernate.default_schema">XXX</property>
- 选择window->show view可以打开hibernate configurations,可以打开数据库信息
- 右键单击Eclipse的工具条,选择Customize Perspective->Commands->Hibernate Code Generation,单击OK。这样工具栏中就增加了Hibernate的图标。
- 左键单击图标右边的下箭头,在下拉菜单中选择Hibernate Code Generation Configurations, Main标签下的Output directory选择的是生成文件的位置,选择到src目录即可
- 选择Reverse engineer from JDBC Connection,Package选择的是生成POJO的包名,可以根据需求自己起名字。reveng.xml这个地方选择Setup->Create new->工程名->Next->Refresh 会出现 table的列表,include入你所需要 gencode的表即可(下次要加其他表可以再include进来)
- 运行run即可
4.hibernate对象里面的子对象操作
delete com.test.ivideo.cms.orm.pojo.Userdevicerole as userdevicerole where userdevicerole.id.userId = :userId