Search code examples
openjpaentitymanager

Why we need a unitName in the PersistenceContext injection in an EJB


When I inject a PersistenceContext in an EJB like this:

@PersistenceContext(unitName = "someEntity")
private EntityManager em = null;

What is the unitName=someEntity doing here? In my project i see these kind of declarations but i was hoping to find some xml file called someEntity.xml, but I cannot find it. There is a RAD project called someEntity but i dont see why we need to specify that as unitName. Also, I am not able to appreciate why we cannot jsut do this:

@Autowired
private EntityManager em = null;

Solution

  • <persistence-unit name="someEntity">
    

    That persistence unit should be defined in the META-INF/persistence.xml file.