-->
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.  [ 15 posts ] 
Author Message
 Post subject: Hibernate help
PostPosted: Thu Dec 21, 2006 2:13 pm 
Newbie

Joined: Thu Dec 21, 2006 2:03 pm
Posts: 7
I am trying to write some data to the database using hibernate. I get the following error:

Dec 21, 2006 12:09:42 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Dec 21, 2006 12:09:42 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Dec 21, 2006 12:09:42 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
An AnnotationConfiguration instance is required to use <mapping clazz="com.marketecho.mecap.Users"/>
Exception in thread "main" java.lang.NullPointerException
at com.marketecho.mecap.WritingRecord.main(WritingRecord.java:39)

My code is following:

package com.marketecho.mecap;

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



public class WritingRecord {
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();
org.hibernate.Transaction tx = session.beginTransaction();

//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Customer cust = new Customer();
cust.setCustomerId(3457);
cust.setCustomerFName("Deepak");
cust.setCustomerLName("Kumar");
cust.setEmail("deepak_38@yahoo.com");
session.save(cust);
tx.commit();
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();
}
}
}

----------
After SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
It catches exception prints out the following line
An AnnotationConfiguration instance is required to use <mapping clazz="com.marketecho.mecap.Users"/>
Then since session is null, it gets null pointer excepetion at
line session.flush();

Any suggestion what can I do?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 3:16 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
How did you map your Persistent class? With hbm.xml files or with annotations? If you're using annotations, I guess you didn't specify correctly the mapped class inside your hibernate.cfg.xml file. Can you post the content of this file here?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject: Annotation
PostPosted: Thu Dec 21, 2006 3:20 pm 
Beginner
Beginner

Joined: Thu Apr 27, 2006 12:19 pm
Posts: 33
Location: Seattle, WA
If you are using annotated classes then you should use the AnnotationConfiguration class instead. Something like:

Code:
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();


Top
 Profile  
 
 Post subject: Hibernate help
PostPosted: Thu Dec 21, 2006 3:41 pm 
Newbie

Joined: Thu Dec 21, 2006 2:03 pm
Posts: 7
Now I use the following code

package com.marketecho.mecap;

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



public class WritingRecord {
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 AnnotationConfiguration().configure().buildSessionFactory();
session = sessionFactory.openSession();
org.hibernate.Transaction tx = session.beginTransaction();

//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Customer cust = new Customer();
cust.setCustomerId(3457);
cust.setCustomerFName("Deepak");
cust.setCustomerLName("Kumar");
cust.setEmail("deepak_38@yahoo.com");
session.save(cust);
tx.commit();
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();
}
}
}

I get the following error:

Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.0.5
Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Environment <clinit>
INFO: loaded properties from resource hibernate.properties: {hibernate.connection.username=root, hibernate.connection.password=****, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.connection.url=jdbc:mysql://localhost/mecap, hibernate.connection.driver_class=org.gjt.mm.mysql.Driver}
Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Dec 21, 2006 1:34:34 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Exception in thread "main" java.lang.NullPointerException
at com.marketecho.mecap.WritingRecord.main(WritingRecord.java:39)

-----------
Still INFO: Configured SessionFactory: null


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 3:44 pm 
Newbie

Joined: Thu Dec 21, 2006 2:03 pm
Posts: 7
My hibernate.cfg.xml is below

<?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="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/mecap</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="com.marketecho.mecap.Users" />
.................
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject: loose configure()
PostPosted: Thu Dec 21, 2006 5:42 pm 
Beginner
Beginner

Joined: Thu Apr 27, 2006 12:19 pm
Posts: 33
Location: Seattle, WA
I guess you don't need to call configure() for AnnotationConfiguration. Here's an example from the docs:

Code:
public class HibernateUtil {

private static final SessionFactory sessionFactory;

    static {
        try {

            sessionFactory = new AnnotationConfiguration().buildSessionFactory();
        } catch (Throwable ex) {
            // Log exception!
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession()
            throws HibernateException {
        return sessionFactory.openSession();
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 5:46 pm 
Beginner
Beginner

Joined: Tue Aug 29, 2006 8:13 pm
Posts: 32
Location: Spain (GU)
Hi,

But I need the Users.hbm.xml or the POJO Users with Annotations. Can you provide that code (Users.java or Users.hmb.xml)?

Regards.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 6:21 pm 
Newbie

Joined: Thu Dec 21, 2006 2:03 pm
Posts: 7
After I added AnnotationConfigure(). It is not giving me any annotationinstance error. So it probably has nothing to do with
any particular file like Users.

I tried without .configure(), but it does not help. It does not configure at all. So when I tried with AnnotationConfigure().configure() it gives the following error
Dec 21, 2006 4:14:09 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Dec 21, 2006 4:14:09 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Dec 21, 2006 4:14:09 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null

So some reason it is having problem building sessionfactory.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 6:34 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Maybe you should try and add log4j in the classpath and activate the logs at a lower level (debug, say). You would certainly have more infos on what's going on...

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 7:07 pm 
Newbie

Joined: Thu Dec 21, 2006 2:03 pm
Posts: 7
I put the statement out of try block
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

Then I get the following error

Dec 21, 2006 5:01:31 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Dec 21, 2006 5:01:31 PM org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: com.marketecho.mecap.Users
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/SequencedHashMap
at org.hibernate.mapping.Table.<init>(Table.java:33)
at org.hibernate.cfg.Mappings.addTable(Mappings.java:165)
at org.hibernate.cfg.annotations.TableBinder.fillTable(TableBinder.java:132)
at org.hibernate.cfg.annotations.EntityBinder.bindTable(EntityBinder.java:368)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:537)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:452)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:268)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283)
at com.marketecho.mecap.WritingRecord.main(WritingRecord.java:12)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 22, 2006 3:06 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
You're just missing one jar (and maybe some others). Where did you get you hibernate archive?

Normally, there's a lib directory. You should put every jars inside in your classpath.

Here, it seems like you're missing the commons-collections jar : http://jakarta.apache.org/commons/collections/

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 22, 2006 10:53 am 
Newbie

Joined: Thu Dec 21, 2006 2:03 pm
Posts: 7
After I added common collections jar and mysql connector jar file I am getting different error. The following error is

Dec 22, 2006 8:47:28 AM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/cglib/proxy/CallbackFilter
at org.hibernate.bytecode.cglib.BytecodeProviderImpl.getProxyFactoryFactory(BytecodeProviderImpl.java:33)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactoryInternal(PojoEntityTuplizer.java:182)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:160)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:135)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:56)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:269)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:425)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1291)
at com.marketecho.mecap.WritingRecord.main(WritingRecord.java:12)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 22, 2006 11:27 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, now you seem to be missing cglib...

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 22, 2006 11:51 am 
Newbie

Joined: Thu Dec 21, 2006 2:03 pm
Posts: 7
Now I am getting the following error

Dec 22, 2006 9:30:55 AM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured


Do I need to include something like
<property name="connection.datasource"> in hibernate.cfg.xml file.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 22, 2006 12:34 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
It's not an error. It's just saying it won't bind to jndi. Is there any "other" error message else?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 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.