How to initialize EntityManager
Asked by Max Madjarov:

Hi,

I'm trying to create an instance of EntityManager in confluence context...

```
...private EntityManager entityManager;...@Autowired    public void setEntityManager(EntityManager entityManager) {        this.entityManager = entityManager;    }...
```

but it is **null** . I have no declaration or definitions in **atlassian-plugin.xml**.

P.S. i do not want to specify db path or so like this:

```
EntityManager entityManager = EntityManagerBuilder                .url(jdbcProperties.url) // the JDBC url for database connection                .username(jdbcProperties.username)                 .password(jdbcProperties.passord)                .auto() // configuring the connection pool, auto detects connection pools on the classpath                .useWeakCache() // an option to use weak caches                .build(); // actually builds the entity manager
```

or

```
EntityManager manager = new EntityManager("jdbc:mysql://localhost/ao_test", "user", "password");
```

is there a way to get a "pre" configured instance fom context or i have to define a class path and server path and user, user password and so on...

Thanks!


Answers:
author: Samuel Le Berrigaud
body: If you want to use Active Objects in a plugin you should simply use the Active Objects plugin and a good place to start would be here: <https://developer.atlassian.com/display/AO/Getting+Started+with+Active+Objects>

This will give you a configured Active Objects (backed by the entity manager) talking to the Confluence database. If you're trying to hit an external database this isn't possible.

comments: author: Max Madjarov
body: ok, thanks...

what is an advantage of using EntityManager instead of ActiveObjects?


author: Samuel Le Berrigaud
body: There is none.

EntityManager is the class exposed by the AO library. In the case of Atlassian Plugins we have a facade for it, which is only this a facade.


author: Fr?d?ric Esnault
body: Samuel, the facade you're talking about is the ActiveObjects class, right ?If it is the case, then something is missing : Access to the underlying EntityManager and/or the name converters instances.


author: Richard Simko [RefinedWiki]
body: I have the exact same problem, the ActiveObjects class is missing access to the raw EntityManager or its TableNameConverter.


author: Fr?d?ric Esnault
body: Using ActiveObjects facade only allows to use AO operations (save, find, ...). I need to access the tables holding my entities data using SQL (statistics, solve the count problem of AO, any reason you can think of...).Without creating or loading an entity, we cannot access the EntityManager or (which is my goal) the TableNameConverter, to know the exact table name for my entity.I know how to use active objects using the facade, my problem is that i need access to the table itself, hence i need its name.To Samuel, yes there is an advantage, using SQL instead of AO methods.


author: Mr Parashar
body: All Docs are outdated. They have no social responsibility for updating them


author: Fr?d?ric Esnault
body: Max, from the discussion above you can see that it seems not possible to access the entity manager directly.

But there is a way to ask the application for its database configuration, allowing to create a new entity manager instance :

```
JiraHome jiraHome = (JiraHome)ComponentAccessor.getComponentOfType(JiraHome.class);
String dbconfigPath = jiraHome.getHomePath() + File.separator + "dbconfig.xml";
File dbconfigFile = new File(dbconfigPath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbFactory.newDocumentBuilder();
Document document = builder.parse(dbconfigFile);
NodeList jdbcDataSources = document.getElementsByTagName("jdbc-datasource");
if(jdbcDataSources != null && jdbcDataSources.getLength() > 0) {
    Element jdbcDataSource = (Element)jdbcDataSources.item(0);
 String url = jdbcDataSource.getElementsByTagName("url").item(0).getTextContent();
 String userName = jdbcDataSource.getElementsByTagName("username").item(0).getTextContent();
 String password = jdbcDataSource.getElementsByTagName("password").item(0).getTextContent();
```

```
}
```

Using this information, you can create the entity manager.


Tags: active-objects

database