The Embedded and Embeddable Example in Hibernate

In this tutorial, I am attaching simple application about, usage of Embedded and Embeddable Example in Hibernate framework. The application were tested with MySQL database environment and shared in the same post.

Project Structure

Embedded and Embeddable Example in Hibernate

@Embeddable

Address.java

package com.dineshkrish.hibernate;
import javax.persistence.Embeddable;
@Embeddable
public class Address {
private String city;
private String state;
private String country;
private String zipCode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
}

@Embedded

Customer.java

package com.dineshkrish.hibernate;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Customer {
@Id
private int customerId;
private String customerName;
private int customerAge;
@Embedded
private Address customerAddress;
public Address getCustomerAddress() {
return customerAddress;
}
public void setCustomerAddress(Address customerAddress) {
this.customerAddress = customerAddress;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public int getCustomerAge() {
return customerAge;
}
public void setCustomerAge(int customerAge) {
this.customerAge = customerAge;
}
}

Getting Session for Transaction

SessionProvider.java

package com.dineshkrish.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* 
* @author Dinesh Krishnan
*
*/
public class SessionProvider {
private static Session session;
private static Configuration configuration;
private SessionProvider() { }
static {
configuration = new Configuration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/dineshkrish")
.setProperty("hibernate.connection.username", "root")
.setProperty("hibernate.connection.password", "")
.setProperty("hibernate.show_sql", "true")
.setProperty("hibernate.hbm2ddl.auto", "update")
// Your Mapping Class
.addAnnotatedClass(com.dineshkrish.hibernate.Customer.class);
}
public static Session getSession() {
SessionFactory factory = configuration.buildSessionFactory();
session = factory.openSession();
return session;
}
}

CustomerApplication.java

package com.dineshkrish.hibernate;
import org.hibernate.Session;
public class CustomerApplication {
public static void main(String[] args) {
// Defining Customer Object
Customer customer = new Customer();
customer.setCustomerId(101);
customer.setCustomerName("Dinesh Krishnan");
customer.setCustomerAge(28);
// Defining the Address Object
Address customerAddress = new Address();
customerAddress.setCity("New York");
customerAddress.setState("New York");
customerAddress.setCountry("US");
customerAddress.setZipCode("10005");
// Setting Address to Customer
customer.setCustomerAddress(customerAddress);
Session session = SessionProvider.getSession();
session.beginTransaction();
// Saving the Customer Object to Database
session.save(customer);
session.getTransaction().commit();
session.close();
}
}

Output

Embedded and Embeddable Example

References

1. Hibernate 4.3 JavaDoc
2. JPA 2.1 Embeddable JavaDoc
3. JPA 2.1 Embedded JavaDoc

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *