Home > Java > Hibernate Component Mapping

Hibernate Component Mapping

December 14, 2013 Leave a comment Go to comments

Berikut ini contoh component mapping menggunakan annotation pada Hibernate

1.Table

Table people

Table People

Table People

2. Entity Class

Address class ( component )

package pojo;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Address implements Serializable {
@Column(name="address_street")
private String addressStreet;
@Column(name="address_city")
private String addressCity;
@Column(name="address_zipcode")
private String addressZipcode;

public Address(){
}

public Address(String addressStreet, String addressCity, String addressZipcode){
this.addressStreet =addressStreet;
this.addressCity   = addressCity;
this.addressZipcode =addressZipcode;
}
public String getAddressStreet() {
return addressStreet;
}

public void setAddressStreet(String addressStreet) {
this.addressStreet = addressStreet;
}

public String getAddressCity() {
return addressCity;
}

public void setAddressCity(String addressCity) {
this.addressCity = addressCity;
}

public String getAddressZipcode() {
return addressZipcode;
}

public void setAddressZipcode(String addressZipcode) {
this.addressZipcode = addressZipcode;
}

}

People class

package pojo;

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

@Entity
@Table(name="people")
public class People {
@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="job")
private String job;
@Embedded
private Address address;

public People(){
}

public People(String name, String job, Address address){
this.name = name;
this.job = job;
this.address = address;
}
public int getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getJob() {
return job;
}

public void setJob(String job) {
this.job = job;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}
}

3. Hibernate Configuration File

<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/jointrain?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql">true</property>
<mapping clas="pojo.Address"/>
<mapping clas="pojo.People"/>
</session-factory>
</hibernate-configuration>

4. Hibernate Utility Class

package componentmapping;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

5. Main Class

package componentmapping;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.Address;
import pojo.People;

public class ComponentMapping {

public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();

Address address= new Address("Tegal Parang","Jakarta","172111");
People people = new People("Kukuh Utama", "Programmer", address);

session.save(people);

session.getTransaction().commit();
session.close();
}
}

6. Project Structure

Project Structure

Project Structure

 
Referensi : dari berbagai sumber.
Semoga bermanfaat.

  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: