config hibernate untuk menggenerate tabel

Dalam file config hibernate { hibernate.cfg.xml } terdapat property yang digunakan untuk meng-generate table yang berasal dari object model.
Gunakan property ini untuk meng-generate table.

<property name="hibernate.hbm2ddl.auto">create</property>

Contoh model yang akan di-generate adalah Contact

package com.ipi.myweb.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="CONTACT")
public class Contact implements Serializable{

@Id @GeneratedValue
@Column(name="ID")
private Long id;

@Column(name="NAMA", nullable=false,length=50)
private String nama;

@Column(name="NO_HP", nullable=false, length=15)
private String no_hp;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getNama() {
return nama;
}

public void setNama(String nama) {
this.nama = nama;
}

public String getNo_hp() {
return no_hp;
}

public void setNo_hp(String no_hp) {
this.no_hp = no_hp;
}

}

file hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/web</property>
<property name="connection.username">saifi</property>
<property name="connection.password">ahmada</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.min_size">1</property>
<property name="c3p0.idle_test_period">30</property>
<property name="cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
<property name="cache.use_second_level_cache">true</property>

<property name="show_sql">true</property>

<mapping class="com.ipi.myweb.model.Contact" />

</session-factory>
</hibernate-configuration>

Q:> jalankan aplikasi hibernate, maka pada database “web” akan terbentuk table CONTACT.

Log yang terlihat saat menjalankan server
2576 [main] INFO  org.hibernate.cfg.AnnotationBinder  – Binding entity from annotated class: com.ipi.myweb.model.Contact
2669 [main] INFO  org.hibernate.cfg.annotations.EntityBinder  – Bind entity com.ipi.myweb.model.Contact on table CONTACT

6507 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport  – Running hbm2ddl schema export
6508 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport  – exporting generated schema to database
8227 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport  – schema export complete

Q:> Cek database web

mysql -usaifi -p

masukkan password untuk user saifi , ****** ahmada.

use web;
desc contact;

Selamat ber-Hibernate ;D

Published by

saifiahmada

Begin a big Process with a bit trying

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s