|Home |Login |Registration |Struts Step by Step Tutorial |Hibernate Step by Step Tutorial |Spring Step by Step Tutorial |JSP Step by Step Tutorial |JDBC Step by Step Tutorial |Web Services Step by Step Tutorial |EJB fundamentals | ORACLE Step by Step Tutorial | SCJP 5.0 and SCJP 6.0 Study Guide | SCWCD 5.0 Study Guide | SCJP Tips
Java mock test | SCJP mock test | SCJP DUMP | SCBCD mock test |Java online test exam | JSP online test | ORACLE online test | Hibernate online test | Servlet online test | Struts online test | EJB online test | C online test | C++ online test | Aptitude online test

Java interview questions | JSP interview questions | ORACLE interview questions | Hibernate interview questions | Servlet interview questions | Struts interview questions | JDBC interview questions | C/C++ interview questions | Spring interview questions | JMS interview questions | Informatica interview questions | EJB interview questions | OOPS and Design Pattern interview questions
1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc) JobsJobs and Walkins

 

Recent Questions

!!! Hibernate Frequently Asked Questions !!!

Many To Many Relation In Hibernate ?

SCJP 1.5/1.6 Exam Kit

!!!Answer!!!- From Technical Expert

Best Example..for Many to Many in Hibernate ..
EVENTS ( uid int, name VARCHAR) Table
SPEAKERS ( uid int, firstName VARCHAR) Table
EVENT_SPEAKERS (elt int, event_id int, speaker_id int) Table
-----------------------------------------------------------
import java.util.Set;
import java.util.HashSet;

public class Speaker{

private Long id;
private String firstName;
private Set events;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public Set getEvents() {
return this.events;
}

public void setEvents(Set events) {
this.events = events;
}

private void addEvent(Event event) {
if (events == null) {
events = new HashSet();
}
events.add(event);
}
}
--------------------------------------------------------
import java.util.Date;
import java.util.Set;

public class Event{

private Long id;
private String name;
private Set speakers;

public void setId(Long id) {
this.id = id;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void setSpeakers(Set speakers) {
this.speakers = speakers;
}

public Set getSpeakers() {
return speakers;
}

}
--------------------------------------------------------------
Event.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="Event" table="events">
<id name="id" column="uid" type="long" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="name" type="string" length="100"/>
<set name="speakers" table="event_speakers" cascade="all">
<key column="event_id"/>
<many-to-many class="Speaker"/>
</set>
</class>
</hibernate-mapping>
------------------------------------------------------------------
Speaker.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="Speaker" table="speakers">
<id name="id" column="uid" type="long">
<generator class="increment"/>
</id>
<property name="firstName" type="string" length="20"/>
<set name="events" table="event_speakers" cascade="all">
<key column="speaker_id"/>
<many-to-many class="Event"/>
</set>
</class>
</hibernate-mapping>
----------------------------------------------------------------------
Save and Fetch Example
Event event = new Event();
event.setName("Inverse test");
event.setSpeakers(new HashSet());
event.getSpeakers().add(new Speaker("Ram", event));
event.getSpeakers().add(new SpeakerManyToMany("Syam", event));
event.getSpeakers().add(new SpeakerManyToMany("Jadu", event));
session.save(event); /// Save All the Data

event = (Event) session.load(Event.class, event.getId());
Set speakers = event.getSpeakers();

for (Iterator i = speakers.iterator(); i.hasNext();) {
Speaker speaker = (Speaker) i.next();
System.out.println(speaker.getFirstName());
System.out.println(speaker.getId());
}

Answered By : null Replied Date : Feb 7 2012
Answer :


Answered By : null Replied Date : Jan 11 2012
Answer :


Answered By : null Replied Date : Nov 29 2011
Answer :


Answered By : null Replied Date : Nov 19 2011
Answer :


Answered By : null Replied Date : Oct 25 2011
Answer :


Answered By : null Replied Date : Oct 13 2011
Answer :


Answered By : null Replied Date : Sep 21 2011
Answer :


Answered By : null Replied Date : Aug 5 2011
Answer :


Answered By : null Replied Date : Jun 22 2011
Answer :


Answered By : null Replied Date : Jun 18 2011
Answer :


Answered By : null Replied Date : Jun 17 2011
Answer :


Answered By : null Replied Date : Jun 6 2011
Answer :


Answered By : null Replied Date : May 7 2011
Answer :


Answered By : null Replied Date : Apr 24 2011
Answer :


Answered By : null Replied Date : Mar 19 2011
Answer :


Answered By : null Replied Date : Mar 9 2011
Answer :


Answered By : null Replied Date : Feb 25 2011
Answer :


Answered By : null Replied Date : Jan 15 2011
Answer :


Answered By : null Replied Date : Dec 28 2010
Answer :


Answered By : null Replied Date : Dec 28 2010
Answer :


Answered By : null Replied Date : Dec 15 2010
Answer :


Answered By : null Replied Date : Dec 14 2010
Answer :


Answered By : null Replied Date : Dec 12 2010
Answer :


Answered By : null Replied Date : Dec 11 2010
Answer :


Answered By : null Replied Date : Dec 10 2010
Answer :


Answered By : null Replied Date : Dec 7 2010
Answer :


Answered By : null Replied Date : Dec 4 2010
Answer :


Answered By : null Replied Date : Nov 28 2010
Answer :


Answered By : null Replied Date : Nov 20 2010
Answer :


Answered By : null Replied Date : Nov 15 2010
Answer :


Answered By : null Replied Date : Nov 7 2010
Answer :


Answered By : null Replied Date : Nov 5 2010
Answer :


Answered By : null Replied Date : Oct 31 2010
Answer :


Answered By : null Replied Date : Oct 30 2010
Answer :


Answered By : null Replied Date : Oct 28 2010
Answer :


Answered By : null Replied Date : Oct 28 2010
Answer :


Answered By : null Replied Date : Oct 27 2010
Answer :


Answered By : null Replied Date : Oct 23 2010
Answer :


Answered By : null Replied Date : Oct 20 2010
Answer :


Answered By : null Replied Date : Oct 18 2010
Answer :


Answered By : null Replied Date : Oct 16 2010
Answer :


Answered By : null Replied Date : Oct 3 2010
Answer :


Answered By : null Replied Date : Aug 21 2010
Answer :


Answered By : null Replied Date : May 8 2012
Answer :


Answered By : null Replied Date : Jul 15 2012
Answer :


Answered By : null Replied Date : Jul 28 2012
Answer :


Answered By : null Replied Date : Aug 22 2012
Answer :


Answered By : null Replied Date : Oct 10 2012
Answer :


Answered By : null Replied Date : Nov 17 2012
Answer :


Answered By : null Replied Date : Nov 29 2012
Answer :


Answered By : null Replied Date : Feb 18 2013
Answer :


Answered By : null Replied Date : Apr 26 2013
Answer :


 

You can also contribute to this answer:

Your Name:
Answer:

 
Ask Question and get answer from Expert.
View Answers List from Expert.

The information you are posting should be related to java and ORACLE technology. Not political.