Hibernate

Working With Collection In Hibernate
Previous Home Next

Collection is widely used in hibernate.

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

<!-- Generated by MyEclipse Hibernate Tools.   -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">system</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="ListCollection.hbm.xml"/>
</session-factory>
</hibernate-configuration>

web.xml

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

<!-- Generated by MyEclipse Hibernate Tools.   -->
<hibernate-mapping>
<class name="r4r.Songs" table="songs">
<id name="id" type="int">
<generator class="increment" ></generator>
</id>
<property name="info"/>
</class>

<class name="r4r.Persons" table="persons">
<id name="id" type="int">
<generator class="increment"></generator>
</id>
<property name="name"/>
<bag name="song" table="songs1" cascade="all"> 
<key column="personid"/>
<many-to-many class="r4r.Songs" column="songid"/>
</bag>
</class>
</hibernate-mapping>

SessionProvider.java

package r4r;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SessionProvider {
static SessionFactory factory;
static
{
Configuration cfg=new Configuration().configure();
factory=cfg.buildSessionFactory();
}
public static Session getSession() {
return factory.openSession();
}
}

Persons.java

package r4r;

import java.util.List;
public class Persons {

int id;
String name;
List<Songs> song;

public Persons() {
super();
// TODO Auto-generated constructor stub
}
public Persons(String name, List<Songs> song) {
super();
this.name = name;
this.song = song;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Songs> getSong() {
return song;
}
public void setSong(List<Songs> song) {
this.song = song;
}
}

Songs.java

package r4r;

public class Songs {
int id;
String info; 

public Songs() {
super();
}
public Songs(String info) {
super();
this.info = info;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}

InsertTest.java

package r4r;

import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class InsertTest {
public static void main(String[] args) {
try
{
Configuration cfg=new Configuration().configure();
SessionFactory f=cfg.buildSessionFactory();
Session session=f.openSession();
ArrayList<Songs> list=new ArrayList<Songs>();
list.add(new Songs("mast mast laila"));
list.add(new Songs("dhink ckika"));
Persons p=new Persons("sushant",list);
Transaction t=session.beginTransaction();
session.save(p);
t.commit();

session.close();
System.out.println("successfully inserted");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: