How to initialize EntityManager

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

Accepted Answer
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: 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.

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 Sign up to answer
Community showcase
Posted yesterday in Featured Groups

Tuesday tips & tricks: What is the Atlassian Community?

It's officially Tuesday, which means it's officially time for another tip to help you better navigate this space we call the Atlassian Community. 😄 I got a great question from community member, Sa...

47 views 2 4
View post

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