I'm writing a simple Maven project to access my MySQL / MariaDB database. In order to download the proper database connector jar file, I'm adding the following Maven dependency:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>provided</scope>
</dependency>
I've got that 5.1.47 version above "by chance", based on a previous project which already worked on my computer.
However, when I type mysql -V (capital V) on terminal to check my MySQL/MariaDB version, I get the following:
>mysql -V
mysql Ver 15.1 Distrib 10.4.6-MariaDB, for Win64 (AMD64), source revision b8e655ce029a1f182602c9f12c3cc5931226eec2
But that 10.4.6 version doesn't mean much to me in terms of determining a proper version to set up my Maven dependency.
What's the procedure to determine MySQL version for Maven dependency based on my local MySQL / MariaDB server?
What's the procedure to determine MySQL version for Maven dependency
based on my local MySQL / MariaDB server?
You cannot .They are different things. The one specified in maven is the client version while one the you checked with mysql -V is the server version. The JDBC client version does not needed to be exactly matched with the server version.
Instead , the thing that you need to ensure is that the server support client version that you used.
From the official docs, 5.1.47 should support MySQL 5.61, 5.71, 8.0. And from the compatibility matrix of MariaDB , MariaDB 10.4.6 is comparability with MySQL 5.6 and 5.7 , which mean the JDBC client version that you are using should have no problem. But if it is possible , you can update the client to version 8.0 series , which is the official recommendation and should also work with your existing MariaDB version.
You can get the newest version from mvnrepository.
If you have the older version of mysql, you need to make sure, that the versions of the connector, jdbc, database etc. match. You can find this information in the mysql developer guide. The same thing applies to the compatibility for mariadb - check out the documentation.
Note, that the driver version depends not only on the version of the database, but also on java version.
I've checked MySQL connector MVN Repository for latest version, then I decided to replace 5.1.47 to 8.0.18.
However, after replacing to 8.x.x, I got "hibernate.dialect not set" error. So I added MySQL8Dialect to persistence.xml, then got "time zone value 'unknown' is unrecognized" error. So I added &serverTimezone=UTC to my connection parameters, than it worked.
Here is the updated persistence.xml after using 8.x.x:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.1">
<persistence-unit name="myunit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/databasename?useSSL=false&serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect" />
</properties>
</persistence-unit>
</persistence>
Related
I have two Postgres databases, one for Dev and one for Prod. I also have a Java/Hibernate application that has the following:
persistence.xml
<persistence-unit name="pims">
<jta-data-source>jdbc/pims</jta-data-source>
<properties>
<!-- For JBoss / Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="update" />
<!-- For Glassfish / Toplink -->
<property name="toplink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
Problem
When the JBoss server starts in the Prod environment, it creates the new tables as expected. But when it starts in the DEV environment (same code), it does not create the new tables.
This makes me think there is some setting on the database that prevents the creation of the new tables.
Question
Is there any config on the database that will prevent JPA/Hibernate from creating the new tables?
More Info:
Java 1.7
Struts 1
Hibernate 4.2.21.Final
PostgreSQL 8.0
Connection:
<jndi-name>jdbc/pims</jndi-name>
<connection-url>jdbc:postgresql://localhost:5432/pims</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<user-name>postgres</user-name>
Users:
DEV
PROD
I'm coding an app that uses Servlet 3.0, Jsp, and JPA 2.0 and i'm deploying it into Websphere application server 8.5.
Since i already configured into the ibm websphere console, the data source and the jdbc driver, and the j2c authentification (i'm using oracle 11g as a database ). I dont know how my persistence.xml should look like, if i need to specify and add openJPA jars to my project.
For now anything i put into persistence.xml i'm having this issue :
Error 500: <openjpa-2.2.3-SNAPSHOT-r422266:1764177 fatal user error> org.apache.openjpa.persistence.ArgumentException
What should i do ? maybe i'm missing how JPA works
Thanks in advance
The OpenJPA jars should be provided by WebSphere and available to use for your application. There is a JPA sample available here: https://developer.ibm.com/wasdev/downloads/#asset/samples-Java_Persistence_API_JPA_Sample
In the sample, you can see an example of the persistence.xml file:
<?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="jpasamplepersistenceunit">
<jta-data-source>java:comp/env/jdbc/samplejpadatasource</jta-data-source>
<non-jta-data-source>java:comp/env/jdbc/samplejpadatasourcenonjta</non-jta-data-source>
<class>wasdev.sample.jpa.Thing</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- These properties are creating the database on the fly. We are using them to avoid users having
to create a database to run the sample.
See also the create=true line in the datasource meta data. -->
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
<property name="openjpa.jdbc.DBDictionary" value="derby" />
<!-- EclipseLink specific properties to create the database. They are only used if using the jpa-2.1 feature. -->
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="both" />
</properties>
</persistence-unit>
</persistence>
I think your error is not caused by the OpenJPA jars not being available. It might be because your database is not configured correctly. Make sure your persistence.xml file refers to your datasources properly.
I am currently working on a Java EE project and am working with the Wildfly server.
I have a Web project and EJB project which are deployed onto the Wildfly server.
I can save a user for example, but only for as long as the server is running.
There is no data persistence between server downtimes.
I have searched through the internet but couldn't find an answer.
My persistence.xml looks like this:
<persistence-unit name="primary">
<!-- If you are running in a production environment, add a managed
data source, this example data source is just for development and testing! -->
<!-- The datasource is deployed as WEB-INF/kitchensink-quickstart-ds.xml, you
can find it in the source at src/main/webapp/WEB-INF/kitchensink-quickstart-ds.xml -->
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
<value="true"/>
</properties>
If I want to persist any information, do i need to reconfigure this file?
I hope you can help me :)
Your problem is this line
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
Everytime when the wildfly starts up, JPA creates a new database model with an empty database.
Adjust your code to
<property name="hibernate.hbm2ddl.auto" value="update" />
You are using "ExampleDS" which is set up as H2 in-memory database by default. It therefore does not persist data between restarts on purpose (useful for development/testing). Go to wildfly's standalone/configuration/standalone.xml configuration file and search for "ExampleDS" in the "datasources" section. It should show:
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
where "mem" means in-memory. You can change "mem:test" to any write path, e.g.
<connection-url>jdbc:h2:~/test;DB_CLOSE_DELAY=-1</connection-url>
to use a H2 file-based database stored as "test" in your home-folder (assuming *nix).
You can also define additional databases (Postgresql, Oracle, etc) in the datasources-section.
I have an application running on Glassfish 4.1 that uses a JDBC Resource. In the application itself I have a persistence.xml file that lists all the entities and tells the container which JDBC Resource to use. I have defined some properties to log the SQL it executes. It looks something like this:
<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="MyResource_PU" transaction-type="JTA">
<jta-data-source>jdbc/my_resource</jta-data-source>
<class>com.example.entities.EntityOne</class>
<class>com.example.entities.EntityTwo</class>
<class>com.example.entities.EntityThree</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
Now when this application goes to production, I don't want the SQL to be logged. So every time I do a release, I need to remind myself to change the eclipselink.logging properties.
I thought, there should be a better way to handle this. So I went to the Glassfish admin console, to JDBC Resources and added the 2 properties there, but that doesn't work.
Is there any way to remove the "environment specific" values out of the persistence.xml file and into the Glassfish configuration? I have googled for a while now, but don't seem to find the correct sollution. What I did find is that Hibernate has the option to specify a "configuration file", but I haven't found this for EclipseLink.
We used maintain property files for each environment such as DEV,QA,PROD,UAT etc in different files and copy one of them during build.
Ant build
<property environment="env" />
<!-- ***** COMMAND LINE ARGUMENTS DEMOED HERE -->
<property name="build_type" value= "${env.build_type}"/>
<copy todir="deploy">
<fileset dir="src_dir"/>
<globmapper from=${env.build_type}".persistence.xml" to="persistence.xml"/>
</copy>
Run build like this
ant -Denv.build_type=PROD
This will copy PROD.persistence.xml to persistence.xml
ant -Denv.build_type=DEV
This will copy DEV.persistence.xml to persistence.xml
As per glassfish documentation, persistence.xml settings are meant to take precedence over global settings, so I wouldn't recommend anything in a persistence.xml file that you would want to override later.
That said, EclipseLink server integration can make use of a server log, allowing external control over settings. See this for a description of setting logging properties in glassfish that should control the log file EclipseLink writes to. Otherwise, you can define a different log mechanism in your peristence.xml file, such as log4J or a custom one that you can control how you wish as touched on here
Well, this question may be a duplicate or something. But I am desperate. I am learning Java EE and I cant even set up all the configurations properly.
My situation:
I have a Wildfly 8.1- Jboss server, up and ready.
I have deployed datasource (SQL server) Its working.
It has this JNDI:
java:/TimeTrackerDS
And this driver: sqlserver
Driver Class: com.microsoft.sqlserver.jdbc.SQLServerDriver
All this seems to be okay. Now I am deploying my new Application.
And I just dont know how to use this staff to connect to database.
The application is also deployed. How to connect these 2 things (db and app)
I have found a lot of solutions. Write properties to persistance.xml But I cant get anything to work. Because I don't understand it. Maybe if someone from you guys, show it to me on my particular example. I would understand what I am doing wrong.
(The server, app and sql server are all at same machine )(localhost)
I found the solution.
I have a mistake in my entity java class. So it didnt compile properly.
Propper persistence.xml for someone who may need it :
<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="TimeTracker">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/TimeTrackerDS</jta-data-source>
<class>model.Task</class>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.jdbc.batch_size" value="20" />
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>