如何创建实体并将其保存到数据库?

时间:2016-08-04 08:55:01

标签: java spring autowired

我正在创建一个春季启动应用程序。我想将testEntity保存在数据库中。我遵循本教程:https://spring.io/guides/gs/accessing-data-jpa/ 应自动创建表以保存实体。

然而,当我尝试将其作为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;
  }
}

使用main的类:

@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"));
}

3 个答案:

答案 0 :(得分:1)

试试这个:

@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);
}

答案 1 :(得分:0)

试试这个:

@Autowired
public void demo(EntityRepo repository){
    repository.save(new testEntity("jack"));
}

答案 2 :(得分:0)

找到解决方案。 JpaConfig类在默认包中并且没有指定路径toit,将JpaConfig文件移动到包,并且Aplication修复了问题