2

I have a Service which looks like this :

@Service
public class MyService {

private final EntityManager entityManager;

private final SomeBean someBean;

@Autowired
public DbServiceImpl(EntityManager entityManager, SomeBean someBean) {
    this.entityManager = entityManager;
    this.someBean = someBean;
}
}

I need to annotate the EntityManager with @PersistenceContext. How can I do that, while keeping the @Autowired constructor ? So the question is, can I annotate the constructor parameter entityManager separately, while keeping the @Autowired annotation ?

Neeraj
  • 2,124
  • 1
  • 19
  • 35

2 Answers2

1

I had this problem before and unfortunately I don't think that you can inject the EntityManager in the constructor using the annotation @Autowired. See this ticket:

https://jira.spring.io/browse/SPR-10443

db80
  • 3,738
  • 1
  • 34
  • 36
  • Unfortunately if you want to use Spring DI and annotations injection the only solution that you have is using the annotation @PersistenceContext on the filed EntityManager. – db80 Oct 18 '17 at 09:41
1

I am not sure if you need to have the EntityManager in the constructor. Actually, you cannot do it. You can check a detailed solution here.

pik4
  • 1,203
  • 1
  • 21
  • 53