0
votes

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);        
    }
}
1
If am usring UserJpaRepository.java directly in UserController.java is working fineRaj
@SpringBootApplication public class TestSpringBootDataJpaApplication { public static void main(String[] args) { SpringApplication.run(TestSpringBootDataJpaApplication.class, args); }Raj
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 daoSagar
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 placeAntJavaDev
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

1 Answers

0
votes

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