[英]how to create Entity and save it to database?
我正在創建一個春季啟動應用程序。 我想將我的testEntity保存在數據庫中。 我在遵循本教程: https : //spring.io/guides/gs/accessing-data-jpa/應該自動創建一個表以保存Entity。
但是,當我嘗試將其作為Spring Booot App運行時,出現以下錯誤:
Error creating bean with name 'demo' defined in backend.Application: Unsatisfied dependency expressed through constructor argument with index 0 of type [test.EntityRepo]: : No qualifying bean of type [test.EntityRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [test.EntityRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
No qualifying bean of type [test.EntityRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
請說明我在做什么錯以及如何解決?
您可以在下面找到數據源配置和類。
application.properties:
spring.datasource.url=jdbc:oracle:thin://localhost:1521/orcl
spring.datasource.username=HR
spring.datasource.password=orcl
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
testEntity:
package test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import org.springframework.data.annotation.Id;
@Entity
public class testEntity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long ID;
private String name;
public testEntity() {}
public testEntity(long iD) {
ID = iD;
}
public testEntity(String name) {
this.name = name;
}
public long getID() {
return ID;
}
public void setID(long iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
entityRepository:
package test;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EntityRepo extends CrudRepository<testEntity, Long>{
List<testEntity> findByName(String name);
}
JPA配置類:
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
class JpaConfiguration {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.H2).build();
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("test");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
}
主類:
@EnableSwagger2
@SpringBootApplication
@EnableMapRepositories
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public void demo(EntityRepo repository){
repository.save(new testEntity("jack"));
}
嘗試這個 :
@Autowired
@Qualifier("demo")
public void demo(EntityRepo repository){
repository.save(new testEntity("jack"));
}
@Repository("entityRepo")
public interface EntityRepo extends CrudRepository<testEntity, Long>{
List<testEntity> findByName(String name);
}
嘗試這個 :
@Autowired
public void demo(EntityRepo repository){
repository.save(new testEntity("jack"));
}
找到了解決方案。 JpaConfig類位於默認程序包中,未指定路徑,將JpaConfig文件移動到帶有Aplication的程序包中可解決此問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.