How to initialize EntityManager

Max Madjarov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 4, 2013

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!

2 answers

1 accepted

5 votes
Answer accepted
Samuel Le Berrigaud
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 4, 2013

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.

Max Madjarov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 4, 2013

ok, thanks...

what is an advantage of using EntityManager instead of ActiveObjects?

Samuel Le Berrigaud
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 4, 2013

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.

Frédéric Esnault March 5, 2015

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.

Richard Simko [RefinedWiki] March 30, 2015

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

Frédéric Esnault March 31, 2015

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.

Mr Parashar September 20, 2016

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

2 votes
Frédéric Esnault March 31, 2015

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 Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events