|
|
| hibernate |
Expert:poonam
hi i am new to hibernate. i wan to run a select query with oracle database. could you please help me out. my files are:
package roseindia.tutorial.hibernate;
import org.hibernate.Session; import org.hibernate.*; import org.hibernate.cfg.*;
import java.util.*;
/** * @author Deepak Kumar * * http://www.roseindia.net * Select HQL Example */ 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{ }
} }
//Java file is:
package roseindia.tutorial.hibernate;
import java.util.Date; /** * @author Deepak Kumar * * http://www.roseindia.net * Java Class to map to the database insurance table */ public class Insurance { private long lngInsuranceId; private String insuranceName; private int investementAmount; private Date investementDate; /** * @return Returns the insuranceName. */ public String getInsuranceName() { return insuranceName; } /** * @param insuranceName The insuranceName to set. */ public void setInsuranceName(String insuranceName) { this.insuranceName = insuranceName; } /** * @return Returns the investementAmount. */ public int getInvestementAmount() { return investementAmount; } /** * @param investementAmount The investementAmount to set. */ public void setInvestementAmount(int investementAmount) { this.investementAmount = investementAmount; } /** * @return Returns the investementDate. */ public Date getInvestementDate() { return investementDate; } /** * @param investementDate The investementDate to set. */ public void setInvestementDate(Date investementDate) { this.investementDate = investementDate; } /** * @return Returns the lngInsuranceId. */ public long getLngInsuranceId() { return lngInsuranceId; } /** * @param lngInsuranceId The lngInsuranceId to set. */ public void setLngInsuranceId(long lngInsuranceId) { this.lngInsuranceId = lngInsuranceId; } }
//cfg file is:
<?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 name="session"> <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@172.16.31.25:1521:BOMBAY</property> <property name="hibernate.connection.username">poonam</property> <property name="hibernate.connection.password">poonam</property> <!-- Set AutoCommit to true --> <property name="connection.autocommit">true</property> <!-- Mapping files --> <mapping resource="contact.hbm.xml"/> </session-factory> </hibernate-configuration>
//hgf file is:
<?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="roseindia.tutorial.hibernate.Contact" table="CONTACT"> <id name="id" type="long" column="ID" > <generator class="assigned"/> </id>
<property name="firstName"> <column name="FIRSTNAME" /> </property> <property name="lastName"> <column name="LASTNAME"/> </property> <property name="email"> <column name="EMAIL"/> </property> </class> </hibernate-mapping>
plz help me as i m unable to do it....... thanks a lot..in advance..for ur help...... |
| Answers |
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|