2016-08-04 14 views
0

私は春のブートアプリケーションを作成しています。私はtestEntityをデータベースに保存したい。このチュートリアルの後に:https://spring.io/guides/gs/accessing-data-jpa/ Entityを保存するためにテーブルを自動的に作成する必要があります。エンティティを作成してデータベースに保存する方法は?

しかし、私は春BoootのAppイム取得follwoingエラーとしてそれを実行しようとすると:

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

答えて

0

はこれを試してみてください:

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

助けてくれないと同じエラーが表示されます –

+0

上記のようにデモメソッドをautowiredにして@Repositoryを削除しますインタフェース。プロジェクトをきれいにして再実行してください。 –

+0

もしあなたが私のコードをここで使うことができないのであれば、私はここでコードを使うことができます:(注:それはグラベルですが、あなたはそれをmavenに変換できます)https://drive.google.com/file/d/0B_EVyl90ivXwbHlXZDNwcGg5eHM/view –

0

これを試してみてください。

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

まだ同じ問題が表示されます –

+0

回答更新.. – FuSsA

+0

問題を解決しませんでした –

0

は解決策を見つけました。 JpaConfigクラスがデフォルトのパッケージにあり、パスが指定されていませんでした.JpaConfigファイルをパッケージに移動してAplicationで問題を解決しました。

関連する問題