Java Spring:缺少JPA元模型
我无法理解我的带有JPA存储库的简单SpringMVC项目出了什么问题。你能给我一个提示吗 域:Java Spring:缺少JPA元模型,java,spring,hibernate,Java,Spring,Hibernate,我无法理解我的带有JPA存储库的简单SpringMVC项目出了什么问题。你能给我一个提示吗 域: package com.test.app; @Entity @Table(name = "foo_table") public class FooDomain { @Id @Column(name = "id", unique = true, nullable = false) private Integer id; @Column(name = "text",
package com.test.app;
@Entity
@Table(name = "foo_table")
public class FooDomain {
@Id
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "text", nullable = false)
private String text;
// getters & setters here...
}
存储库
package com.test.app;
@RepositoryDefinition(domainClass=FooDomain.class, idClass=Long.class)
public interface FooRepository extends CrudRepository<FooDomain, Long> {}
}
根上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns & xsi here...>
<context:annotation-config />
<!-- Defining folders containing bean components (@Component, @Service) -->
<context:component-scan base-package="ru.lexikos.app" />
<import resource="hibernate.xml" />
<import resource="repositories.xml" />
<context:component-scan base-package="com.test.app" />
</beans>
<?xml xmlns & xsi here...>
<context:property-placeholder location="classpath:db-connection.properties" />
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns & xsi here...>
<jpa:repositories base-package="com.test.app"/>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<context:property-placeholder location="classpath:db-connection.properties" />
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="database" value="MYSQL"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<!-- spring based scanning for entity classes>-->
<property name="packagesToScan" value="com.test.app"/>
</bean>
<!-- Enables the Hibernate @Transactional programming model -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<jpa:repositories base-package="com.test.app"/>
</beans>
Xstian是正确的。我丢失了整个管理工厂声明。下面是一个现在对我有用的示例: hibernate.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns & xsi here...>
<context:annotation-config />
<!-- Defining folders containing bean components (@Component, @Service) -->
<context:component-scan base-package="ru.lexikos.app" />
<import resource="hibernate.xml" />
<import resource="repositories.xml" />
<context:component-scan base-package="com.test.app" />
</beans>
<?xml xmlns & xsi here...>
<context:property-placeholder location="classpath:db-connection.properties" />
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns & xsi here...>
<jpa:repositories base-package="com.test.app"/>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<context:property-placeholder location="classpath:db-connection.properties" />
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="database" value="MYSQL"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<!-- spring based scanning for entity classes>-->
<property name="packagesToScan" value="com.test.app"/>
</bean>
<!-- Enables the Hibernate @Transactional programming model -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<jpa:repositories base-package="com.test.app"/>
</beans>
更新
${hibernate.dial}
当我在Spring Boot中使用Hibernate 4(SessionFactory用于持久化方式,而不是EntityManager)时,我遇到了这个问题。添加此项可以消除错误。也许对某人有帮助
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.0.Final</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>ejb3-persistence</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
org.hibernate
休眠实体管理器
3.6.0.1最终版本
org.hibernate
ejb3持久性
org.hibernate
冬眠
org.hibernate
休眠注释
EntityManagerFactory的声明在哪里?如果你“使用JPA”,为什么到处都是“SessionFactory”的东西?如果使用EntityManagerFactory,为什么有SessionFactory?!