Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to implement @Embeddable and @Embedded annotation in Hibernate Framework ?.

Program to demonstrate how to implement @Embeddable and @Embedded annotation in Hibernate Framework.


A simple POJO class (model) Customer.java

package com.hubberspot.hibernate.examples;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;

// @Entity to tell Hibernate that we need this Model
// class to get store in database as a table by name
// Customer
@Entity
public class Customer {

 // @Id to tell Hibernate that we need this Model
 // class having customerId as a Primary Key in the
 // table Customer created by @Entity
 @Id
 private int customerId;
 private String customerName;

 // @Embedded defines that customer address will be
 // embedded by an object called as Address
 // The value of Address will be provided by Address object
 // but in database the properties of Address will be embedded
 // into Customer table.
 @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;
 }
}




A simple POJO class (model) Address which is embedded into Customer

package com.hubberspot.hibernate.examples;

import javax.persistence.Embeddable;

// @Embeddable tells Hibernate that Address object 
// will be embedded into some other tables.
// The values of the properties will be populated
// into a table in which we want to embed the Address
@Embeddable
public class Address {

 private String street;
 private String city;
 private String state;
 private String zipCode;

 public String getStreet() {
  return street;
 }

 public void setStreet(String street) {
  this.street = street;
 }

 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 getZipCode() {
  return zipCode;
 }

 public void setZipCode(String zipCode) {
  this.zipCode = zipCode;
 } 

}




Hibernate Configuration XML file


<?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>
 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name="connection.url">jdbc:mysql://localhost/customerdb</property>
 <property name="connection.username">root</property>
 <property name="connection.password">root</property>

 <!-- SQL dialect -->
 <property name="dialect">
  org.hibernate.dialect.MySQL5Dialect
 </property>

 <!-- Drop and re-create the database schema on startup  -->
 <property name="hbm2ddl.auto">create</property>

 <!-- Mapping class entry into configuration xml for annotated classes -->
 <mapping class="com.hubberspot.hibernate.examples.Customer" />

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


A simple Test.java
package com.hubberspot.hibernate.examples;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateTest {
 public static void main(String[] args) {

  // Lets create a Customer Object 
  Customer customer = new Customer();
  customer.setCustomerId(1);
  customer.setCustomerName("Jonty");

  // Lets create a Address object which will be
  // embedded into Customer object
  Address address = new Address();
  address.setStreet("Street Name");
  address.setCity("City Name");
  address.setState("State Name");
  address.setZipCode("Zip-Code Value");

  customer.setCustomerAddress(address);

  // SessionFactory gives us a factory of sessions
  // Usually SessionFactory is been configured by the 
  // configuration file named as hibernate.cfg.xml
  // buildSessionFactory() builds the sessionFactory for us
  SessionFactory sessionFactory = new Configuration().
    configure().buildSessionFactory();

  // Session is created by calling openSession()
  // method on SessionFactory object
  Session session = sessionFactory.openSession();
  session.beginTransaction();

  // Our Transaction Code goes here 
  // i.e save, load , merge and remove
  // Lets save our Customer object created
  // above using sessions save method.
  session.save(customer);

  // Committing the transaction in order to save. 
  session.getTransaction().commit();
  // Closing the session
  session.close();
 }
}



Output of the program : 





















 




Video tutorial to demonstrate how to implement Component Mapping by @Embedded annotation in Hibernate using JPA.








Video tutorial to demonstrate how to create Composite Primary Keys by @Embeddable annotation in Hibernate using JPA.










 
© 2021 Learn Java by Examples Template by Hubberspot