Hibernate operations(Insert,Delete,Retrieve and Update data) with Mysql.

Filed under: Hibernate — 7 Comments 2011/12/25

5 Votes

Firstly you should want to create a database.It may be can do using Mysql server or Eclipse IDE. Window—–>Open Perspective—–>Other—–>Database Development.In that window has Database Connection link.Right click it and select NEW.After that select MYSQL and give the DATABASE NAME.After that select NEXT. In that window you should want to select MYSQL JDBC DRIVER(download it) for Drivers.And also give USERNAME and PASSWORD.After completed all of those you can test your connection using TEST CONNECTION button.If you can see PING SUCCESSFULLy you have not any errors.Now you can click FINISH Button. Now your package has two files.Those are hibernate.cfg.xml and log4j.properties. hibernate.cfg.xml file should want to edit like this.
<?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">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/MilindaFirst</property> <property name="connection.username">UserName</property> <property name="connection.password">PassWord</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">2</property> <!-- SQL dialect -->

hibernate. import javax. private int age.Enable Hibernate's current session context --> <property name="current_session_context_class">thread</property> <!-. . } public void setId(int id) { this. } } Data Insertion import import import import org.<property name="dialect">org. } public String getName() { return name. @Id public int getId() { return id.hibernate.Disable the second-level cache --> <property name="cache. } public void setAge(int age) { this.provider_class">org.hibernate. org.persistence.NoCacheProvider</property> <!-. } public void setName(String name) { this. import javax.persistence.AnnotationConfiguration.hbm2ddl.name = name.hibernate.dialect.MySQLDialect</property> <!-.SessionFactory.hibernate.age = age.classic.hibernate.tool. } public int getAge() { return age. org.SchemaExport.Entity. org.cache. private String name. @Entity public class Person { private int id.id = id.Echo all executed SQL to stdout --> <property name="show_sql">true</property> </session-factory> </hibernate-configuration> Secoundly you want create Persistence class.Id.cfg.Session.

//create new schema(db and table) new SchemaExport(config).xml").addAnnotatedClass(Person.beginTransaction().buildSessionFactory().hibernate.commit().create(true.public class TestPerson { public static void main(String[] args) { //create configurations for Person class AnnotationConfiguration config = new AnnotationConfiguration().//save permanently to db(end of db operations) } Data Deletion.AnnotationConfiguration.hbm2ddl.xml").getCurrentSession().cfg. config. import import import import org.cfg.SessionFactory. config.//beginning of the transaction String searchId = "1".buildSessionFactory().hibernate.id = :key". . SessionFactory factory = config.configure("hibernate.//this is the key id for field being updated //create the delete query String query = "delete from Person as p where p.classic. config.SchemaExport. org. session.tool.configure("hibernate. org.setName("Sandun").hibernate.class).getTransaction().//create the object from persistent class //set data that you want to store in db person. person. //beginning of the session Session session = factory.addAnnotatedClass(Person. config. true).Session.setAge(22). person.beginTransaction().//save the object to db session.hibernate. public class TestPerson { public static void main(String[] args) { AnnotationConfiguration config = new AnnotationConfiguration().//begin the db operations //Insertion to the table Person person = new Person().cfg. session.save(person).class). Session session = factory.setId(1).openSession(). org.//comment this line if you already created the table Person SessionFactory factory = config. session.

SchemaExport.//this is the key id for field being updated //create the update query String query = "update Person as p set p.List.name = :newName where p.classic. //beginning of the session Session session = factory.commit().close().//beginning of the transaction String name = "New_Milinda". session.AnnotationConfiguration.openSession().buildSessionFactory().cfg.close(). import java.AnnotationConfiguration. org.classic. org.hibernate.hibernate.id = :keyId". SessionFactory factory = config. import org.getTransaction(). name)//this will set the string 'newName' to name variable . id) .executeUpdate().hibernate.SessionFactory.hibernate.hbm2ddl.getTransaction().Session.executeUpdate(). . session. org.//end of session } } Data Updating.SessionFactory. session.setString("key".hibernate. public class TestPerson { public static void main(String[] args) { AnnotationConfiguration config = new AnnotationConfiguration(). import java.util.hbm2ddl. config. session. import import import import org.setString("newName". //import org. searchId).class).beginTransaction().tool.//end of transaction session.import org.hibernate.cfg.session.createQuery(query).hibernate.addAnnotatedClass(Person.//end of transaction session.createQuery(query) . config.//end of session } } Data Retreive.xml").cfg.commit().util.tool.Session.SchemaExport.setString("keyId".hibernate. import org.configure("hibernate.Iterator.//this is new name for 'id' String id = "1".

getId() + "\t" + obj.getName() + "\n"). .class).cfg.getCurrentSession().getAge() + "\t" + obj.list(). SessionFactory factory = config.id = :sId".addAnnotatedClass(Person. session.iterator().next(). Session session = factory.setString("sId".//cast and assign next data to Person type object System.beginTransaction(). String query = "from Person as p where p.print(obj. searchId).public class TestPerson { public static void main(String[] args) { AnnotationConfiguration config = new AnnotationConfiguration(). Iterator iterator = list.hasNext()) {//is next data exist? then loop Person obj = (Person) iterator. } session. String searchId = "1".xml"). while (iterator.createQuery(query) .close(). config.configure("hibernate.out. config. List list = session.buildSessionFactory().

Sign up to vote on this title
UsefulNot useful