-->
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.  [ 2 posts ] 
Author Message
 Post subject: session.load not allowed for long parameter?
PostPosted: Thu Aug 30, 2007 10:31 am 
Newbie

Joined: Thu Aug 30, 2007 10:03 am
Posts: 8
Hi,

i'm a beginner with hibernate, trying to make my first example.

So I have written the following code:

Code:
package hibernatesample;

import java.io.Serializable;
import java.util.Date;

import junit.framework.TestCase;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.tool.hbm2ddl.SchemaExport;


/**
* Einfacher Test, der CRUD mit Hibernate demonstriert.
*/
public class CRUDTest extends TestCase implements Serializable{
   
   private SessionFactory _sessionFactory;
   private static final String ORT = "Hamburg";
   private static final String BESCHREIBUNG = "termin";
   private static final String TITEL = "titel";
   private static final Date ZEIT_PUNKT = new Date(System.currentTimeMillis() + 2 * 24 * 60 * 60 * 1000);
   private long _id;


    /**
     * Wir starten eine SessionFactory und erzeugen schon mal einen Termin.
     */
    protected void setUp() throws Exception {
        super.setUp();
        Configuration configuration =
                new Configuration().configure();
        configuration.setProperty("hibernate.connection.url", "jdbc:h2:file://home/tony/H2/Termin;DB_CLOSE_DELAY=-1");
        SchemaExport export =
                new SchemaExport(configuration);
        export.create(false, true);

        _sessionFactory = configuration.buildSessionFactory();
        _id = erzeugeTermin(TITEL, BESCHREIBUNG, ORT, ZEIT_PUNKT);
       
        System.out.println("Id: ");
        System.out.println(_id);
        testLoad();
       
    }

    private long erzeugeTermin(
            String titel, String beschreibung,
            String ort, Date zeitPunkt) {
        Termin termin = new Termin();
        termin.setTitel(titel);
        termin.setBeschreibung(beschreibung);
        termin.setOrt(ort);
        termin.setZeitPunkt(zeitPunkt);
        Session session = null;
        Transaction transaction = null;
        try {
            session = _sessionFactory.openSession();
            transaction =
                    session.beginTransaction();
            session.save(termin);
            transaction.commit();
        }
        catch (HibernateException e) {
            if (transaction != null) {
                transaction.rollback();
                throw e;
            }
        }
        finally {
            if (session != null) {
                session.close();
            }

        }
        return termin.getId();
    }

    public void testLoad(){
       
       Session session2 = null;
       
       try{
       
          session2 = _sessionFactory.openSession();
          Termin termin2 = (Termin) session2.load(Termin.class,  _id);
         
          assertEquals(ZEIT_PUNKT, termin2.getZeitPunkt());
          assertEquals(TITEL, termin2.getZeitPunkt());
          assertEquals(BESCHREIBUNG, termin2.getZeitPunkt());
          assertEquals(ORT, termin2.getZeitPunkt());
          
          System.out.print("Test: " + termin2.toString());
          
       }
       finally{
          if (session2 != null && session2.isConnected()){
             session2.close();
          }
       }
       
    }

}



I got one failure and don't know why it pops up.
The failure sounds like: The method load(Class, Serializable) in the type Session is not applicable for the arguments (Class, long). This failure notice referenced to the session.load in the method testLoad.

Has anybody one idea, why this failure notice pops up?

Thanx


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 1:40 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Because long is not Serializable but Long is, use the wrapper class and you're fine. You'd probably be ok in JDK5 or 6 as well as it would autobox for you I would imagine.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


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