code: address.hbm.xml: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="javabeat.net.hibernate.Address" table="ADDRESS"> <meta attribute="class-description">This class contains the student's address details.</meta> <id name="addressId" type="long" column="ADDRESS_ID"> <generator class="increment" /> </id> <property name="street" column="ADDRESS_STREET" type="string" length="250" /> <property name="city" column="ADDRESS_CITY" type="string" length="50" /> <property name="state" column="ADDRESS_STATE" type="string" length="50" /> <property name="zipcode" column="ADDRESS_ZIPCODE" type="string" length="10" /> </class> </hibernate-mapping>
=======================================================================================
hibernate.configuration file: <?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="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property> <property name="hibernate.connection.url">jdbc:derby://localhost:1527/hkbk</property> <property name="hibernate.connection.username">root</property> <property name="connection.password">root</property> <property name="connection.pool_size">1</property> <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">create-drop</property> <!-- mapping resource--> <mapping resource="javabeat/net/hibernate/Student.hbm.xml"/> <mapping resource="javabeat/net/hibernate/Address.hbm.xml"/> <mapping resource="javabeat/net/hibernate/Course.hbm.xml"/> </session-factory> </hibernate-configuration> ==========================================================================================
code to enter records in data base: /* * To change this template, choose Tools | Templates * and open the template in the editor. */
package javabeat.net.hibernate;
/** * * @author juahmed */ import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration;
public class JavaBeatHibernateExample {
public static void main(String[] args) { Session session = null; SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session =sessionFactory.openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); Address address = new Address("OMR Road", "Bangalore", "KARNATAKA", "560008"); //By using cascade=all option the address need not be saved explicitly when the student object is persisted the address will be automatically saved. //session.save(address); Set<Course> courses = new HashSet<Course>(); courses.add(new Course("Maths")); courses.add(new Course("Computer Science")); Student student1 = new Student("Eswar", address, courses); Student student2 = new Student("Joe", address, courses); session.save(student1); session.save(student2); transaction.commit(); } catch (Exception e) { transaction.rollback(); e.printStackTrace(); } finally { session.close(); }
}
} ======================================================== code to update the database record:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package javabeat.net.hibernate;
/** * * @author juahmed */ import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration;
public class JavaBeatHibernateExampleUpdate {
public static void main(String[] args) { Session session = null; SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session =sessionFactory.openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); Address addr = (Address)session.load(Address.class, new Long(1)); addr.setCity("Bangalore"); addr.setState("Karnataka"); addr.setStreet("bowee"); addr.setZipcode("560008"); session.update(addr); transaction.commit(); } catch (Exception e) { transaction.rollback(); e.printStackTrace(); } finally { session.close(); }
}
} ================== address record is present in database with id=1.
please help me.
|