搭建了项目框架,创建了*.hbm.xml文件后,运行项目,没能和预想的一样自动建表,配置文件如下所示:
<!--配置hibernate的SessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations" value="classpath:com/step/entity/hbm/*.hbm.xml"/>
<!--hiberate配置信息-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!-- 开启二级缓存 ehcache -->
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.cache.provider_configuration_file_resource_path}
</prop>
</props>
</property>
<!--扫描hibernate注解配置的entity-->
<!--<property name="packagesToScan" value="com.experiment.Entity" />-->
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
编译运行,没有报错也没能正常建表,故意改错User.hbm.xml中的name值,将id改为myId,编译,没有报错,判断是文件地址问题,尝试了很多方式,并没有解决,抱着怀疑的心态看了一下编译目录,发现hbm文件夹是空的!空的!空的!也就是说,项目在编译的时候,*.hbm.xml文件并没有发布。手动将文件复制到编译目录下,重新运行后可以自动建表了。
查了一下发现解决IntelliJ IDEA Maven项目在编译发布时候 .xml不发布的的办法(原文地址),即在*.pom文件中添加build resources的方式发布src/main下面的资源文件:
<build>
<finalName>step</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>