Hi all...
I m new to hibernate and trying some examples to know this...
i struck in while retrieve records from database.... and getting error..
java.lang.IllegalStateException: Could not locate SessionFactory in JNDIThe below code is in package structure
com.mybaby.entityCode:
import static org.hibernate.criterion.Example.create;
import java.io.File;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Home object for domain model class DentalRecord.
* @see com.mybaby.entity.DentalRecord
* @author Hibernate Tools
*/
public class DentalRecordHome {
private static final Log log = LogFactory.getLog(DentalRecordHome.class);
private final SessionFactory sessionFactory = getSessionFactory();
protected SessionFactory getSessionFactory() {
try {
// return (SessionFactory) new InitialContext()
// .lookup("SessionFactory");
return new Configuration().configure("../../src/hibernate.cfg.xml").buildSessionFactory();
} catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException(
"Could not locate SessionFactory in JNDI");
}
}
public void persist(DentalRecord transientInstance) {
log.debug("persisting DentalRecord instance");
try {
sessionFactory.getCurrentSession().persist(transientInstance);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
public void attachDirty(DentalRecord instance) {
log.debug("attaching dirty DentalRecord instance");
try {
sessionFactory.getCurrentSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(DentalRecord instance) {
log.debug("attaching clean DentalRecord instance");
try {
sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void delete(DentalRecord persistentInstance) {
log.debug("deleting DentalRecord instance");
try {
sessionFactory.getCurrentSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public DentalRecord merge(DentalRecord detachedInstance) {
log.debug("merging DentalRecord instance");
try {
DentalRecord result = (DentalRecord) sessionFactory
.getCurrentSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public DentalRecord findById(java.lang.String id) {
log.debug("getting DentalRecord instance with id: " + id);
try {
DentalRecord instance = (DentalRecord) sessionFactory
.getCurrentSession().get("com.mybaby.entity.DentalRecord",
id);
if (instance == null) {
log.debug("get successful, no instance found");
} else {
log.debug("get successful, instance found");
}
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List<DentalRecord> findByExample(DentalRecord instance) {
log.debug("finding DentalRecord instance by example");
try {
List<DentalRecord> results = (List<DentalRecord>) sessionFactory
.getCurrentSession().createCriteria(
"com.mybaby.entity.DentalRecord").add(
create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public static void main(String arg[])
{
/*DentalRecord dr=new DentalRecord();
dr.setBabyId("mybaby");
dr.setDeseaseDiagnosed("yes");
dr.setVDate(new Date());
dr.setFinding("nothing");
dr.setTeeth("first");
dr.setLastUpdated(new Date());
Session sess=null;
try {
sess=new DentalRecordHome().sessionFactory.openSession();
Transaction trx=sess.beginTransaction();
sess.save(dr);
System.out.println("The record is inserting to database.....");
trx.commit();
sess.flush();
sess.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println("The ex="+e);
}*/
//BabyDetails bd=new BabyDetails();
/*Session sess=null;
try {
sess=new DentalRecordHome().sessionFactory.openSession();
Query query=sess.createQuery("from DentalRecord");
List list=query.list();
for(Iterator it=list.listIterator();it.hasNext();)
{
DentalRecord dr=(DentalRecord)it.next();
System.out.println("The record Id"+dr.getBabyId());
System.out.println("The teeth is="+dr.getTeeth());
}
sess.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println("The error is="+e);
}*/
try {
DentalRecordHome drh=new DentalRecordHome();
DentalRecord dr=drh.findById("mubaby");
System.out.println("The id is="+dr.getDeseaseDiagnosed());
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
}
}
com.mybaby.entityCode:
package com.mybaby.entity;
// Generated Jun 29, 2009 12:42:36 PM by Hibernate Tools 3.2.4.GA
import java.util.Date;
/**
* DentalRecord generated by hbm2java
*/
public class DentalRecord implements java.io.Serializable {
private String babyId;
private BabyDetails babyDetails;
private Date VDate;
private String teeth;
private String finding;
private String deseaseDiagnosed;
private Date lastUpdated;
public DentalRecord() {
}
public DentalRecord(BabyDetails babyDetails, Date lastUpdated) {
this.babyDetails = babyDetails;
this.lastUpdated = lastUpdated;
}
public DentalRecord(BabyDetails babyDetails, Date VDate, String teeth,
String finding, String deseaseDiagnosed, Date lastUpdated) {
this.babyDetails = babyDetails;
this.VDate = VDate;
this.teeth = teeth;
this.finding = finding;
this.deseaseDiagnosed = deseaseDiagnosed;
this.lastUpdated = lastUpdated;
}
public String getBabyId() {
return this.babyId;
}
public void setBabyId(String babyId) {
this.babyId = babyId;
}
public BabyDetails getBabyDetails() {
return this.babyDetails;
}
public void setBabyDetails(BabyDetails babyDetails) {
this.babyDetails = babyDetails;
}
public Date getVDate() {
return this.VDate;
}
public void setVDate(Date VDate) {
this.VDate = VDate;
}
public String getTeeth() {
return this.teeth;
}
public void setTeeth(String teeth) {
this.teeth = teeth;
}
public String getFinding() {
return this.finding;
}
public void setFinding(String finding) {
this.finding = finding;
}
public String getDeseaseDiagnosed() {
return this.deseaseDiagnosed;
}
public void setDeseaseDiagnosed(String deseaseDiagnosed) {
this.deseaseDiagnosed = deseaseDiagnosed;
}
public Date getLastUpdated() {
return this.lastUpdated;
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
}
The hibernate configuration file is in the package
srcCode:
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/babyhealthdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">babyhealthdb</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/mybaby/entity/BabyDetails.hbm.xml" />
<mapping resource="com/mybaby/entity/PrenatalMotherDetails.hbm.xml" />
<mapping resource="com/mybaby/entity/VaccineName.hbm.xml" />
<mapping resource="com/mybaby/entity/PhysicalDetails.hbm.xml" />
<mapping resource="com/mybaby/entity/DevelopmentalLandmarks.hbm.xml" />
<mapping resource="com/mybaby/entity/Address.hbm.xml" />
<mapping resource="com/mybaby/entity/State.hbm.xml" />
<mapping resource="com/mybaby/entity/OperativeProcedures.hbm.xml" />
<mapping resource="com/mybaby/entity/Authenticate.hbm.xml" />
<mapping resource="com/mybaby/entity/DietInfo.hbm.xml" />
<mapping resource="com/mybaby/entity/BabyGuardianInfo.hbm.xml" />
<mapping resource="com/mybaby/entity/Country.hbm.xml" />
<mapping resource="com/mybaby/entity/Immunization.hbm.xml" />
<mapping resource="com/mybaby/entity/BabySiblingsInfo.hbm.xml" />
<mapping resource="com/mybaby/entity/Gender.hbm.xml" />
<mapping resource="com/mybaby/entity/NewbornHistory.hbm.xml" />
<mapping resource="com/mybaby/entity/RaceEthinicity.hbm.xml" />
<mapping resource="com/mybaby/entity/SocialHistory.hbm.xml" />
<mapping resource="com/mybaby/entity/DoctorDetails.hbm.xml" />
<mapping resource="com/mybaby/entity/Medication.hbm.xml" />
<mapping resource="com/mybaby/entity/MedicalHistory.hbm.xml" />
<mapping resource="com/mybaby/entity/VaccineType.hbm.xml" />
<mapping resource="com/mybaby/entity/DentalRecord.hbm.xml" />
<mapping resource="com/mybaby/entity/Pediatricianinfo.hbm.xml" />
<mapping resource="com/mybaby/entity/Labtest.hbm.xml" />
<mapping resource="com/mybaby/entity/BloodType.hbm.xml" />
<mapping resource="com/mybaby/entity/Medicaltest.hbm.xml" />
<mapping resource="com/mybaby/entity/FamilyHistory.hbm.xml" />
<mapping resource="com/mybaby/entity/BabyMotherInfo.hbm.xml" />
<mapping resource="com/mybaby/entity/UploadReports.hbm.xml" />
<mapping resource="com/mybaby/entity/Specialty.hbm.xml" />
<mapping resource="com/mybaby/entity/Reminder.hbm.xml" />
<mapping resource="com/mybaby/entity/BabyFatherInfo.hbm.xml" />
<mapping resource="com/mybaby/entity/Allergies.hbm.xml" />
</session-factory>
</hibernate-configuration>
Any one can help me.....