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);
}
}
emf
which in your spring configuration(not yet posted) i am sure that you havent defined an entity manager with nameemf
. 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@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