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

set Java Bean Timeout
JPA not updating ManyToMany relationship in returning result
Working fine without #column specified in the program
Spring data JPA collection pagination total elements
Dynamic schema selection in eclipselink
How to persist a guava hashcode with JPA?
JPA: Unidirectional ManyToMany with existing object to be inserted
spring data lazy loading
JPA Entity with id from referencing class
JPA: implicity persist #ManyToOne entities
modifying the persistence.xml at runtime
Insert object with composite primary key
In JPA, having a many-to-one as primary key throws referential integrity constraint violation
JPA never fetcheable #OneToMany
JPA reading/referencing annotations programatically
JPQL “DISTINCT” returns only one result

Categories

HOME
eclipse
cobalt
formal-languages
jax-rs
case
zend-framework
pascal
sitecore
javacc
google-apps
rds
zip
echo
crystal-reports-2013
barcode
teechart
rcloud
sendgrid-api-v3
arangodb
fosrestbundle
gson
text-mining
windows-ce
angular-dart
flatpak
do-while
altera
scsi
chargify
blade
pimcore
windows-server-2003
onload
definitelytyped
syntax-highlighting
shared
entity-relationship-model
browser-sync
testcomplete
constexpr
scatter3d
kodi
papaparse
classcastexception
nested-lists
searchkit
gdata
metawidget
google-data-api
chef-solo
plaintext
attributeerror
wmp
softmax
android-ibeacon
oracle-service-bus
x++
term
openrasta
globalize
oozie-coordinator
flyout
rsa-archer-grc
cudd
centrifuge
vulcanize
dup
http-status-code-401
nservicebus5
lz77
grass
xenomai
oracle-spatial
jdi
ess
morton-number
liquid-layout
airbrake
phpldapadmin
mruby
crowd
sql-scripts
jqmodal
tinyxml
amslidemenu
ecslidingviewcontroller
windows-embedded-standard
gwt-openlayers
artemis
mysql-error-1044
printdialog
ui-select2
jquery-1.9
font-awesome-4.0.0
superscrollorama
alice
preload
sqlclr
curb
extreme-programming
cgbitmapcontextcreate
umra
dataform
silent
purepdf
change-management
mathematical-typesetting

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App