-->
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.  [ 6 posts ] 
Author Message
 Post subject: ClassCastException when store object
PostPosted: Sun Oct 12, 2003 4:37 pm 
Newbie

Joined: Mon Sep 15, 2003 12:55 pm
Posts: 9
Location: Bangkok, Thailand
Hi.

When I store object with session.save(people); It cause an exception

java.lang.ClassCastException
at net.sf.hibernate.type.CharacterType.set(CharacterType.java:32)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:46)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:31)
at net.sf.hibernate.persister.EntityPersister.dehydrate(EntityPersister.java:371)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:503)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:444)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:717)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)
at persistance.UseHibernate.main(UseHibernate.java:42)

I use hibernate provided connection on mysql and my code an xml are

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="persistance.People" table="people">
        <id name="id" type="integer" column="peopleid">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="character" not-null="true"/>
        <property name="birthDate" column="birthdate" type="date" not-null="true"/>
    </class>
</hibernate-mapping>


Code:
public class UseHibernate {
    public static void main(String[] args) throws HibernateException {
        Configuration cfg = null;
        try {
            cfg = new Configuration().addFile("D:/IdeaProjects" +
                    "/Projects/restaurant/Prototype/hibernate/People.hbm.xml");
        } catch (MappingException e) { }


        SessionFactory sessionFactory = null;
        sessionFactory = cfg.buildSessionFactory();

        Session session = null;
        session = sessionFactory.openSession();

        People people = new People();
        people.setName("Paan");
        people.setBirthDate(Calendar.getInstance().getTime());

        session.save(people);
        session.close();
    }
}

Code:
public class People {
    private int id;
    private String name;
    private Date birthDate;


    private int getId() {
        return id;
    }
   
    private void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
}


Please tell me what I done wrong.

Best Regard


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2003 4:39 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
'name' is a String typed property, not a character.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2003 5:23 pm 
Newbie

Joined: Mon Sep 15, 2003 12:55 pm
Posts: 9
Location: Bangkok, Thailand
Thanks for your help but still I can't store object to database.

runtime output is

[code]13


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2003 5:24 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You are not committing any transaction. Please read the Quickstart tutorial.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 13, 2003 1:37 am 
Newbie

Joined: Mon Sep 15, 2003 12:55 pm
Posts: 9
Location: Bangkok, Thailand
Thanks a lot but I have some question. Is Hibernate need me to begin and commit transaction every time I want to manipulate object. Ref doc said it was optional.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 13, 2003 2:25 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Using the Hibernate Transaction API is optional, at least you have to commit the JDBC connection without.

Yes, you need a transaction, even for read-only operations. No, it is not faster without.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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