在我之前的 Spring MVC 项目中,我使用 Hibernate 作为 JPA 的提供者。我不必创建hibernate.cfg.xml
文件,因为我已经在 Spring DispatcherServlet 上下文文件中声明了 Hibernate Session Factory,并且我已经声明了persistence.xml
file.
在我的新项目中,我基本上想使用 Hibernate。我已经从我的数据库结构生成了实体类。但是在IDEA中DAO类还没有生成,为什么?我可以通过某种方式在 IDEA 中生成 DAO 类吗?在生成这个 POJO 的过程中,我还在 DispatcherSerlvet Context 文件中创建了 Hibernate Session Factory 实体类。
我已经创建了自己的简单 DAO 类来检查数据库中的持久类。但出现了这个错误:
Error in creating SessionFactory object./hibernate.cfg.xml not found
所以我想我必须创建hibernate.cfg.xml
。如果是的话,我是否必须将 Hibernate Session Factory 声明保留在我的 DispatcherServlet 上下文文件中?
EDIT
<!-- Hibernate session factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource">
<beans:ref bean="dataSource" />
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="hibernate.connection.url">jdbc:mysql://localhost/finances</beans:prop>
<beans:prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</beans:prop>
<beans:prop key="hibernate.connection.username">root</beans:prop>
<beans:prop key="hibernate.connection.password">root</beans:prop>
</beans:props>
</beans:property>
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>my.package.FirstClass</beans:value>
<beans:value>my.package.SecondClass</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<!-- Hibernate session factory end -->
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
EDIT #2
我已将带注释的类和连接定义移至hibernate.cfg.xml
文件。我已经删除了会话工厂定义并且还Transaction Manager
spring配置文件中的定义。我的数据库中的简单持久对象工作正常。那么也许这是使用 Spring MVC 和 Hibernate 的最短方法?但是关于Transaction Manager
?其他操作或动作是否需要此操作?
我不必创建 hibernate.cfg.xml 文件,因为我有
在我的 Spring DispatcherServlet 中声明 Hibernate Session Factory
上下文文件和我已经声明的 persistence.xml 文件。
AFAIK 在使用 JPA 时我们需要定义entityManagerFactory
Spring配置文件和JPA实现由以下决定jpaVendorAdapter
财产。persistence.xml
用于定义持久性单元。hibernate.cfg.xml
JPA 不需要。
在我的新项目中,我基本上想使用 Hibernate。
如果你想直接使用hibernate,你需要在spring配置文件或hibernate.cfg.xml文件中定义会话工厂,例如
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.
annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>com.foo.Bar</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
您混淆了 JPA 和 Hibernate 配置。
以下链接可能会帮助您避免一些混乱
春天+休眠 http://viralpatel.net/blogs/spring3-mvc-hibernate-maven-tutorial-eclipse-example/
Spring + JPA(带有 Hibernate 实现) http://www.roseindia.net/hibernate/hibernate4/SpringHibernateJpa.shtml
EDIT: Use AnnotationSessionFactoryBean
当您使用注释来定义映射时
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)