-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Please Help...no Exception No result
PostPosted: Tue Mar 17, 2009 9:50 am 
Newbie

Joined: Tue Mar 17, 2009 9:14 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.2

Mapping documents: I am trying to enter name, age,sex and phone numbers of a user with one to many relationship but when I click on submit there is no result coming in database in both of the tables and no exceptions also there.

UserBean.java


package com;

import java.util.*;


public class Userbean {
private String username;
private String email;
private int age;
private long userId;
private Set phonenumbers = new HashSet();

public Set getPhoneNumbers() {
return phonenumbers;
}
public void setPhoneNumbers(Set phoneNumbers) {
this.phonenumbers = phoneNumbers;
}
public Userbean() {
System.out.println("Inside UserBean");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAge() {
return "" + age;
}
public void setAge(String age) {
this.age = Integer.parseInt("" + age);
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}

}
Mapping xml for this:----Usertest.hbm.xml
<?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="com.Userbean" table="newusers" >
<id name="userId" column="user_id" >
<generator class="increment" />
</id>
<property name="username" column="user_name" length="20"/>
<property name="email" column="user_email" length="40" />
<property name="age" column="user_age" />
<set name="phonenumbers" cascade="all">
<key column="USER_ID"/>
<one-to-many class="com.visualbuilder.hibernate.Relationuserbean" />
</set>
</class>
</hibernate-mapping>

Now phonenumbers class for which I am making one to may relationship

Relationuserbean.java
package com;
import java.io.Serializable;

public class Relationuserbean implements Serializable
{
private long userId;
private int phonenumbers;
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getPhonenumbers() {
return "" + phonenumbers ;
}
public void setPhonenumbers(String phonenumbers) {
this.phonenumbers = Integer.parseInt("" + phonenumbers);
}
}

mapping xml file for this bean class:

Relationusertest.hbm.xml
<?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="com.Relationuserbean" table="Relationusertest">
<id name="userId" column="user_id">
<generator class="increment" />
</id>
<property name="phonenumbers" length="20"/>
</class>
</hibernate-mapping>

jsp file name from which I am sending request

user.jsp
<HTML>
<BODY>
<FORM METHOD=POST ACTION="Servletmain">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4><BR>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

Servlet Code which is acting as a controller

Servletmain.java
package com;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.io.IOException;
import java.util.*;

public class Servletmain extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{

Userbean object = new Userbean();
object.setUsername(request.getParameter("username"));
object.setEmail(request.getParameter("email"));
object.setAge(request.getParameter("age"));
System.out.println(request.getParameter("username"));
System.out.println(request.getParameter("email"));
Set tmpList = new HashSet();
tmpList.add("1111");
tmpList.add("2222");
tmpList.add("3333");
object.setPhoneNumbers(tmpList);

/* request.setAttribute("username1", object.getUsername());
request.setAttribute("email1", object.getEmail());
request.setAttribute("age1", object.getAge());*/
// Get the request dispatcher -- request will be
// dispatched to this URL.
try{
System.out.println("Inside doPost");
TestClient t = new TestClient();

if(t.testSaveUser(object).equals("SUCCESS"))
{RequestDispatcher rd =
request.getRequestDispatcher("/UserList.jsp");

// Forward to requested URL
rd.forward(request, response);
}
}catch (Exception e) {
// TODO: handle exception
}
}
}
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">
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:mysql://localhost/hibernate
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
ok
</property>
<!-- Set AutoCommit to true -->
<property name="connection.autocommit">
true
</property>
<!-- SQL Dialect to use. Dialects are database specific -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<!-- Mapping files -->
<mapping resource="com/Usertest.hbm.xml" />
<mapping resource="com/Relationusertest.hbm.xml" />
</session-factory>
</hibernate-configuration>



Code between sessionFactory.openSession() and session.close():
package com;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import com.*;
import java.util.*;
public class TestClient {


public String testSaveUser(Userbean b)
{
System.out.println("Inside testsaveUser");
System.out.println(b.getAge());
System.out.println(b.getEmail());
System.out.println(b.getPhoneNumbers());

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Session sess =sessionFactory.openSession();
try{
sess.beginTransaction();


}catch (HibernateException h)
{
h.printStackTrace();
}
try {
sess.save(b);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sess.getTransaction().commit();
System.out.println("User saved with ID = "+ b.getUserId());
System.out.println("User saved with ID= "+ b.getAge());
return "SUCCESS";
}

}


Full stack trace of any exception that occurs:
Mar 17, 2009 7:20:13 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.5.0_06\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jre1.5.0_06\bin\client;C:\Program Files\Java\jre1.5.0_06\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;D:\jdk1.5.0_06\bin;c:\ant\bin
Mar 17, 2009 7:20:14 PM org.apache.coyote.http11.Http11BaseProtocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Mar 17, 2009 7:20:14 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1296 ms
Mar 17, 2009 7:20:14 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Mar 17, 2009 7:20:14 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/5.5.26
Mar 17, 2009 7:20:14 PM org.apache.catalina.core.StandardHost start
INFO: XML validation disabled
Mar 17, 2009 7:20:15 PM org.apache.coyote.http11.Http11BaseProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Mar 17, 2009 7:20:15 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Mar 17, 2009 7:20:15 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/47 config=null
Mar 17, 2009 7:20:15 PM org.apache.catalina.storeconfig.StoreLoader load
INFO: Find registry server-registry.xml at classpath resource
Mar 17, 2009 7:20:15 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1813 ms
Inside UserBean
ssss
aaaa
Inside doPost
Inside testsaveUser
23
aaaa
[3333, 2222, 1111]
Mar 17, 2009 7:20:24 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.6
Mar 17, 2009 7:20:24 PM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Mar 17, 2009 7:20:24 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Mar 17, 2009 7:20:24 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Mar 17, 2009 7:20:24 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Mar 17, 2009 7:20:25 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Mar 17, 2009 7:20:25 PM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : com/Usertest.hbm.xml
Mar 17, 2009 7:20:25 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: com.Userbean -> newusers
Mar 17, 2009 7:20:25 PM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : com/Relationusertest.hbm.xml
Mar 17, 2009 7:20:25 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: com.Relationuserbean -> Relationusertest
Mar 17, 2009 7:20:25 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null


Name and version of the database you are using:Mysql 5.0.22

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


I am not getting any of the data in any of the table. I am sending name, age,email by jsp and phone numbers by hardcode. I want that when I click on submit it will automatically insert data in both of the tables, but I am unable to do it.

Please help me out in this situation.

Thanks in advance.

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 3:18 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
try this instead

Code:
public String testSaveUser(Userbean b) {
   System.out.println("Inside testsaveUser");
   System.out.println(b.getAge());
   System.out.println(b.getEmail());
   System.out.println(b.getPhoneNumbers());

   SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

   Session sess =sessionFactory.openSession();
   try{
      Transaction tx = session.beginTransaction();
      sess.save(b);
      tx.commit();

   }catch (HibernateException e)
      tx.rollback();
      e.printStackTrace();

   }finally{
      session.close();      
   }

   System.out.println("User saved with ID = "+ b.getUserId());
   System.out.println("User saved with ID= "+ b.getAge());
   return "SUCCESS";
}

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 4:36 am 
Newbie

Joined: Tue Mar 17, 2009 9:14 am
Posts: 2
Sorry it did not work....Please tell me something else.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 19, 2009 9:14 am 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
You have to give me more information then that.

I forgot one more thing. Normally you use an EntityManager but you are trying to create the configuration yourself it looks like.

Take a look here.

http://www.hibernate.org/hib_docs/refer ... ation.html

Even though you are creating a new instance of the configuration class you have not actually configured anything.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.