My project started with JPA only and it doesn't have Spring. Later, I added Liquibase and I had some issues with the persistence unit name since it is necessary to have one to be able to use EntityManager.
entityManagerFactory = Persistence.createEntityManagerFactory("MyPU");
So, to be able to continue with the tables creation with Liquibase and persisting into the database with JPA, I kept both persistence.xml and liquibase.properties files, despite contaning the same database configuration.
<?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_2.xsd"
version="2.2">
<persistence-unit name="MyPU">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa_specialist?createDatabaseIfNotExist=true&useTimezone=true&serverTimezone=UTC"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
changeLogFile=src/main/resources/META-INF/database/dbchangelog.xml
url=jdbc:mysql://localhost/jpa_specialist?createDatabaseIfNotExist=true&useTimezone=true&serverTimezone=UTC
username=root
password=root
I've taken a look at liquibase-hibernate and I didn't understand it very well but it seems to be used to generate the diff files, which is not my need at the moment.
Are both files necessary? Is there something I can do to have only one of them?
Liquibase doesn't have a direct way to read the url/username/password information from the presistence.xml file. The liquibase-hibernate extension does add support for diff'ing a database with your java file mapping files, but doesn't change how Liquibase gets the url/username/password.
You said you were not using Spring, but if you are still in a web application, you can use the Liquibase servlet listener to run Liquibase which pulls the connection from a pre-configured datasource. JPA can pull from that same pre-configured datasource instead of re-defining the configuration as well.
Otherwise, unless you want to do a bit of custom Java coding to parse the persistence.xml file and pass it into Liquibase, you do need both files.
To avoid the repetition you could do something like defining build properties in your maven/gradle/whatever setup and have <property name="javax.persistence.jdbc.url" value="${database.url}"/>in your persistence.xml source file, and url: ${database.url} in your liquibase.properties file.
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 havea java maven3 project in Eclipse IDE with jboss tools installed. I'm using hibenrate4
I'm triing to setup the hibenrate configuratoin in hibernate view to test hql queries, the problem is that is says that it couldnt find the persistence unit. I have the persistence.xml placed in src/main/resources/META-INF/persistence.xml
is there some maven config that I nead to set?
Persistence.xml
<persistence-unit name="ypay">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ypay</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<!-- <property name="hibernate.hbm2ddl.auto" value="validate" /> -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
</properties>
hibernate.properties
hibernate.connection.password=
hibernate.connection.username=root
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.url=jdbc:mysql://****************:3306/ypay?useUnicode=true&characterEncoding=UTF-8
hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider
hibernate.datasource=
hibernate.transaction.manager_lookup_class=
You need a META-INF/persistence.xml with
<persistence-unit name="ypay">
(usually put it in WEB-INF/classes/META-INF. When using maven you can try placing it in src/main/resources/META-INF)
Please refer to this doc for details.
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>
Hibernate is able to create database structure. Below is example how configuration looks using JPA2 with <property name="hibernate.hbm2ddl.auto" value="create"/>
( Use Hibernate Entity Manager in the case)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<!-- Uncomment the following two properties for JBoss only -->
<!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
<!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
</properties>
</persistence-unit>
</persistence>
How to make Hibernate to create database SQL script for initialization and update? (that could be run manually or via batch execution) and what tools to use for that.
Yes, there are SQL statements in log (Q Getting SQL script from Hibernate update ), but that is not clean solution.
The goal is to prepare solution for automated database upgrade with every version release.
For automated database upgrades, look at another tool specifically designed for the job. I would suggest Liqiubase.
I have one java project. In that I use #Entity. In my project have some constraints. Tables should be created automatically. So I use JPa #Entity. what the problem is that, when ever the Program is restarting, the new table is created by the Entity. So my old data got losed. I need those type of data. Trap Entity should not overwrite my content in Database? How to solve it?
use <property name="hibernate.hbm2ddl.auto" value="update"/> in your persistence.xml . If you have not generate the schema then first use <property name="hibernate.hbm2ddl.auto" value="create"/> and the schema will be generated. Then use <property name="hibernate.hbm2ddl.auto" value="update"/> since now you have the schema.
In the persistence.xml file there should be a setting to turn off the auto generation of DLL. Here is an example of a persistence.xml file using hibernate where the hibernate.hbm2ddl.auto property has been set to validate, which will cause the tables not to be overwritten. If your relying on Hibernate to create your DDL you would not want to have this set the first time your run the application, it would be set after running once and successfully creating the DDL (Tables).
If this setting were set to create-drop the schema would recreate the tables each time the persistence context is created, causing all data to be lost. This property will be specific to your ORM vendor's implementation.
The best advice is to find the documentation for your vendor's specific setting and then choose the option which best fits your needs. This post does a good job of describing the possible values for the setting in Hibernate.
<persistence-unit name="toThought" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
</properties>
</persistence-unit>
In EclipseLink this property is:
<property name="eclipselink.ddl-generation" value="create-tables"/>