日期:2014-05-16 浏览次数:20808 次
最近在学习Hibernate(基于《深入浅出Hibernate》一书),在动手练习的时候,出现了一些问题,并加以解决,现将其罗列如下:
?
数据库表创建:
?
CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(45) default NULL, `age` int(3) default NULL, `group_id` int(11) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;?
?
CREATE TABLE `t_passport` ( `id` int(11) NOT NULL , `serial` varchar(30) default NULL, `expiry` int(11) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;?
?
?
配置文件:
?
<?xml version="1.0" encoding="GB2312"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="123" /> <property name="maxActive" value="64"/> <property name="maxIdle" value="2" /> <property name="maxWait" value="10" /> <!-- <property name="validationQuery" value="select sysdate from dual" /> --> <property name="testWhileIdle" value="true" /> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <property name="minEvictableIdleTimeMillis" value="60000" /> <property name="removeAbandoned" value="true" /> </bean> <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <property name="dataSource"><ref local="myDataSource"/></property> <property name="mappingResources"> <list> <!-- <value>com/chinabank/b2c/hbm/Person.hbm.xml</value> <value>com/chinabank/b2c/hbm/Address.hbm.xml</value> --> <value>com/chinabank/b2c/hbm/TUser.hbm.xml</value> <value>com/chinabank/b2c/hbm/TPassport.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>--> <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.cglib.use_reflection_optimizer">true</prop> <prop key="hibernate.proxool.existing_pool">true</prop> <prop key="hibernate.proxool.pool_alias">test</prop> <prop key="hibernate.jdbc.batch_size">0</prop> </props> </property> </bean> <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --> <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory"><ref local="mySessionFactory"/></property> <!-- <property name="dataSource"><ref local="myDataSource"/></property> --> </bean> </beans>
?
?
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <class name="com.chinabank.b2c.hbm.TUser" table="t_user"> <id name="id" column="id" type="java.lang.Integer"> <generator class="native"><!--assigned native--> </generator> </id&