我在使用Spring JPA方面相对较新,并且花了几个小时来尝试解决此问题。我知道这个问题已经问了很多遍了,我尝试了许多解决方案都没有用(很可能是由于我自己的经验,我发现自己在尝试解决方案时迷失了许多次)。我已经按照建议删除了.m2存储库文件夹,确保将SQLite方言依赖项放在pom.xml中,甚至尝试制作一个配置类,在该类中我将手动创建此bean(但如前所述)如此混乱不堪,我决定在无法再次运行后将其删除)。我想做的是将SQLite数据库连接到Spring JPA项目(似乎没有多少人在他们的Spring JPA项目中使用SQLite,因为我没有找到一个清晰的教程/文档)。以下是相关文件:
应用程序主要
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableJpaRepositories
public class myApplication {
public static void main(String[] args) {
SpringApplication.run(myApplication.class, args);
}
}
我的存储库
import org.springframework.data.repository.JpaRepository;
import java.util.List;
public interface UserRepository extends JpaRepository<User, Integer> {
List<User> getUsers();
}
我的实体
@Entity
@Table(name = "User")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int user_id;
@Column(name="user_name")
private String user_name;
@Column(name="user_password")
private String user_password;
protected User() {}
public User(String user_name, String user_password) {
this.user_name = user_name;
this.user_password = user_password;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_password() {
return user_password;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
}
@Override
public String toString() {
return String.format("User[id=%d, user_name='%s']", user_id, user_name);
}
}
我的服务
@Service
public class myService {
@Autowired
UserRepository userRepository;
public List<User> getUsers() {
return userRepository.getUsers();
}
}
应用程序属性
spring.datasource.url= jdbc:sqlite:memory:database/myDB
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.datasource.username = ""
spring.datasource.password = ""
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLiteDialect
spring.jpa.generate-ddl=true
POM
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.23.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.dialect/sqlite-dialect -->
<dependency>
<groupId>org.hibernate.dialect</groupId>
<artifactId>sqlite-dialect</artifactId>
<version>0.1.0</version>
</dependency>
如标题中所述,问题似乎是找不到名为entityManagerFactory
的bean:
Field userRepository in com.project.myService required a bean named 'entityManagerFactory' that could not be found.
我认为Spring可以自动检测项目所需的bean并自动为您生产它们?许多使用MySQL和Spring嵌入式数据库的教程似乎从未遇到过此问题。这是在Spring JPA中使用SQLite的主要问题吗?我真的很感谢任何人在配置SQLite与Spring JPA一起使用方面的指导,谢谢!