jpa


JPA - Is there a way/method to retrieve Persistence Unit information


I'd like to find out my data source name in the code. Is there a way of doing that?
I am using eclipselink.
thanks
To be more specific, my aim is to get an jdbc connection object.
I know i can do that thru:
datasource = (DataSource) (new InitialContext()).lookup("my_data_source_name")
connection = dataSource.getConnection();
But I don't want to hard code the data source name in my code.
I also tried
java.sql.Connection connection = em.unwrap(java.sql.Connection.class);
and it always return null.
.unwrap() should be the way to go, as written in EclipseLink wiki.
I also used to get null when calling em.unwrap(java.sql.Connection.class); because it was not inside a transaction. When called like this:
em.getTransaction().begin();
java.sql.Connection conn = em.unwrap(java.sql.Connection.class);
// ...
em.getTransaction().commit();
everything works fine!
java.sql.Connection connection = em.unwrap(java.sql.Connection.class);
Should work, what version are you using? Ensure that a transaction is active.
To get the data source name you should be able to use,
((JNDIConnector)em.unwrap(JpaEntityManager.class).getSession().getLogin().getConnector()).getName();
Here's what I've found helpful:
private DataSource createDataSource() {
ClientDataSource dataSource = new ClientDataSource();
dataSource.setServerName("localhost");
dataSource.setPortNumber(1527);
dataSource.setDatabaseName("sample");
dataSource.setUser("app");
dataSource.setPassword("app");
return dataSource;
}
private EntityManagerFactory getEntityManagerFactory() {
if (emf == null) {
Map properties = new HashMap();
properties
.put(PersistenceUnitProperties.NON_JTA_DATASOURCE,createDataSource());
emf = Persistence.createEntityManagerFactory(PU_NAME, properties);
}
return emf;
}
Can you create your datasource in the code, rather than configure via persistence.xml?

Related Links

JPA entity relations are not populated after .persist()
UPDATE and JOIN with JPQL
JPA how to remove parent without delete children?
JPA entityManager is null in Pointcut
JPA: NamedQuery of name: <query> not found
Glassfish EJB 3 OpenJPA 2
owned and unowned relationships
Is there a way to get all managed entities from an EntityManager
Multi-Tenant Seam + JPA Application
JPA mapping error for sequence number
JPA : What is the behaviour of merge with lazy initialized collection?
JPA problem one-to-one association cascade= PERSIST
JPQL we can't CONCAT(String, Integer) EclipseLink?
Cant persist an entity
How to implement self join in JPA?
Unneeded column added trying to set up OneToMany relationship

Categories

HOME
batch-file
httparty
search
paraview
mysql-workbench
solver
big-o
code-coverage
retrofit2
modal-dialog
enthought
filechannel
openbugs
speech-synthesis
azure-powershell
linear-algebra
android-7.0-nougat
digital-signature
symfony-console
nim
connector
google-maps-autocomplete
angular-routing
monit
interpreter
resourcebundle
surveymonkey
fable-f#
mat
android-maps
bringtofront
carrierwave
vex
winpe
google-earth
mobx
livelink
weather
deltaspike
erb
laravel-eloquent
clickjacking
card.io
dsx-desktop
sammy.js
gojs
browser-extension
getlasterror
hivemq
sim-toolkit
google-earth-engine
tasm
laravel-elixir
levenshtein-distance
wmp
bindingnavigator
clang++
dapper-simplecrud
openresty
boo
bbpress
gpg-signature
sandcastle
materialized-path-pattern
rocks
winusb
tinkerpop3
jquery-jscrollpane
freepbx
design-principles
atan2
rpart
kubuntu
shopizer
iso-prolog
entry-point
wcm
unix-socket
parallel-testing
getopt
ebay-lms
cocoalibspotify-2.0
beaker-testing
dolby-audio-api
jquery-1.9
onload-event
superscrollorama
web-notifications
oracle-enterprise-linux
wiimote
procedures
screen-size
workflow-services
clrstoredprocedure
ie-compatibility-mode
request-headers
filesystemobject
lemmatization

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App