Repository Connection

Hi ,
How can I access the repository connection.
Based on the research it looks like I need to use EntityManager and Persistence, but all persistence example seems to be opening a transaction. All I need is to get the connection of Repository.

Please suggest the best way to get the connection of the repository.

Thanks
Dave.

Hi!

Could you please explain what exactly you want to achieve? Do you simply want to load a collection of entities from the database?

Regards,

Aleksey

I am planning to pass the connection to a jar file, which will read data present in the entities table to perform a certain collection of operation.

Unfortunately, I don’t fully understand what exactly you want to achieve. However, you can find an example, reflecting the most common ways of manipulation with data in this sample. The sample is also available through the CUBA Studio, as it is shown is the picture below:

Screenshot at Dec 27 19-57-18

Aleksey,
Let me try to rephrase it.
All the entities are stored into Cuba Project Repository which can be either HSQLDB , Oracle or other database. Iam looking for a way to get the connection to Cuba Repository without manually providing the credentials.
I believe there must be an internal api like getConnection()

In the examples you provided and other such example usually there is insert/update operation through entity/data manager but i have no such dml transaction requirement.
I only want to get the connection and read data inside entities table.

My requirement is simple , to get the SQL connection for Cuba Project Repository. Thank you.

Thanks for the clarification, seems clear now. To get java.sql.Connection (doc link) and obtain info about, for example, the connection URL you can use the following code:


java.sql.Connection cnn = AppBeans.get(Persistence.class).getDataSource().getConnection();
String connectionURL = cnn.getMetaData().getURL();

Thank you Aleksey. The solution seems to be what iam looking , but now iam facing another challenge.
Iam getting the below error when i Assemble project.

Code block


:app-web:compileJava/home/oracle/studio-projects/DVAP/modules/web/src/com/company/dvap/web/MainGUI/Projectscreen.java:25: error: cannot find symbol
import com.haulmont.cuba.core.Persistence;
                             ^
  symbol:   class Persistence
  location: package com.haulmont.cuba.core
/home/oracle/studio-projects/DVAP/modules/web/src/com/company/dvap/web/MainGUI/Projectscreen.java:77: error: cannot find symbol
            java.sql.Connection cnn = AppBeans.get(Persistence.class).getDataSource().getConnection();
                                                   ^
  symbol:   class Persistence
  location: class Projectscreen

I imported the following library

import com.haulmont.cuba.core.Persistence;
import com.haulmont.cuba.core.global.AppBeans;

Please suggest what iam doing incorrectly.

You face this problem because you are trying to get the

Persistence

bean on the web (client) tier. To be able to use it, wrap the above code into a service, so the code is executed on the server side and the result is transferred to the client side.

I attached a sample project, that demonstrates how it should be done. In the project you can find

CnnInfoService

, that declares

String getCnnURL()

method. This method is implemented in

CnnInfoServiceBean

.

To call the

CnnInfoService#getCnnURL

method from the client side you will need to inject the service, as it is shown below:


public class Screen extends AbstractWindow {

    @Inject
    private CnnInfoService cnnInfoService;

    public void onButtonClick() throws SQLException {
        showNotification(cnnInfoService.getCnnURL(), NotificationType.HUMANIZED);
    }
}

cnn-info.zip (24.6K)

Thank you very much Aleksey. This is exactly what i was looking for and it worked perfectly as you suggested.