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.
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:
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:
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
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);
}
}