-->
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: getting error while running simple hibernate example
PostPosted: Thu May 11, 2006 3:50 am 
Newbie

Joined: Wed May 10, 2006 8:12 am
Posts: 9
Hi

I have already posted the problem here, here i am just posting again with the clear problem.

i have simple example like :

[b]FirstExample.java [/b] as :

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class FirstExample {
public static void main(String[] args) {
Session session = null;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
System.out.println("Inserting Record");
Emp emp = new Emp();
emp.setEmpNo(7999);
emp.setENAME("VIswanath");
emp.setJOB("CLERK");
emp.setMGR(7902);
emp.setHIREDATE(null);
emp.setSAL(22000.00);
emp.setCOMM(0.00);
emp.setDEPTNO(20);
session.save(emp);
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}
finally{
session.flush();
session.close(); }
}
}

and
[b]Emp.java [/b] as :

package hibernate.net.roseindia.tutorial.hibernate;

import java.util.Date;

public class Emp{
private int empno;
private String ename;
private String job;
private Date hiredate;
private int mgr;
private double sal;
private double comm;
private int dept;

public int getEmpNo(){return empno;}
public String getENAME(){return ename;}
public String getJOB(){return job;}
public int getMGR(){return mgr;}
public Date getHIREDATE(){return hiredate;}
public double getSAL(){return sal;}
public double getCOMM(){return comm;}
public int getDEPTNO(){return dept;}
public void setEmpNo(int empno){this.empno=empno;}
public void setENAME(String ename){this.ename=ename;}
public void setJOB(String job){this.job=job;}
public void setMGR(int mgr){this.mgr=mgr;}
public void setHIREDATE(Date hiredate){this.hiredate=hiredate;}
public void setSAL(double sal){this.sal=sal;}
public void setCOMM(double comm){this.comm=comm;}
public void setDEPTNO(int dept){this.dept = dept;}
}

and Mapping file

[b]emp.hbm.xml[/b] as

<?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="Emp" table="EMP">
<id name="EMPNO" type="int" column="EMPNO" >
<generator class="assigned"/>
</id>
<property name="ename">
<column name="ENAME" />
</property>
<property name="job">
<column name="JOB"/>
</property>
<property name="mgr">
<column name="MGR"/>
</property>
<property name="hiredate">
<column name="HIREDATE"/>
</property>
<property name="sal">
<column name="SAL"/>
</property>
<property name="comm">
<column name="COMM"/>
</property>
<property name="dept">
<column name="DEPTNO"/>
</property>
</class>
</hibernate-mapping>

and config file
[b]hibernate.cfg.xml[/b] as:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.68.44:1521:EX</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>

and my directory structure is as following

JARs
D:\ex\lib\antlr-2.7.4.jar
D:\ex\lib\cglib-full-2.0.2.jar
D:\ex\lib\commons-collections-2.1.1.jar
D:\ex\lib\commons-logging-1.0.4.jar
D:\ex\lib\dom4j-1.5.2.jar
D:\ex\lib\hibernate3.jar
D:\ex\lib\jta.jar
D:\ex\lib\log4j-1.2.9.jar

CLASS file and mapping and config files
D:\ex\src\Emp.class
D:\ex\src\FirstExample.class
D:\ex\src\hibernate.cfg.xml
D:\ex\src\emp.hbm.xml

after all, while running my FirstExample.java i am getting the following exception

[b]---------- java ----------
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
java.lang.NullPointerException
at FirstExample.main(FirstExample.java:32)
Exception in thread "main"
Output completed (0 sec consumed) - Normal Termination[/b]

Please help me to resolve this, i am new to hibernate i am just trying to learn with examples.

Thanks in advance
-KRVR


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 6:13 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Hi,

you should place a log4j.properties in your classpath,
so that hibernate messages can be logged.

Can you tell us the which is the line,
where the NullPointerException is thrown ?

Quote:
java.lang.NullPointerException
at FirstExample.main(FirstExample.java:32)

_________________
dont forget to rate !


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 6:21 am 
Newbie

Joined: Wed May 10, 2006 8:12 am
Posts: 9
[quote="steckemetz"]Hi,

you should place a log4j.properties in your classpath,
so that hibernate messages can be logged.

Can you tell us the which is the line,
where the NullPointerException is thrown ?

[quote]java.lang.NullPointerException
at FirstExample.main(FirstExample.java:32) [/quote][/quote]


ThanX,

I put log4j.properties in classpath, but getting the same error.
i am getting nullpointer exception in the following part

finally{
session.flush(); // GETTING ERROR HERE
session.close();
}.


And can you please tell me where i can find the simple working examples for hibernate using Oracle database and eclipse IDE.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 6:25 am 
Newbie

Joined: Wed Nov 23, 2005 5:01 am
Posts: 15
Hi,

First of all you have to put an e.printStackTrace() into you catch statement to see the whole stack trace. It is difficult to see the problem if you didn't show us the stack trace

Second from what is in you message I can see a few problems:
1. You didn't use a transaction in your code (open one transaction and commit it in the end)
2. And you don't respect the Java Beans specification relating to the property getter and setter. This will raise some error in your application later when hibernate will try to determine the getter and setter for your properties.

Please resolve this issues first and then post your stack trace in case of error, and tell us if you are running the code from command line or from some IDE

I hope this help.

Calin


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.