我的Spring MVC项目中有以下配置:
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/tracDB"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="spring-practice"/>
<property name="packagesToScan" value="com.paweljarosz.trac.data"/>
<property name="dataSource" ref="datasource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="database" value="MYSQL"/>
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
我希望Hibernate能够根据实体自动创建和更新数据模型。有谁知道如何修复此配置来实现它?
答案 0 :(得分:3)
我想你需要这样的东西
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL"/>
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
</bean>
</property>
或者,大多数这些东西也可以直接在JPA属性中设置。 Spring允许您通过jpaVendorProperties
执行此操作。请参阅Hibernate Configuration Properties以供参考。
例如:
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.jdbc.fetch_size">100</prop>
<!-- This one below is what you're looking for-->
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>