How to embed java database server into distributed jar? - java

I am working on a project that uses JDBC through derby.jar, and I am trying to make it usable without the database server running inside NetBeans. I already have a running database (wich is not embedded) and a complete code. Here is my xml that i use.
<?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="boltPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>entity.Termek</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/bolt;create=true"/>
<property name="javax.persistence.jdbc.password" value="asd"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="asd"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Any suggestion for its modification? I tried using EmbeddedDriver instead of ClientDriver, but i got an exception that it cannot be found.
Edit:
I tried putting all required jar files in the classpath and they are all added to the libraries.

Did you try putting the derby jar and other required jars by derby in the classpath of the app?
More instructions here
http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
Can you edit your question to include stacktrace of errors you get after trying above link

Related

Loading class `com.mysql.jdbc.Driver'. This is deprecated

In a maven-java-sql project using hibernate, when I try to run the project this error message appears. The MySQL server is running but the the maven-java project is not able to comunicate with the MySQL server. I am using the Apache NetBeans IDE and Workbench as the server tool. Here is my persistence class
<?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="Persistence" transaction-type="RESOURCE_LOCAL">
<!-- Persistence provider -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/technikon"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
the error message
The error message tells you that:
Caused by: java.sql.SQLSyntaxErrorException: Unknown database 'technikon'
So, you will need to connect to your RDBMS via Workbench (or CLI or whatever) and run
show databases;
See what databases you are finding in the list (beware db names like Technikon) and change your settings accordingly.

JPA persistence.xml, defining a postgresql JTA data source

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"/>

How to implement JPA in Play Framework in a standard way

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.

How to populate a DB with my JPA entities in IntelliJ

Is there a way in IntelliJ to create tables in a database from my entity classes? I mean without ER diagram. The IDE allows me to create entities from the DB, but not the other way around.
I used to use netbeans and it just lets you add entities to your persistense.xml and select mode (drop and create, create, etc), but I can't find these options in IntelliJ.
I have configures de DB connection and it works, it just doesn't allow me to populate it with my entities.
Here's my persistence.xml from NetBeans (works perfect)
<?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="ClubPU2" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>baseclub.entidades.Actividad</class>
<class>baseclub.entidades.Comercio</class>
<class>baseclub.entidades.Contacto</class>
<class>baseclub.entidades.Cuota</class>
<class>baseclub.entidades.DatosInstitucionales</class>
<class>baseclub.entidades.DatosPersonales</class>
<class>baseclub.entidades.Descuento</class>
<class>baseclub.entidades.Identificacion</class>
<class>baseclub.entidades.Miembro</class>
<class>baseclub.entidades.NotaEntrada</class>
<class>baseclub.entidades.Participacion</class>
<class>baseclub.entidades.Rol</class>
<class>baseclub.entidades.Secretaria</class>
<class>baseclub.entidades.Sesion</class>
<class>baseclub.entidades.Tutor</class>
<class>baseclub.entidades.Ubicacion</class>
<class>baseclub.entidades.Usuario</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/club_universitario?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.password" value="XXXXXX"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="club"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
And here's persistence.xml from IntelliJ:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="persistenceUnit">
</persistence-unit>
</persistence>
Is there any particular reason you want IntelliJ to create the database?
if you have selected Hibernate as ORM , you can simply add in your persistence.xml the following code:
<property name="hibernate.hbm2ddl.auto" value="update" />
Start your application server, deploy your app and the database will be created.

Persistence provider not found exception in GWT app

Hey I'm getting a persistence provide not found exception the code for my persistence.xml is
<?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="GWTSales"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.jeroennouws.sales.beans.Branch</class>
<class>com.jeroennouws.sales.beans.Customer</class>
<class>com.jeroennouws.sales.beans.Employee</class>
<class>com.jeroennouws.sales.beans.Product</class>
<class>com.jeroennouws.sales.beans.Purchase</class>
<class>com.jeroennouws.sales.beans.PurchaseDetail</class>
<class>com.jeroennouws.sales.beans.PurchaseDetailPK</class>
<class>com.jeroennouws.sales.beans.Sale</class>
<class>com.jeroennouws.sales.beans.SalesDesk</class>
<class>com.jeroennouws.sales.beans.SalesDetail</class>
<class>com.jeroennouws.sales.beans.SalesDetailPK</class>
<class>com.jeroennouws.sales.beans.Stock</class>
<class>com.jeroennouws.sales.beans.Supplier</class>
<class>com.jeroennouws.sales.beans.User</class>
<properties>
<property name="eclipselink.jdbc.password" value="" />
<property name="eclipselink.jdbc.user" value="root" />
<property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/sales" />
<property name="eclipselink.logging.level" value="INFO" />
</properties>
</persistence-unit>
</persistence>
and the code that generates this exception is
public BranchJpaController() {
emf = Persistence.createEntityManagerFactory("GWTSales");
}
I have tried all different kinds of things by now, eclipselink jar file is in my WEB-INF/lib folder and my persistence.xml file is in my src/META-INF folder.
Does this has something to do with the GWT Jetty server in the eclipse plugin or am I missing something?
Check if the MySQL JDBC Driver is in your WEB-INF/lib folder.
I hope it helps.

Categories

Resources