Thursday, 28 December 2017

List Collection componets based object mapping in Hibernate 4.x


In Hibernate you can also map a collection of component based objects by making object Embeddable  . like in following example we have Product and one product can have multiple image and image can have further its attribute like image name ,width length . so we put all image object in collection and map to it a single product object .

here we have

Product.java


package in.jk.list.component.maping;
import java.util.List;
 
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OrderColumn;
import javax.persistence.Table;

@Entity
@Table(name="Products")
public class Product {
 
@Id
 
private int productId;
private String productName;
private String price;

@ElementCollection
@CollectionTable(name="Prodcut_Images")
@OrderColumn(name="product_image_index")
private List<ProductImage> productImageList;

public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;

}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public List<ProductImage> getProductImageList() {
return productImageList;
}
public void setProductImageList(List<ProductImage> productImageList) {
this.productImageList = productImageList;

}
}


ProductImage.java

package in.jk.list.component.maping;
import javax.persistence.Embeddable;



 



@Embeddable
public class ProductImage {
 
private String imageName;
private int length;
private int width;

public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public int getLength() {
eturn length;
}
public void setLength(int length) {
this.length = length;

}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;


}
}

Hibernate.cfg.xml
<?xml version = "1.0" encoding = "utf-8"?>

<!DOCTYPE hibernate-configuration SYSTEM

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
<property name="hibernate.show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.ProgressDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<!-- Assume hibernate is the database name -->

<property name="hibernate.connection.url">jdbc:postgresql://localhost/hibernate</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<mapping class="in.jk.list.component.maping.Product" />

</session-factory>

</hibernate-configuration>
 
HibernateTest .java


package in.jk.list.component.maping;
 
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;


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

Configuration configuration = new Configuration();configuration.configure("hibernate.cfg.xml")
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
 
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();


 
Product product1 = new Product();
product1.setProductId(1);
product1.setProductName("Mobile");
product1.setPrice("20000");

ProductImage productImage1 = new ProductImage();

productImage1.setImageName("Mob1");
productImage1.setLength(12);
productImage1.setWidth(58);

ProductImage productImage2 = new ProductImage();


 
productImage2.setImageName("Mob2");
productImage2.setLength(18);
productImage2.setWidth(48);

ProductImage productImage3 = new ProductImage();

productImage3.setImageName("Mob3");
productImage3.setLength(23);
productImage3.setWidth(89);

List<ProductImage> imageList = new ArrayList<ProductImage>();

imageList.add(productImage1);
imageList.add(productImage2);
mageList.add(productImage3);
product1.setProductImageList(imageList);

session.persist(product1);


transaction.commit();

 
session.flush();
session.close();
System.out.println("Saved.............");

 


 

}
}

 
In Database Products table data looks like this .......
 
 
 
 
Product_image table with Prodcuct Image Data







 


 





 








       
 
 
 



 

 
  
 
 
 

 
 
 
 
 










 

 



 



 
 
 
       
 
 


       




 
       



 
 
 



 
       


       




 
       
 
 
       


















 


 



       









 



 












 




 






 
 
 






 









 





 




 




 

 



 




 





 




















No comments:

Post a Comment