User Tools

Site Tools


zalozeni:prerequisities:persistence

persistence.xml

A JPA Persistence Unit is a logical grouping of user defined persistable classes (entity classes, embeddable classes and mapped superclasses) with related settings. Defining a persistence unit is optional when using ObjectDB, but required by JPA.

Ve vsech aplikacich markalru jsem data persistoval do MySQL DB na localhostu. Struktura persistence.xml odpovida nastaveni persistovani presne v tomto duchu.

persistence.xml musi byt umisteno v src/main/resources/META-INF/persistence.xml a vypada nasledovne

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
 
	<persistence-unit name="marklarDatabase" transaction-type="RESOURCE_LOCAL">
 
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
 
		<class>cz.jlochman.marklar.core.entityDomain.ExperimentInstance</class>
		<class>cz.jlochman.marklar.core.entityDomain.ExperimentInstanceSingleData</class>
		<class>cz.jlochman.marklar.core.entityDomain.ExperimentInstanceTimedData</class>
		<class>cz.jlochman.marklar.core.entityDomain.Sample</class>
		<class>cz.jlochman.marklar.core.entityDomain.GenericSetting</class>
 
		<properties>
 
			<property name="hibernate.show_sql" value="false" />
			<property name="hibernate.format_sql" value="true" />
 
			<property name="hibernate.connection.useUnicode" value="true" />
			<property name="hibernate.connection.characterEncoding" value="UTF-8" />
 
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost/testDB?useSSL=false&amp;useTimezone=true&amp;serverTimezone=UTC" /> 
			<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver" /> 
			<property name="hibernate.connection.username" value="root" /> 
			<property name="hibernate.connection.password" value="root" /> 
 
			<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" /> 
			<property name="hibernate.c3p0.min_size" value="1" /> 
			<property name="hibernate.c3p0.max_size" value="20" /> 
			<property name="hibernate.c3p0.max_statements" value="50" />
			<property name="hibernate.c3p0.acquire_increment" value="1" />
 
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
 
		</properties>
	</persistence-unit>
</persistence>

<persistence-unit name=“markalrDatabase” … /> musi zustat jak je (je definovano v jadre).

Casti mezi <class> jsou odkazy na persistable classes, ktere jsou definovany v jadre (pokud by tyto classy byly v projektu samotnem, tato deklarace by nebyla treba).

<property name=“hibernate.show_sql” value=“false” /> je mozno zapnout, pokud nastane nejaky problem.

<property name=“hibernate.connection.url” value=“jdbc:mysql://localhost/testDB?…” /> je treba nastavit spravny connectin string. Ve vsech aplikacich jsem pouzil MySQL DB na localhostu. DB je treba rucne vytvorit s prislusnym kodovanim (coding UTF8, collation UTF8_cs)

<property name=“hibernate.hbm2ddl.auto” value=“update” />. Hodnotal value=“…” bude nabyvat hodnot

  • create-drop (pri vytvareni projektu si celou DB znovu vytvorim)
  • update (jakmile projekt pracuje, data si uchovavam)

Ostatni parametry by nemelo byt treba menit.

zalozeni/prerequisities/persistence.txt · Last modified: 2016/07/22 09:50 by jlochman