MCQs Test!! VB Basic Test !! Essentials of VB !! Creating User Services !! function !! Anatomy !! GK March-2019 !! DATA TYPES IN JAVA !! Classes ,Constructor ,Methods,Inheritance !! Ecology-I !! Biology 2019 !! Gk april 2019 !! Biology (living world) !! Plant anatomy !! CELL STRUCTURES AND FUNCTION !! General biology !! Human physiology !! Data structure !! Ecology-II !! GK TEST !! Reproduction in plants !! Human reproduction !! The ABC of C !! Operating system set1 !! Operating system set2 !! Gk and current affairs !! Animal kingdom !! Number system !! Animal kingdom-II !! Gk TEST-III !! Number series !! Microorganisms !! Interview question in c !! Time and Work !! Percentage !! General history !! Average !! Cells !! Locomotion and Movement !! paging !! Bacteriology !! Page replacement policy !! Secondary memory and Disk scheduling !! Introduction of Nervous System and neurons !! Plant harmones !! Transport and Mineral Nutrition in Plants !! Neurotransmitters !! Modern Indian History !! Photosynthesis in Plants !! Modern Indian History-II !! Plant respiration !! Environment & Biodiversity !! Environment & Biodiversity - II !! Algae, Bryophytes, Pteridophytes !! Area !! Cricket !! Days of the Year (India and the World) !! Plant Pathology - I !! Indian Transport System !! Plant Pathology - II !! Aptitude !! Biology !! Botany !! C language !! Computer Science !! Core Java !! Gate !! GK !! Mysql !! Operating system !! Physics !! VB-Visual Basic !! Zoology

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