Redian新闻
>
hibernate question? session and hibernate.cfg.xml
avatar
hibernate question? session and hibernate.cfg.xml# Java - 爪哇娇娃
d*y
1
I learn from a tutorial. I try run the example. errors happen.
something about session and hibernate.cfg.xml.
Here are files.
package roseindia.tutorial.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* Hibernate example to inset data into Contact table
*/
public class FirstExample {
public static void main(String[] args) {
Session session = null;
try{
// This step will read hibernate.cfg.xml
//and prepare hibernate for use
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set
//values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("d*******[email protected]");
session.save(contact);
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();
}

}
}
hibernate.cfg.xml file

br />"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


com.mysql.jdbc.Driverproperty>
jdbc:mysql://localhost:3306/mydb

root
password
2
true
org.hibernate.dialect.MySQLDialect
update



avatar
d*y
2
error message:
Exception in thread "main" java.lang.NullPointerException
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:46)
session.flush();//this is the FirstExample.java:46
avatar
d*y
3
Any comments?
avatar
d*y
4
Any comments?
avatar
t*e
5
Attach Debugger to step through the sample code, and find out where the
session object is set to null.
avatar
d*y
6
thanks in advance. I will try.
avatar
d*y
7
thanks in advance. I will try.
avatar
d*y
8
the source attachment does not contain the source for the file LoggerFactory
.class.
avatar
g*g
9
I guess you are missing the slf4j dependency for hibernate.

LoggerFactory

【在 d******y 的大作中提到】
: the source attachment does not contain the source for the file LoggerFactory
: .class.

avatar
d*y
10
yes. How can I solve it? can you give some links.
avatar
g*g
11
when you download hibernate, it should have that dependency as one
of 3rd party libs.
It's always preferrable to start with a maven project from official
site, then this kind of issue can be avoided.

【在 d******y 的大作中提到】
: yes. How can I solve it? can you give some links.
avatar
d*y
12
Thanks. I download the apache-log4j-1.2.16 and add to classpath.
nothing happens.
avatar
g*g
13
I thought I mention slf4j?

【在 d******y 的大作中提到】
: Thanks. I download the apache-log4j-1.2.16 and add to classpath.
: nothing happens.

avatar
D*y
14
are you sure you put the hibernate.cfg.xml to the right place so that your
sample application can find it when you run it?
avatar
c*r
15
1) I think the reason you got a nullpointerexception is because the session
object is never created.
As what was mentioned in previous posts, please check:
In your stack trace, is there any exception caused by ClassNotFound? If so,
check the class online, download the jar and put it in your build path.
ps, slf4j and log4j is totally different.
cannot type chinese, sorry for that.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。