我们有一个Spring应用程序,可以通过@Autowire和其他所有内容连接到没有问题的MongoDB。
现在我们还需要应用程序将也连接到SQL数据库。
所以我们创建了一个 @entity 类:
@Entity(name = "SqlCarRecord")
@Table(name = "Cars")
final public class SqlCarRecord {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "name", nullable = false)
private String name;
....
@repository 界面:
@Repository
public interface SqlCarsRepository extends JpaRepository<SqlCarRecord, Long> {
...
@Configuraion 类就像这里的示例https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-one-configuration/
在 applicationContext 中我们添加了
<jpa:repositories base-package="path.to.interface.package" />
在 pom.xml 中,我们已经有了
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
我们补充说:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.0.0.M3</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<!-- DataSource (HikariCP) -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.6.2</version>
</dependency>
<!-- JPA Provider (Hibernate) -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.6.RELEASE</version>
</dependency>
<!-- adding this to avoid "java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal" -->
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<!-- adding this to avoid "ClassNotFoundException: org.springframework.data.util.CloseableIterator" -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.13.6.RELEASE</version>
</dependency>
在 @Service 类中,我们添加了:
....
@Autowired
private SqlCarsRepository carsRepository;
项目已成功构建,但是当我们尝试运行它时,我们收到此错误:
Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'jpaMappingContext': Invocation of init method failed; nested
exception is java.lang.IllegalArgumentException: At least one JPA metamodel
must be present!
我们尝试过的一些事情:
没有运气。
将获得帮助。
提前致谢。
答案 0 :(得分:0)
我通过将@SpringBootApplication注释更改为此来解决了相同的错误消息:
@SpringBootApplication(exclude = {JndiConnectionFactoryAutoConfiguration.class,DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,JpaRepositoriesAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})
如果您直接使用@EnableAutoConfiguration,请尝试:
@EnableAutoConfiguration(exclude = {JndiConnectionFactoryAutoConfiguration.class,DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,JpaRepositoriesAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})
似乎根本问题是Spring Boot正在尝试添加已经在类路径上的东西。