Hibernate Select Query
This is an example for
loading the object from the database remember in the hibernate loading 1 object from the database means applying select command (
select * from _____) for fetching one complete record from the database.
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)
Product.java (POJO)
|
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
|
<?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
|
<?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
|
package str;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class ClientProgram {
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(101));
Product s=(Product)o;
// For loading Transaction scope is not necessary...
System.out.println("Loaded object product name is___"+s.getProName());
System.out.println("Object Loaded successfully.....!!");
session.close();
factory.close();
}
}
|
Now compile all .java files and run
ClientProgram.java to see the output
Eclipse Output
In the database
Note:
- In this program Product.java is just pojo class nothing special
- Mapping and Configuration files are just like previous programs
- But in ClientProgram.java, see in line number 16 load(-,-) method which is in the session, actually we have 2 methods to load the object from the database, they are load and get i will explain when time comes, as of now just remember this point
- Now see line number 19, we are going to print the product name by writing
+s.getProName
- Actually once we loaded the object for the database with load or get methods the object data will be loads into the Product.java(POJO) setter methods, so we are printing by using getter methods
No comments:
Post a Comment