Hibernate Update Query
This is the program to
update an object (1 complete row) in the database, which is
already persisted in the database, then we have the following two approaches…
Approach 1
Load that object from the database, and
modify its values, now hibernate automatically modifies the values on to
database also, when ever the transaction is
committed.
Approach 2:
If we want to modify object in the database, then create
new object with same
id and we must call
update() given by session interface.
Files required to execute this program..
- Product.java (My POJO class)
- product.hbm.xml (Xml mapping file )
- hibernate.cfg.xml (Xml configuration file)
- ClientProgram.java(java file to write our hibernate logic)
Related to approach 1:
Product.java (POJO)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package str;
public class Product{
private int productId;
private String proName;
private double price;
public void setProductId(int productId)
{
this.productId = productId;
}
public int getProductId()
{
return productId;
}
public void setProName(String proName)
{
this.proName = proName;
}
public String getProName()
{
return proName;
}
public void setPrice(double price)
{
this.price = price;
}
public double getPrice()
{
return price;
}
}
|
Product.hbm.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?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="str.Product" table="products">
<id name="productId" column="pid" />
<property name="proName" column="pname" length="10"/>
<property name="price"/>
</class>
</hibernate-mapping>
|
hibernate.cfg.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?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="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="Product.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
|
ClientProgram.java (* Recommended way)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package str;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class ForOurLogic {
public static void main(String[] args)
{
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Object o=session.load(Product.class,new Integer(105));
Product s=(Product)o;
Transaction tx = session.beginTransaction();
//s.setStno(105); should not update, because we loaded with that number right..?
s.setPrice(4000); // implicitly update method will be called..
tx.commit();
System.out.println("Object Updated successfully.....!!");
session.close();
factory.close();
}
}
|
Notes:
- See line number 20, actually there i tried to update
Stno(105),
we
should not do this, because we have loaded the object from the database
with his id number only, see line number 16, if we update hibernate
will rises the exception
- See line number 24 once we call the commit(), automatically update method will be called by hibernate.
- When ever an object is loaded from the database then hibernate stores the loaded object in cache-memory maintained by session-interface
- Once an object is loaded, if we
do any modifications on that object by calling its setter methods, then
these modification are stored in the object maintained by cache-memory
- if we modify the loaded object for multiple times then also the
modifications will be stored in object maintained by the cache-memory
only.
- when ever we issue commit()
operation then hibernate verify whether any changes are there between
the object stored in the cache and object in the database, if changes
exists then hibernate automatically updates the database by generating any update operation.
- What am saying is hibernate automatically maintains synchronization between cache-memory object and database table objects (rows)
Related to approach 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<strong> </strong>package str;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class ForOurLogic {
public static void main(String[] args)
{
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Product p=new Product();
p.setProductId(104); // 104 must be in the DB
p.setProName("Someting");
Transaction tx = session.beginTransaction();
session.update(p);
tx.commit();
System.out.println("Object Updated successfully.....!!");
session.close();
factory.close();
}
}
|
Oracle DB, Before Run The Program
Oracle DB, After Run the program
Notes:
- From line number 17 to 19, we created new object and modified, and in line number 22 we are calling update method explicitly
- Here we no need to load an object from the database
- we will create a new object, and we will assign same id no’s to it and we will call update() explicitly in order to make the changes on the object that is stored in the database
That’s it, actually first approach is recommended always..
No comments:
Post a Comment