0

I have created spring boot data jpa application using gradle's and my project structure look likes below.

com.duregesh

 --TestSpringBootDataJpaApplication.java

com.durgesh.controller

 --UserController.java

com.durgesh.model

 --User.java

com.durgesh.repositories

 --UserJpaRepository.java

com.durgesh.services

 --UserServiceImpl.java

when i am runing spring boot below exception is firing Description:

Field userJpaRepository in com.durgesh.services.UserServiceImpl required a bean named 'emf' that could not be found.

Action: Consider defining a bean named 'emf' in your configuration.


@SpringBootApplication
public class TestSpringBootDataJpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestSpringBootDataJpaApplication.class, args);
    }
-------------
public interface UserJpaRepository extends JpaRepository<User, Long> {}
-------------
@Entity
@Table(name = "USER")   
public class User implements Serializable{
        @Id
        private Long id;
        @Column(unique = true)
        private String uid;
        private String password;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public String getUid() {
            return uid;
        }
        public void setUid(String uid) {
            this.uid = uid;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        }
----------------
@Service
@EnableJpaRepositories(basePackages="com.construction.de.*", entityManagerFactoryRef="emf")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserJpaRepository userJpaRepository;
    @Override
    public User add(final User user) {
        return userJpaRepository.save(user);
    }
    @Override
    public User findById(final Long id) {
        final User user = userJpaRepository.findOne(id);
            return user;
    }
}
----
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value ="/",method = RequestMethod.POST)
    public User add( @RequestBody final User user){
        return userService.add(user);
    }
    @RequestMapping(value ="/{id}",method = RequestMethod.GET)
    public User findById(@PathVariable("id") final Long id){
        return userService.findById(id);        
    }
}
Raj
  • 173
  • 3
  • 9
  • If am usring UserJpaRepository.java directly in UserController.java is working fine – Raj Nov 24 '16 at 11:31
  • @SpringBootApplication public class TestSpringBootDataJpaApplication { public static void main(String[] args) { SpringApplication.run(TestSpringBootDataJpaApplication.class, args); } – Raj Nov 24 '16 at 11:32
  • how have you configured spring ! .. seems like a problem in bean configuration . you have any bean named 'emf' like may be your EntityManagerFactory.. how have you wired that into your dao – Sagar Nov 24 '16 at 11:38
  • hmm looks like you have mixed up different configuration from different code examples. First issue is that in the your service layer 'UserServiceImpl' you re trying to wire a bean named `emf` which in your spring configuration(not yet posted) i am sure that you havent defined an entity manager with name `emf`. Second issue , is why you want the entityManager to be wired in the service layer ?? Spring Data will take care of your repositories , so you simply have to autowire the repository class as you did in the first place – AntJavaDev Nov 24 '16 at 12:13
  • Just remove `@EnableJpaRepositories` spring boot does that automatically . You now have configured it yourself and you have configured it with the wrong name of the entitymanagerfactory. But the whole annotation isn't needed when using Spring Boot. – M. Deinum Nov 24 '16 at 12:21
  • now springbooting up immediately closing – Raj Nov 24 '16 at 12:27
  • see the below log:-- [ main] c.c.d.d.TestSpringBootDataJpaApplication : Started TestSpringBootDataJpaApplication in 3.933 seconds (JVM running for 4.313) – Raj Nov 24 '16 at 12:29
  • 2016-11-24 11:57:11.526 INFO 6416 --- [ Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@479d31f3: startup date [Thu Nov 24 11:57:08 UTC 2016]; root of context hierarchy 2016-11-24 11:57:11.527 INFO 6416 --- [ Thread-3] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown 2016-11-24 11:57:11.528 INFO 6416 --- [ Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' – Raj Nov 24 '16 at 12:29
  • Don't add code/stracktraces etc. as comments as that isn't really readable. Edit and improve your question instead. – M. Deinum Nov 24 '16 at 12:33
  • Same code working fine in maven project,but not working gradle project – Raj Nov 24 '16 at 12:36
  • Deinum,when i am running application spring service is up after few second closing with below this message"Closing JPA EntityManagerFactory for persistence unit 'default' " – Raj Nov 24 '16 at 12:42
  • Resolved. Gradle dependency issue – Raj Nov 24 '16 at 12:56
  • Thanks guys for quick support – Raj Nov 24 '16 at 13:14

1 Answers1

0

The problem is with your database entityManager configuration for which, you need to follow the below steps:

(1) Provide @EnableJpaRepositories to the Application class as shown below (remove it from service).

@SpringBootApplication
@EnableJpaRepositories(basePackages="com.duregesh.*")
public class TestSpringBootDataJpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestSpringBootDataJpaApplication.class, args);
}

(2) Provide your database driver and connection properties configured in the application.properties (under src/main/resources in eclipse or WEB-INF/classes folder in server) as shown below:

spring.datasource.url: YOUR_DB_URL
spring.datasource.driverClassName: YOUR_DB_DRIVER_CLASS
spring.datasource.username: YOUR_DB_USER
spring.datasource.password: YOUR_DB_PWD
Vasu
  • 21,832
  • 11
  • 51
  • 67