For help with installation, bugs reports or feature requests, please head over to our new forums.
Genuitec Community on GitHub
- This topic has 8 replies, 4 voices, and was last updated 21 years, 3 months ago by
snpe.
-
AuthorPosts
-
excentMemberHi
I’m trying to add Hibernate capabilities to my web project ( ie . to my servlet) .
Steps that I followed.
1) Created a web module project2) Added a new servlet called hiber to this project, with webroot as the root folder, now automatically MEATA-INF and WEB-INF(with lib and web.xml) folders are created.
3) Now I have added StandardWEB Xdoclet to my project, when ran that Xdoclet it too created a WEB-INF folder with in ‘ src’ folder with another web.xml and jboss-web.xml ( But thse two were empty).
4) Now I I have added Hibernate capabilities to my project. Then it created a hibernate.cfg.xml in my project folder.
5) Then I have added a mapping class Event.class in a new package (com.san.hibernate) and a mapping resource – Event.hbm.xml
6) Now I have modified my Servlet so that it can update my hypersonic database table ( I have got the steps for the servlet from a tutorial).
7) Then to the hibernate.cfg.xml I have added the following line manually
<mapping resource=”com/hiber/san/Event.hbm.xml”/>
And then by using MyEclipse packaging tool I ve deployed it to my jboss(3.2.4).
For the first time my application executed success fully, but ofcourse with out knowing how to create an application with Hibernate and servlet using MyEclipse 🙁 . But after some time my application has failed and now it is giving some errors as follwos.
———————————————————————————
1:06:14,506 ERROR [Engine] StandardWrapperValve[hiber]: Servlet.service() for servlet hiber threw exception
java.lang.NullPointerException
at hiber.doGet(hiber.java:62) AND
———————————————————————————11:12:24,338 INFO [TomcatDeployer] deploy, ctxPath=/Hiber, warUrl=file:/D:/jboss-3.2.4/server/default/deploy/Hiber.war/
11:12:45,669 INFO [STDOUT] Initializing Hibernate
11:12:45,669 INFO [Configuration] configuring from resource: /hibernate.cfg.xml
11:12:45,669 INFO [Configuration] Configuration resource: /hibernate.cfg.xml
11:12:45,689 WARN [Configuration] /hibernate.cfg.xml not found
11:12:45,749 INFO [STDOUT] net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found
at net.sf.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:849)———————————————————————————
I don know how to solve it, and what are the exact steps that must be followed in order create an application with servlet, MyEclipse and Hibernate. I have googled a lot , but I couldn’t find any material for my purpose. Can you please give me steps for that, PLEASE HELP ME.
EXCENT|-Hiber
|—src
|___ (default package)
| |_ hibar.java (My servlet )
|___ com.san.hiber (Package)
| |__ HibernateSessionFactory.java
| |__ Event.java (file to map table)
| |__ Event.hbm.xml (Mapping resourse)
| |_ WEB-INF
| |_ web.xml
| |_ jboss-web.xml
|
|— JRE system library
|— J2EE 1.3 container library
|— }
|— } different other libraries (Jboss,ant,Hibernate etc)
|— }
|_ Webroot
| |_ META-INF
| | |_ MANIFEST.MF
| |_ WEB-INF
| | |_ lib
| | |_ web.xml
| |__ index.html
|__ hibernate.cfg.xml
|__ xdocklet.xmlAugust 9, 2004 at 5:29 am #211681
excentMemberHi ,
I have found some help for my problem from one of your previous discussion. As directed in the forum I have made one the change to my application, ie I have moved the “hibernate.cfg.xml” file to the “src” folder. Now the “hibernate.cf.xml” file is visible to the application, but still I’m getting some errors as follows, with the error report I’m attaching my servlet for your kind persual, PLEASE HELP ME, ITS VERY URGENT!!
————————————————————————————
15:40:59,134 INFO [STDOUT] Initializing Hibernate
15:40:59,254 INFO [Environment] Hibernate 2.1.3
15:40:59,284 INFO [Environment] hibernate.properties not found
15:40:59,304 INFO [Environment] using CGLIB reflection optimizer
15:40:59,344 INFO [Configuration] configuring from resource: /hibernate.cfg.xml
15:40:59,344 INFO [Configuration] Configuration resource: /hibernate.cfg.xml
15:40:59,475 INFO [Configuration] Mapping resource: com/hiber/san/Event.hbm.xml
15:41:00,105 INFO [Binder] Mapping class: com.hiber.san.Event -> EVENTS
15:41:00,967 INFO [Configuration] Configured SessionFactory: null
15:41:00,977 INFO [Configuration] processing one-to-many association mappings
15:41:00,977 INFO [Configuration] processing one-to-one association property references
15:41:00,977 INFO [Configuration] processing foreign key constraints
15:41:01,177 INFO [Dialect] Using dialect: net.sf.hibernate.dialect.HSQLDialect
15:41:01,187 INFO [SettingsFactory] Use outer join fetching: false
15:41:01,237 INFO [DriverManagerConnectionProvider] Using Hibernate built-in connection pool (not for production use!)
15:41:01,237 INFO [DriverManagerConnectionProvider] Hibernate connection pool size: 20
15:41:01,237 INFO [DriverManagerConnectionProvider] using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:http://localhost:1701
15:41:01,237 INFO [DriverManagerConnectionProvider] connection properties: {user=sa, password=}
15:41:01,357 INFO [TransactionManagerLookupFactory] No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
————————————————————————————-
My servelet (hiber.java)
================
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.cfg.Configuration;
import com.hiber.san.*;
import java.util.Date;/**
* @author Excent
* TODO To change the template for this generated type comment go to
* Window – Preferences – Java – Code Style – Code Templates
*/public class hiber extends HttpServlet {
SessionFactory sessionFactory;
/**
* Constructor of the object.
*/
public hiber() {
super();
}/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts “destroy” string in log}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {response.setContentType(“text/html”);
PrintWriter pw=response.getWriter();
pw.println(“<html>”);
pw.println(“<body>”);
pw.println(“<h1>HELLO</h1>”);
// ——————————————————-
/*try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();Event theEvent = new Event();
theEvent.setId(new Long(111));theEvent.setTitle(“Hay”);
theEvent.setDate(new Date(“02/02/2002”));session.save(theEvent);
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}*/
// ——————————————————-pw.println(“</body></html>”);
}/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out
.println(“<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.01 Transitional//EN\”>”);
out.println(“<HTML>”);
out.println(” <HEAD><TITLE>A Servlet</TITLE></HEAD>”);
out.println(” <BODY>”);
out.print(” This is “);
out.print(this.getClass());
out.println(“, using the POST method”);
out.println(” </BODY>”);
out.println(“</HTML>”);
out.flush();
out.close();
}/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
try {
System.out.println(“Initializing Hibernate”);
sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println(“Finished Initializing Hibernate”);
} catch (HibernateException e) {
System.out.println(“hello”);
//e.printStackTrace();
}
}}
————————————————————————————Thanks,
EXCENTNB: Please don’t feel bad, I know I’m using a lot of space. There is no other option for me. Please help me at your earliest.
August 9, 2004 at 8:21 am #211688
Thomas TrostelParticipantI had a similar problem. The first step is always going to be moving the “hibernate.cfg.xml” file to the src folder. After that was done and the hibernate options were pointed to that file everything worked fine for me.
Here is a working snippet that you might be able to template. Maybe this will assist you. I did notice you are using openSession instead of currentSession. This is the only major difference between our two code methods.
======
public UsersLogin getInstance() {
try {
Session hsession = HibernateSessionFactory.currentSession();
tx= hsession.beginTransaction();Query query = hsession.createQuery(“select out from UsersLogin as out where out.userId = :thisId”);
query.setParameter(“thisId”, userId);Iterator it = query.iterate();
if (it.hasNext())
return ((UsersLogin) it.next());
else
return null;} catch (HibernateException e) {
logger.error(e.getMessage());
e.printStackTrace();
return null;
}}
====
You may also want to take a close look at your HibernateSessionFactory class and make sure its pointing to the right directory…. Don’t worry about using a lot of space. More details are always good 🙂
August 9, 2004 at 9:08 am #211694
Riyad KallaMemberttrostel, thanks for helping out here!
Excent, did ttrostel’s suggestion work for you?
August 10, 2004 at 1:55 am #211757
excentMemberThanks for your help 🙂 ,
But I’m still facing some problem 🙁 (second one ),ie
——————————————————————————
12:18:43,566 INFO [TransactionFactoryFactory] Transaction strategy: net.sf.hibernate.transaction.JDBCTransactionFactory
12:18:43,586 INFO [TransactionManagerLookupFactory] No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
———————————————————————————–please help,
Thanks in advance, excentAugust 10, 2004 at 6:07 am #211761
Thomas TrostelParticipantSo you’re using a transactional data source. I think you want to be using the net.sf.hibernate.transaction.JTATransactionFactory class in this case instead of JDBCTransactionFactory. The property is something like hibernate.transaction.factory_class but I need to look it up when I get a chance. Maybe that will get you started.
Good luck!
Tom
August 10, 2004 at 2:56 pm #211808
snpeMemberYou can use JTA transaction only in container with transction support (btw JBoss, but not tomcat) – Only delete property with JTA in hiberntae.cfg.xml and hibernate wiil choose JDBC transaction for you
regards
August 11, 2004 at 12:29 am #211823
excentMemberHi,
I have no such entry in hibernate.cfg.xml. I’m attaching the file for your reference. I’m still confuced. 😥 Please HELPThanks.
——————————————————————————
<hibernate-configuration><session-factory>
<!– properties –>
<property name=”connection.username”>sa</property>
<property name=”connection.url”>jdbc:hsqldb:http://localhost:1701</property>
<property name=”dialect”>net.sf.hibernate.dialect.HSQLDialect</property>
<property name=”connection.password”></property>
<property name=”connection.driver_class”>org.hsqldb.jdbcDriver</property>
<property name=”show_sql”>true</property>
<property name=”transaction.factory_class”>net.sf.hibernate.transaction.JDBCTransactionFactory</property>
<property name=”hibernate.cache.provider_class”>net.sf.hibernate.cache.HashtableCacheProvider</property>
<property name=”hibernate.hbm2ddl.auto”>update</property>
<!– mapping files –>
<mapping resource=”com/hiber/san/Event.hbm.xml”/></session-factory>
</hibernate-configuration>
August 11, 2004 at 11:12 am #211850
snpeMemberIf You use Jboss You CAN use JTA transaction, but You have to set JBoss correct
see ; http://www.hibernate.org/66.html
regards
-
AuthorPosts
