I have gone though https://www.playframework.com/documentation/2.5.x/JavaJPA
But it doesn't feel like a stardard way. I would like fill my jdbc connection in persistence.xml instead of application.conf
like this
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="TestPersistence"
transaction-type="RESOURCE_LOCAL">
<class>com.example.pojo.Employee</class>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/jpadb" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password"
value="mukesh" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
instead of filling the jdbc connection in application.conf like
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/jpadb"
db.default.user=root
db.default.password="mukesh"
db.default.jndiName=DefaultDS
jpa.default=TestPersistence
Also if there is no other way to get jdbc connection in persistence.xml how can we define more than one jdbc connection in application.conf. like if i have a mysql for users and oracle db for posts.
NOTE: the source code posted are just dummy copied from public domain to show the briefly problem.
You don't have to use Play's DB module. You can have your own module defined, and load your database connection and run it in isolation.
I would check out https://github.com/playframework/play-isolated-slick as an example -- here the UserDAO is bound through to SlickDAO in the Play module:
https://github.com/playframework/play-isolated-slick/blob/master/modules/play/app/Module.scala#L19
and the database provider is provided:
https://github.com/playframework/play-isolated-slick/blob/master/modules/play/app/Module.scala#L27
but you don't have to use Database.forConfig("myapp.database") -- you can define any config you feel like.
Related
I have a Java application which uses JDBC in order to connect to a Postgres database and JPA to perform operations on it. I wish to use the JTA transaction type, not the local one. For that, I need to specify a data source.
Despite reading this thread, I still have no idea what to actually put in the xml file, as I have no idea how to retrieve the name of my datasource, and/or where and how to define it.
Connection to the database already works without a problem when I use the RESOURCE-LOCAL transaction type. A lot of threads I skimmed through mentioned defining this in a file called context.xml. Does it have to be this file? As no such file has been auto generated for me when creating the JDBC database connection and I would need to create it manually.
In short, if it is possible to get the following file working by adding
<jta-data-source>something</jta-data-source>, please tell me what that something is, or how do I find out. Otherwise, please tell me how and where to define that something.
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="BankingPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>banking.Splatka</class>
<class>banking.VlastnikKonta</class>
<class>banking.FyzickaTransakce</class>
<class>banking.Klient</class>
<class>banking.PlatebniKarta</class>
<class>banking.Transakce</class>
<class>banking.Uver</class>
<class>banking.Platba</class>
<class>banking.Konto</class>
<class>banking.BankovniPrevod</class>
<class>DB_control.Transakceprevod</class>
<class>banking.Transakceprevod</class>
<class>banking.TransakcePrevod</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://xxxxxxxxxx"/>
<property name="javax.persistence.jdbc.password" value="xxxx"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.user" value="xxxxxxxxxxx"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties> </persistence-unit> </persistence>
<jta-data-source>something</jta-data-source>
something will be the jndi name of your datasource in J2EE enviornment.
When you are referring to any datasource you don't need to put below part in you persistence.xml.
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://xxxxxxxxxx"/>
<property name="javax.persistence.jdbc.password" value="xxxx"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.user" value="xxxxxxxxxxx"/>
I am new to Java EE and JPA world, and need help with understanding persistence.xml file content. I work with Eclipse IDE, TomEE application server, OpenJPA as persistence provider, and MySQL as database.
Now, lets say I have some Java EE project with annotated entities named A,B and C, and I would like to map these entities to MySQL database named TestDB. I would like for database to be created during deployment, according to entity annotations. On MySQL server I log as "root" user with password "123".
Which properties from persistence.xml correspond to which Java EE environment artifacts (what represents persistence unit, where to specify database name ...)? Is this the correct persistence.xml file content, for situation described above:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="Test" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/TestDB" />
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionUserName" value="root" />
<property name="openjpa.ConnectionPassword" value="123" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<property name="openjpa.jdbc.DBDictionary" value="mysql" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
</persistence>
What else settings are important for JPA to work properly?
(Appreciate any help)
Above persistence.xml is missing datasource tag:
<jta-data-source>TestDB-jta-DS</jta-data-source>
Then, for Apache TomEE, data source can be defined in [tomee]/conf/tomee.xml configuration file. So, proper datasource definition for above persistence.xml would be:
<Resource id="TestDB-jta-DS" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://127.0.0.1:3306/TestDB
UserName root
Password 123
JtaManaged true
DefaultAutoCommit false
</Resource>
I have a standalone java application, which uses JPA for its persistence.
Right now I have a persistence.xml in META-INF.My application is currently in development.
My question is that if I move from development to the next envirnoment, say QA. I have to modify the persistence.xml and rebuild the jar. Is this the right way to go about it ?
If not,if I move the connection properties to a different file, where should this file be placed?
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>ClassA</class>
<class>ClassB</class>
<class>ClassC</class>
<class>ClassD</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.username" value="username" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.connection.url"
value="url" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.archive.autodetection" value="class" />
</properties>
</persistence-unit>
</persistence>
Thanks in advance !
That's a good question. Normally, you put all these environment settings in an external file, say application.properties, and pass the location to it to the JVM when you start your application (e.g. -Dconfig.location=/conf/)
Then you should find a way to get the externalized properties into your EntityManagerFactory. You can't do that in persistence.xml, you can only hard-code things there. But you can do it when creating the entity manager factory by passing vendor properties.
If using a framework like spring, for example, this is easier to do, as spring provides a factory bean for the entity manager. Otherwise you should handle it yourself. Here's the relevant bit from spring:
provider.createEntityManagerFactory(persistenceUnitInfo, getJpaPropertyMap())
I am using Weblogic 11.
After initial deployment of all JMS configurations, JDBC configuration (from XADS-jdbc.xml) and ears everything works properly.
But after weblogic restart application fails to initialize with error
Caused By: java.lang.UnsupportedOperationException: The user must supply a JDBC connection
at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:54)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:130)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:349)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:132)...
According to log JDBC and EAR start their initialization simultaniously. So when EAR tries to validate DB scheme JDBC configuration initialization is not completed yet.
What could fix such situation?
Are there any weblogic configurations using which deployment items initialization could be ordered?
Thanks in Advance.
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="TestDSPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/testN/test/TestXADS</jta-data-source>
<mapping-file>META-INF/orm.xml</mapping-file>
<properties>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect"/> -->
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<!-- <property name="hibernate.cache.use_query_cache" value="true"/> -->
<property name="hibernate.cache.region_prefix" value="hibernate.test"/>
<property name="hibernate.jdbc.use_streams_for_binary" value="true"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WeblogicTransactionManagerLookup"/>
<property name="show_sql" value="true"/>
<property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"/>
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
Did you try creating the JDBC connections through admin console? If not, please try creating the data source through admin console. Once done, open your config.xml, you should see something like below:
<jdbc-system-resource>
<name>TestXADS</name>
<target>AdminServer</target>
<descriptor-file-name>jdbc/TestXADS-jdbc.xml</descriptor-file-name>
</jdbc-system-resource>
Please Note: The outer node should be <jdbc-system-resource >
I would double check WebLogic is deploying resources in the default order as stated in docs.
I am trying to change my web-app's JDBC code to JPA using Hibernate as provider. I am using Eclipse IDE. In that i have defined a MySQL data source. I added it in the persistence.xml.
But, I am getting the below error.
6640 [30289364#qtp-7494106-7] ERROR org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: tamSql
javax.naming.NameNotFoundException; remaining name 'tamSql'
My persistence.xml looks like,
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="ExpensePersistentUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>tamSql</non-jta-data-source>
<class>com.pricar.JPAInteg.Role</class>
<class>com.pricar.JPAInteg.User</class>
<class>com.pricar.JPAInteg.Userdetail</class>
<class>com.pricar.JPAInteg.Category</class>
<class>com.pricar.JPAInteg.Expens</class>
<class>com.pricar.JPAInteg.Leavetable</class>
<class>com.pricar.JPAInteg.Permissiontoken</class>
<class>com.pricar.JPAInteg.Roletokenassociation</class>
<class>com.pricar.JPAInteg.UserPK</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/officemgmt"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
Any Suggestions!!!
Thanks in Advance!
You don't need <non-jta-data-source> when your datasource is configured in <properties>. <non-jta-data-source> is needed when datasource is configured in application server configuration and obtained via JNDI.