Gunakan property ini untuk meng-generate table.
[sourcecode language="xml"]<property name="hibernate.hbm2ddl.auto">create</property>[/sourcecode]
Contoh model yang akan di-generate adalah Contact
[sourcecode language="java"]
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;
}
}
[/sourcecode]
file hibernate.cfg.xml
[sourcecode language="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>[/sourcecode]
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
[sourcecode language="bash"]mysql -usaifi -p[/sourcecode]
masukkan password untuk user saifi , ****** ahmada.
[sourcecode laguage="sql"]use web;[/sourcecode]
[sourcecode laguage="sql"]desc contact;[/sourcecode]
Selamat ber-Hibernate ;D
No comments:
Post a Comment