gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:37:21 24 4
gpt4 key购买 nike

我正在创建一个 Spring Boot 应用程序。我想将我的 testEntity 保存在数据库中。我正在关注本教程:https://spring.io/guides/gs/accessing-data-jpa/应自动创建一个表来保存实体。

但是,当我尝试将其作为 Spring Boot 应用程序运行时,我收到以下错误:

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: {}

请解释我做错了什么以及如何解决?

下面您可以找到数据源配置和类。

应用程序属性:

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

测试实体:

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

}

实体存储库:

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

关于java - 如何创建实体并将其保存到数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38762789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com