|
|
|
}
/**
* @return Returns the lngInsuranceId.
*/
public long getLngInsuranceId() {
return lngInsuranceId;
}
/**
* @param lngInsuranceId The
lngInsuranceId to set.
*/
public void setLngInsuranceId(long
lngInsuranceId) {
this.lngInsuranceId = lngInsuranceId;
}
} |
Adding mappings to contact.hbm.xml file:
<class
name=”roseindia.tutorial.hibernate.Insurance”
table=”insurance”>
<id name=”lngInsuranceId” type=”long”
column=”ID” >
<generator class=”increment”/>
</id>
<property name=”insuranceName”>
<column name=”insurance_name” />
</property>
<property name=”investementAmount”>
<column name=”invested_amount” />
</property>
<property name=”investementDate”>
<column name=”investement_date” />
</property>
</class> |
Now here is the simplest code of SelectHQLExample.java that is using from
clause to get all records from the insurance
table.
package roseindia.tutorial.hibernate;
import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
public class SelectHQLExample { |
|
|
public static void main(String[] args) {
Session session = null;
try{
// This step will read hibernate.cfg.xml and
prepare hibernate for use
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Using from Clause
String SQL_QUERY =”from Insurance
insurance”;
Query query =
session.createQuery(SQL_QUERY);
for(Iterator
it=query.iterate();it.hasNext();){
Insurance
insurance=(Insurance)it.next();
System.out.println(“ID: “ +
insurance.getLngInsuranceId());
System.out.println(“First Name: “ +
insurance.getInsuranceName());
}
session.close();
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
}
}
} |
To run the example select Run-> Run As ->
Java Application from the menu bar. Following
out is displayed in the Eclipse console:
log4j:WARN No appenders could be found for
logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system
properly.
Hibernate: select insurance0_.ID as col_0_0_
from insurance insurance0_
ID: 1
Hibernate: select insurance0_.ID as ID0_,
insurance0_.insurance_name as
insurance2_2_0_,
insurance0_.invested_amount as
invested3_2_0_,
insurance0_.investement_date as
investem4_2_0_ from insurance insurance0_
where insurance0_.ID=?
First Name: Car Insurance |
|
|
Feb
2008 | Java Jazz Up |33 |
|
|
|
View All Topics |
All Pages of this Issue |
Pages:
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,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53 ,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63 ,
64,
65 ,
66 ,
67 ,
68 ,
69 ,
70 ,
71 ,
72 ,
Download PDF |
|
|
|
|
|
|
|
|
|