Konfigurasi Hibernate

All lectures for JV purposes
tingkat , wulangan
kasedhiya

5.1 Nggawe skema data otomatis

Nalika sampeyan pisanan ngatur Hibernate, sampeyan bisa ngaktifake akeh setelan menarik. Aku ora nggawa sadurunge, supaya ora nyemprot. Nanging ing mburi tingkat aku mikir bab sawetara wong marang.

Setelan kasebut pisanan yaiku hbm2ddl.auto. Bisa duwe 5 nilai sing beda:

validasi Validasi: Hibernate bakal mriksa apa jeneng kolom lan kolom lan jinis cocog ing database lan ing anotasi. Iki minangka mode sing paling umum.
nganyari Update: Hibernate bakal nganyari tabel ing database yen padha utawa kolom sing beda saka samesthine.
nggawe Gawe maneh: Hibernate bakal mbusak kabeh tabel ing database lan nggawe maneh adhedhasar data saka anotasi.
nggawe-nyelehake Titah-pambusakan. Ing wiwitan karya, Hibernate bakal nggawe kabeh tabel, ing pungkasan karya, bakal dibusak sawise dhewe.
ora ana Hibernate ora bakal nindakake apa-apa. Yen ing endi wae dhasar ora cocog karo pangarepan, banjur kesalahan bakal dibuwang sajrone eksekusi pitakon.

5.2 Log panjalukan

Setelan Hibernate sing kapindho sing migunani yaiku logging kabeh panjaluk menyang database: kabeh panjalukan kanggo database diduplikasi ing konsol. Iki minangka fitur sing migunani banget yen sampeyan ngganti kode sing gegandhengan karo Hibernate.

Pisanan, sampeyan bakal luwih ngerti carane pitakon sampeyan diowahi dadi SQL. Kapindho, luwih gampang lan luwih awal sampeyan bakal bisa nemokake kesalahan. Lan mesthi bakal. Hibernate ora mesthi bisa kaya sing dikarepake. Iki asring digandhengake karo anotasi: sampeyan ngerti kanthi cara sampeyan dhewe, lan Hibernate kanthi cara sampeyan dhewe.

Setelan sing mbisakake logging diarani hibernate.show_sql. Yen sampeyan nyetel nilai kasebut dadi bener, pitakon menyang database bakal ditulis ing console. Parameter kasebut uga digunakake bebarengan karo hibernate.format_sql, sing ngidini sampeyan nyetel format query SQL sing trep ing log.

Cara liya kanggo nyathet panjalukan menyang database yaiku nggunakake logger standar . Kabeh menehi kasunyatan manawa Hibernate wis nulis pitakon menyang logger standar, nanging mung karo ruang lingkup - DEBUG. Sampeyan kudu ngganti rong properti ing logger standar:

logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace

Ngganti level BasicBinder bakal tracenambah paramèter pitakon kanggo kita, nanging ing wangun sing rada ora biasa - enumerasi urutan sawise pitakon kasebut.

Pendekatan katelu yaiku nggunakake driver proxy khusus kanggo basis data .

Contone, log4jdbcutawa p6spy. Kaloro proxy kasebut digunakake lan duwe wiwitan, sanajan log4jdbcora ana komitmen kanggo wektu sing suwe nalika nulis.


<dependency>
    <groupId>com.integralblue</groupId>
    <artifactId>log4jdbc-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>com.github.gavlyukovskiy</groupId>
    <artifactId>p6spy-spring-boot-starter</artifactId>
</dependency>

Aku ora bakal pindhah menyang rincian, Aku mung pengin sampeyan ngerti sing iki bisa.

5.3 Dialek SQL

Lan sawetara informasi latar mburi liyane.

Hibernate ndhukung akeh banget DBMS. Saben wong nindakake set standar fungsi SQL lan sawetara liyane dhewe. Utawa macem-macem versi SQL. Mula, kanggo nggarap DBMS iki, sampeyan kudu ngandhani Hibernate dialek basa SQL sing bakal digunakake.

Ing ngisor iki dhaptar dialek sing paling populer:

PostgreSQL org.hibernate.dialect.PostgreSQLDialect
SAP DB org.hibernate.dialect.SAPDBDdialect
Sybase org.hibernate.dialect.SybaseDialect
Informix org.hibernate.dialect.InformixDialect
Microsoft SQL Server 2008 org.hibernate.dialect.SQLServer2008Dialect
MySQL org.hibernate.dialect.MySQLDialect
Oracle (versi apa wae) org.hibernate.dialect.OracleDialect
Oracle 11g org.hibernate.dialect.Oracle10gDialect

Mesthi wae, bisa uga ana sawetara dialek kasebut. Sampeyan bisa nulis DBMS lan dialek sampeyan dhewe.

Apa sebabe pentinge nemtokake dialek sing bener?

Saben basis data bisa uga duwe jinis data sing rada beda. Mula, supaya Hibernate bisa mlaku kanthi sampurna kaya sing dikarepake, sampeyan kudu ngandhani dialek basa SQL sing kudu digunakake.

Punika gambar menarik sing nuduhake carane kabeh disambungake ing kasunyatan:

5.4 Setelan populer

Kanggo nggawe urip luwih gampang, iki dhaptar lengkap sawetara setelan:

MySQL 8.0
hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/test
hibernate.connection.username=root
hibernate.connection.password=secret
hibernate.show_sql=true
hibernate.hbm2ddl=validate
MySQL 5.0
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/test
hibernate.connection.username=root
hibernate.connection.password=secret
hibernate.show_sql=true
hibernate.hbm2ddl=validate
PostgreSQL
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class= org.postgresql.Driver
hibernate.connection.url= jdbc:postgresql://localhost/test
hibernate.connection.username=root
hibernate.connection.password=secret
hibernate.show_sql=true
hibernate.hbm2ddl=validate

MySQL 8.0 ndhukung fitur luwih akeh tinimbang MySQL 5.0, dadi yen sampeyan pengin Hibernate ngoptimalake, banjur nemtokake dialek sing bener.

Database H2 biasane disimpen ing memori, uga mem:testjeneng server SQL lan jeneng skema sing bakal digunakake.

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION