Hibernate Many To One Annotation Example
Will find the example on hibernate many to one mapping using annotations
Files required…
- Customers.java
- Vendor.java
- hibernate.cfg.xml
- ForOurLogic.java
Customers.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950package str; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name = "Customers") public class Customers{ @Id @Column(name = "custid") private int customerId; @Column(name = "custName", length=10) private String customerName; @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name="venid",referencedColumnName="vid") private Vendor parent; 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 Vendor getParent() { return parent; } public void setParent(Vendor parent) { this.parent = parent; } }
Vendor.java
123456789101112131415161718192021222324252627282930313233343536373839404142package str; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name = "Vendor") public class Vendor{ @Id @Column(name = "vid") private int vendorId; @Column(name = "vname", length=10) private String vendorName; public int getVendorId() { return vendorId; } public void setVendorId(int vendorId) { this.vendorId = vendorId; } public String getVendorName() { return vendorName; } public void setVendorName(String vendorName) { this.vendorName = vendorName; } }
hibernate.cfg.xml
123456789101112131415161718192021<?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">oracle.jdbc.driver.OracleDriver </property> <property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property> <property name="connection.username">system</property> <property name="connection.password">admin</property> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping></mapping> <mapping></mapping> </session-factory> </hibernate-configuration>
ForOurLogic.java
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960package str; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; public class ForOurLogic { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory(); Session session = factory.openSession(); Vendor v =new Vendor(); v.setVendorId(100); v.setVendorName("java4s6"); Customers c1=new Customers(); c1.setCustomerId(504); c1.setCustomerName("customer4"); c1.setParent(v); Customers c2=new Customers(); c2.setCustomerId(505); c2.setCustomerName("customer5"); c2.setParent(v); Customers c3=new Customers(); c3.setCustomerId(506); c3.setCustomerName("customer6"); c3.setParent(v); Transaction tx = session.beginTransaction(); //session.save(v); session.save(c2); session.save(c2); session.save(c3); tx.commit(); session.close(); System.out.println("Many to One with annotation done...!!"); factory.close(); } }
Output
post a comment