How to initialize EntityManager


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


private EntityManager entityManager;


    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
                .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


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...


2 answers

1 accepted

5 votes

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:

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.

ok, thanks...

what is an advantage of using EntityManager instead of ActiveObjects?

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.

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.

I have the exact same problem, the ActiveObjects class is missing access to the raw EntityManager or its TableNameConverter.

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.

All Docs are outdated. They have no social responsibility for updating them

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.

Suggest an answer

Log in or Join to answer
Community showcase
Alexey Matveev
Published Saturday in Jira

How to run Jira in a docker container

Everything below is tested on Ubuntu 17.10. I prefer to use Jira in a docker container because: 1. I can install Jira with a couple of commands. 2. I can start and stop Jira just by starting and s...

106 views 2 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot