- This topic has 7 replies, 2 voices, and was last updated 15 years, 5 months ago by support-shalini.
-
AuthorPosts
-
JeffMemberUsed the Hibernate tutorial to create a simple app using my tables. No problem.
Used the Hibernate tutorial demo to test reading from the new DAO that was created:
————————-
public static void main(String[] args) {
HotelsDAO dao = new HotelsDAO();
System.out.println(“Hotels Instantiated….. “);
try
{
Hotels xx = dao.findById(“wastg”);
System.out.println(“Entity retrieval successful, message is: ”
+ xx.getME_state());
}
catch (HibernateException e)
{
System.err.println(“Entity retrieval failed.”);
e.printStackTrace();
} finally { ………………..
————————–
Error Returned:Exception in thread “main” java.lang.NullPointerException
at com.genuitec.hibernate.HotelsDAO.findById(HotelsDAO.java:69)
at com.genuitec.hibernate.HibernateReadTest.main(HibernateReadTest.java:25)
————————
Here is HotelsDAO.java:69
public Hotels findById(java.lang.String id) {
log.debug(“getting Hotels instance with id: ” + id);
try {
Hotels instance = (Hotels) getSession().get(“com.genuitec.hibernate.Hotels”, id); return instance;
}
——————————–
Any ideas as to the problem?
support-shaliniMembervausa2020,
This could be a problem with hibernate.cfg.xml file.
Can you verify if the session object is not null after “HotelsDAO dao = new HotelsDAO(); ” in your main class?
By using org.hibernate.Session session = dao.getSession(); you can verify if the session object is null or not.
If it is null, then it could be a problem with hibernate config file.
Let me know if that helps.
JeffMemberShalini,
YES. The Session value is null. I am able to successfully query my table using the SQL tool AND the HQL in the tutorial works find. I had no problems until I tried to get execute the read.Can you see any flaws in the following???
Here is my file: 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”><!– Generated by MyEclipse Hibernate Tools. –>
<hibernate-configuration><session-factory>
<property name=”dialect”>
org.hibernate.dialect.MySQLDialect
</property>
<property name=”connection.url”>
jdbc:mysql://cydev1:3306/GSS
</property>
<property name=”connection.username”>tck145</property>
<property name=”connection.password”>brainDad</property>
<property name=”connection.driver_class”>
com.mysql.jdbc.Driver
</property>
<property name=”myeclipse.connection.profile”>cydev1</property>
<property name=”max_fetch_depth”>1</property>
<mapping resource=”com/genuitec/hibernate/Hotels.hbm.xml” />
</session-factory>
</hibernate-configuration>
support-shaliniMembervausa2020,
its hard to tell what is wrong. I do not have much visibility to your project.
Can you send us all the console errors that you are getting?
Also, can you verify if the mapping resource value is set to the correct path?
JeffMemberThe mapping resource is valid.
Here is all the console output:
Exception in thread “main” java.lang.NullPointerException
at com.genuitec.hibernate.HotelsDAO.findByProperty(HotelsDAO.java:99)
at com.genuitec.hibernate.HotelsDAO.findByME_name(HotelsDAO.java:109)
at com.genuitec.hibernate.HibernateReadTest.main(HibernateReadTest.java:29)
support-shaliniMembervausa2020,
Can you share your project? You can send a mail to support@genuitec.com and attach your project. Please add ATTN: Shalini in the subject and refer to this thread. This will help us reproduce this issue internally and quickly work on resolution.
Also, send us the script of your tables. Thank you for your help.
JeffMemberShalini,
Your suggestion resolved my problem. thank you very much.You have returned null in “BaseHibernateDAO” instead of “HibernateSessionFactory.getSession();”
Change your “BaseHibernateDAO” class and let me know if that helps.I Have pasted the class here for you
public class BaseHibernateDAO implements IBaseHibernateDAO {public Session getSession() {
return HibernateSessionFactory.getSession();
}}
support-shaliniMembervausa2020,
You are welcome.
Let me know if you have any issues. -
AuthorPosts