Newsletter

Hibernate Many To One Annotation Example

Hibernate » on Aug 30, 2011 { 4 Comments } By Sivateja

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

package 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

package 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

<?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

package 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

 

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

4 Responses to “Hibernate Many To One Annotation Example”
  1. ramana says:

    can you post how can we maintain the trasactions in hibernate with explaination?

  2. sandhya says:

    In ForOurLogic.java program on line 49 it must be session.save(c1)… please correct it.. thankyou

  3. manjesh says:

    I learn Spring and hibernate from this website. now I am working spring with hibernate very comfortably..

    Thank you

  4. While executing the above example, I am getting error, Kindly please have a look…

    Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.hibernate_1.Customers
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1443)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:116)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
    at org.hibernate_1.ForOurLogic.main(ForOurLogic.java:41)

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.