Saturday, April 21, 2012

Hibernate Many to Many Example


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

        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

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

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

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

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>
---------------------------------------------------------------------------------------------

package com.hibernate.venkat;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

@Entity
public class Event {


private int eventId;
private String eventName;
private List<Delegate> delegate=new ArrayList<Delegate>();

@Id
@GeneratedValue
public int getEventId() {
return eventId;
}
public void setEventId(int eventId) {
this.eventId = eventId;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
@ManyToMany
@JoinTable(name="Join_Delegate_Event",
joinColumns={@JoinColumn(name="eventId")},
inverseJoinColumns={@JoinColumn(name="delegateId")})
public List<Delegate> getDelegate() {
return delegate;
}
public void setDelegate(List<Delegate> delegate) {
this.delegate = delegate;
}



}
---------------------------------------------------------------------------------------------
/**
 * 
 */
package com.hibernate.venkat;

import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.JoinTable;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author Venkat
 *
 */
public class TestEvent {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Event.class);
config.addAnnotatedClass(Delegate.class);
config.configure();
// new SchemaExport(config).create(true, true);
SessionFactory sf= config.buildSessionFactory();
Session s=sf.getCurrentSession();
s.beginTransaction();
// Delegate d1=new Delegate();
// d1.setDelegateName("Venkat");
// Delegate d2=new Delegate();
// d2.setDelegateName("Sunil");
// Delegate d3=new Delegate();
// d3.setDelegateName("Naresh");
// Delegate d4=new Delegate();
// d4.setDelegateName("Pavan");
// Delegate d5=new Delegate();
// d5.setDelegateName("Satya");
//
// Event e1=new Event();
// e1.setEventName("JAVA");
// Event e2=new Event();
// e2.setEventName("Hibernate");
// Event e3=new Event();
// e3.setEventName("Spring");
// Event e4=new Event();
// e4.setEventName("Flex");
// Event e5=new Event();
// e5.setEventName("Webservices");
//
// e1.getDelegate().add(d1);
// e1.getDelegate().add(d2);
//
// e2.getDelegate().add(d2);
// e1.getDelegate().add(d3);
// e3.getDelegate().add(d4);
// e2.getDelegate().add(d2);
// e2.getDelegate().add(d5);
// e4.getDelegate().add(d1);
//
// s.save(d1);
// s.save(d2);
// s.save(d3);s.save(d4);
// s.save(d5);
//
// s.save(e1);
// s.save(e2);
// s.save(e3);
// s.save(e4);
// s.save(e5);
//
Query q=s.createQuery("from Event");
List l=q.list();
for (Iterator iterator = l.iterator(); iterator.hasNext();) {
Event object = (Event) iterator.next();
System.out.println(object.getEventName());
}

}
}
---------------------------------------------------------------------------------------------

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

        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

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

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

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

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>


No comments:

Post a Comment