Nggarap loro dhasar ing wektu sing padha
Cukup asring ana kahanan nalika sampeyan kudu nggarap rong basis bebarengan. Contone, sampeyan maca data saka siji database, ngolah lan nulis menyang liyane.
Utawa malah luwih umum. Aplikasi sampeyan duwe database lokal sing bisa digunakake, lan uga bisa maca data saka macem-macem sumber remot sing ndhukung format query data SQL. Contone Excel.
Kanggo sumber data remot iki, bisa migunani banget kanggo ngatur pemetaan Hibernate supaya logika bisnis aplikasi bisa luwih gampang lan mung bisa digunakake karo obyek Jawa.
Cara paling gampang kanggo nindakake iki yaiku nduwe pirang-pirang file konfigurasi. Contone, kaya iki:
hibernate_oracle.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">secret<property>
<mapping resource="com/codegym/hibernate/multipledatabase/employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate_mysql.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/employee</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">secret</property>
<mapping resource="com/codegym/hibernate/multipledatabase/employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Ya, kabeh kode inisialisasi Hibernate sejatine mung diduplikasi:
Configuration oracleCfg = new Configuration ( ) ;
oracleCfg. configure ( "/com/codegym/hibernate/multipledatabase/hibernate_oracle.cfg.xml" ) ;
SessionFactory oracleSessionFactory = oracleCfg. buildSessionFactory ( ) ;
Session oracleSession = oracleSessionFactory. openSession ( ) ;
Configuration mysqlCfg = new Configuration ( ) ;
mysqlCfg. configure ( "/com/codegym/hibernate/multipledatabase/hibernate_mysql.cfg.xml" ) ;
SessionFactory mySqlSessionFactory = mysqlCfg. buildSessionFactory ( ) ;
Session mysqlSession = mySqlSessionFactory. openSession ( ) ;
Employee emp = oracleSession. get ( Employee . class , "E0087" ) ;
System . out. println ( "Employee retrived from Oracle Database" ) ;
Transaction tx = mysqlSession. beginTransaction ( ) ;
mysqlSession. save ( emp) ;
tx. commit ( ) ;
System . out. println ( "Employee inserted in MySQL Database" ) ;
dhasar ing memori
Ana DBMS, yaiku database sing ana ing memori. DBMS kasebut asring disebarake mung minangka perpustakaan lan gampang dilebokake ing aplikasi khas. DBMS kuwi, contone, H2 utawa Hsqldb.
Sampeyan bisa nambahake menyang proyek kanthi cara sing padha karo perpustakaan liyane - nggunakake dependensi ing Maven:
hsqldb
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.6.1</version>
<scope>test</scope>
</dependency>
Lan langsung conto dhasar kapindho:
H2 database
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
<scope>test</scope>
</dependency>
Sampeyan bisa ngatur karya karo wong-wong mau ing cara sing padha karo database biasa. Nanging ayo menehi sawetara conto supaya gampang:
hsqldb
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbc.JDBCDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:mem:test</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
Bentenane utama antarane rong konfigurasi yaiku connection.url:
H2 database
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:mem:test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
GO TO FULL VERSION