-->
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.  [ 5 posts ] 
Author Message
 Post subject: Need help in first work with Hibernate
PostPosted: Thu May 01, 2008 5:59 pm 
Newbie

Joined: Thu May 01, 2008 4:53 pm
Posts: 11
Hi EveryOne,


This is my first posting in this forum.Thanks for helping learners like this.
I downloaded hibernate-3.2.6 and i working with eclipse3.3,oracle10g and JRE 1.6.
Is it necessary to download any plugins for hibernate to use in eclipse ?
I wrote a simple java application using hibernate.To write that code i took some help of a website.Here is my directory structure...


HibernatePro1
l
l__src
l l
l l_ events
l l l_ Event.java
l l l
l l l_ Event.hbm.xml
l l
l l_ client
l l l_ TestClient.java
l l l
l l l_ EventManager.java
l l
l l__Event.cfg.xml
l l
l l
l l__log4j.properties
l
l
l__+JRE System Library
l
l__+Hibernate User Library


I included all my hibernate .jar files in "Hibernate User Library"

Now when i try to run TestClient.java i am getting exception like below

Code:
17:12:05,062  INFO Environment:514 - Hibernate 3.2.6
17:12:05,093  INFO Environment:547 - hibernate.properties not found
17:12:05,156  INFO Environment:681 - Bytecode provider name : cglib
17:12:05,171  INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling
17:12:05,390  INFO Configuration:1432 - configuring from resource: /hibernate.cfg.xml
17:12:05,390  INFO Configuration:1409 - Configuration resource: /hibernate.cfg.xml
[color=red]Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found
   at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
   at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1411)
   at org.hibernate.cfg.Configuration.configure(Configuration.java:1433)
   at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
   at client.TestClient.openSession(TestClient.java:23)
   at client.TestClient.main(TestClient.java:48)
[/color]



Is there any wrong with my directory structure ?
Can any one please help me in solving this problem.
This is first posting so if there any mistakes in my posting Please forgive me.

Thank q very much,
sirisha.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 01, 2008 9:58 pm 
Newbie

Joined: Thu May 01, 2008 9:34 pm
Posts: 7
The directory that contains hibernate.cfg.xml file must be on the java classpath.


Top
 Profile  
 
 Post subject: To save all rows in table using hibernate
PostPosted: Sun May 04, 2008 12:19 am 
Newbie

Joined: Thu May 01, 2008 4:53 pm
Posts: 11
Thanks,
I solved this problem by adding hibernate.properties file to my src folder and by changing names of Event.cfg.xml and Event.hbm.xml to hibernate.cfg.xml and hibernate.hbm.xml.Now it's working fine now.

But now i have another problem with logic of my code.I am placing my client programs here please check these once.
Code:
package client;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import events.*;
public class TestClient
{
public Event buildEvent()
{
Event event = new Event();
event.setEventTitle("Environmental Meet");
event.setTotalMembers(100);
return event;
}
public Session openSession()
{
  SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  Session session =sessionFactory.openSession();
  return session;
}
public Event testSaveEvent(EventManager manager)
{
  Event event = buildEvent();
  manager.saveEvent(event);
  System.out.println("Event saved with ID = "+event.getEventId());
  return event;
}
public static void main(String[] args)
{
  TestClient client = new TestClient();
  Session session = client.openSession();
  EventManager manager = new EventManager(session);
  Event event = client.testSaveEvent(manager);
   System.out.println("End of Program");
  session.flush();
}
}

and

Code:
package client;

import org.hibernate.Session;
import events.Event;
public class EventManager
{
   private Session session = null;

   public EventManager(Session session)
   {
   if(session == null)
   throw new RuntimeException("Invalid session object. Cannot instantiate the EventManager.");
   this.session = session;
   }

   public void saveEvent(Event event)
   {
   session.save(event);
   }
}

Now my problem is...
1. when i try to save a row by using these classes,all those previous values(entered from Oracle) are getting deleted.Now my table is having only one row which is entered through this classes.
2. After this i changed entry values for that table fields in my class and now i run this client program once again.Now my previously entered row in step1 was deleted and now my table contains new row only.
At any time i am getting only one row in my table.But i want to save all those rows in my table.

What is wrong with my code.Can any one please help me..

Thank q very much,
sirisha.


Top
 Profile  
 
 Post subject: question
PostPosted: Sun May 04, 2008 3:11 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Could you post your hibernate config files? My theory is that you may be using the hbm2ddl property set to 'drop-create'... so every time you execute your app, the entire db gets re-initialized... You should set this property to 'create' only.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 10:40 am 
Newbie

Joined: Thu May 01, 2008 4:53 pm
Posts: 11
Hi Sagimann,
Thank q very much.You are correct,problem in my code was hbm2ddl.auto property. I removed following tag in hibernate.cfg.xml

<property name="hbm2ddl.auto">create</property>

Now it's working fine.

Thanks,
siri.


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