使用Spring整合Hibernate出现无法自动建立表的问题

在使用Spring整合Hibernate时,出现一个莫名其妙的问题,Spring整合Hibernate出现无法自动建立表,花了好几个小时也没有查找是怎么回事。具体如下:

1、在Hibernate配置文件中设置了:

<property name="hibernate.hbm2ddl.auto">update</property>

在Junit测试中代码是:

public class SpringHibernateTest extends TestCase {
	private ApplicationContext context = null;
	{
		context = new ClassPathXmlApplicationContext("appicationContext.xml");
	}

	@Test
	public void testDataSource() throws SQLException {
		DataSource dataSource = (DataSource) context.getBean("dataSource");
		System.out.println(dataSource.getConnection());
	}

}
在applicationContext中是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<!-- 读取数据源配置 -->
<context:property-placeholder location="classpath:mysql.properties"/>
<!-- 数据源bean -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="user" value="${user}"></property>
	<property name="password" value="${password}"></property>
	<property name="driverClass" value="${driverClass}"></property>
	<property name="jdbcUrl" value="${jdbcUrl}"></property>
	
	<property name="initialPoolSize" value="${initPoolSize}"></property>
	<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	<property name="mappingLocations" value="classpath:/com/yefeng/spring/spring5/*.hbm.xml"></property>
</bean>
	
</beans>

update意思第一次没有表的时候,自动建立表。但是我在运行测试代码时,发现没能建立表。然后我换成create,发现也没有建表。搜索这个hibernate.hbm2ddl.auto的问题,发现并没有找到解决方法。

2、仔细查看了输出显示的结果,发现下面显示中可能是关键所在:

七月 06, 2016 10:31:17 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.1.Final}
七月 06, 2016 10:31:17 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
七月 06, 2016 10:31:17 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
七月 06, 2016 10:31:17 上午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity

说是hibernate的属性文件没有找到。但是以上applicationContext中确实都配置了,因为Juint中有输出,显示已经连接成功,因此不可能是Hibernate.cfg.xml的错误,因此怀疑是*.hbm.xml路径写错了。


3、带着怀疑的态度,我尝试修改了其中的Account.hbm.xml,将其中的id name属性改成一个错误的值("id"改为"myid")。

<hibernate-mapping>
    <class name="com.yefeng.spring.spring5.Account" table="SH_ACCOUNT">
        <id name="myid" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="money" type="int">
            <column name="MONEY" />
        </property>
    </class>
</hibernate-mapping>

结果发现并没有报错。问题就出在设置mappingLocations属性。此时的设置为:

<property name="mappingLocations" value="classpath:/com/yefeng/spring/spring5/*.hbm.xml"></property>
仔细检查了一遍,发现并没有错误。尝试换了一种写法,如下:

<property name="mappingLocations">
		<list>
			<value>classpath:com/yefeng/spring/spring5/Account.hbm.xml</value>
			<value>classpath:com/yefeng/spring/spring5/Book.hbm.xml</value>
		</list>
	</property>
出乎意料的是,居然成功了。怀疑是*通配符的问题,又尝试了一种方式:

<property name="mappingLocations"  value="classpath*:com/yefeng/spring/spring5/*.hbm.xml"></property>
发现此时也能成功建立表,结果是这样:

INFO: HHH000262: Table not found: SH_ACCOUNT
Hibernate: 
    create table SH_ACCOUNT (
        ID integer not null auto_increment,
        NAME varchar(255),
        MONEY integer,
        primary key (ID)
    ) ENGINE=InnoDB
七月 06, 2016 10:45:15 上午 org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl processGetTableResults
INFO: HHH000262: Table not found: SH_BOOK
Hibernate: 
    create table SH_BOOK (
        ID integer not null auto_increment,
        BOOKNAME varchar(255),
        ISBN integer,
        PRICE integer,
        NUMBER integer,
        primary key (ID)
    ) ENGINE=InnoDB
com.mchange.v2.c3p0.impl.NewProxyConnection@7fae4d4a

但此时发现以上的那个提示照样存在:

七月 06, 2016 10:45:14 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
七月 06, 2016 10:45:14 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist

到现在配置是能用了,但关于mappingLocations的通配符方式引起的问题,并没有找到原因。


4、总结可以运行的方式:

方式1:

<property name="mappingLocations">
		<list>
			<value>classpath:com/yefeng/spring/spring5/Account.hbm.xml</value>
			<value>classpath:com/yefeng/spring/spring5/Book.hbm.xml</value>
		</list>
	</property>
方式2,和出错配置方法仅在classpath后多了一个*:

<property name="mappingLocations"  value="classpath*:com/yefeng/spring/spring5/*.hbm.xml"></property>


出错配置方式:
<property name="mappingLocations" value="classpath:/com/yefeng/spring/spring5/*.hbm.xml"></property>
















评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值