-->
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: Insert null, Problem with GeneratedValue
PostPosted: Thu Apr 10, 2008 9:24 am 
Newbie

Joined: Thu Apr 10, 2008 9:16 am
Posts: 1
I am learning hibernate and made a test with hsqldb, now i wonder why the id of the entity Event is null, i think i made something wrong at the GeneratedValue, can you help me?

Hibernate version:
3.2.0

Mapping documents:
Code:
package de.event;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "events", catalog = "public")
public class Event {
    private Integer id;

    private String name;

    public Event() {
    }

    public Event(String name) {
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

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


Code between sessionFactory.openSession() and session.close():
Code:
Transaction transInsert = session.beginTransaction();

Event e = new Event("Hibernate lernen!");

session.save(e);
transInsert.commit();


Full stack trace of any exception that occurs:

Code:
     [java] 15:15:19,478 ERROR JDBCExceptionReporter:78 - Attempt to insert null into a non-nullable column: column: ID table: EVENTS in statement [insert into public.events (id, name) values (null, ?)]
     [java] Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [de.event.Event]
     [java]    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
     [java]    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
     [java]    at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:40)
     [java]    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2163)
     [java]    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2643)
     [java]    at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:51)
     [java]    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
     [java]    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
     [java]    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
     [java]    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
     [java]    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
     [java]    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
     [java]    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
     [java]    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
     [java]    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
     [java]    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
     [java]    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
     [java]    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
     [java]    at de.business.EventManager.createAndStoreEvent(EventManager.java:19)
     [java]    at de.starter.Starter.main(Starter.java:9)
     [java] Caused by: java.sql.SQLException: Attempt to insert null into a non-nullable column: column: ID table: EVENTS in statement [insert into public.events (id, name) values (null, ?)]
     [java]    at org.hsqldb.jdbc.Util.throwError(Unknown Source)
     [java]    at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
     [java]    at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:33)
     [java]    ... 17 more
     [java] Java Result: 1


Hibernate Configuration File
Code:
<?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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.url">jdbc:hsqldb:file:../db/events</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>
       
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
       
        <property name="hibernate.hbm2ddl.auto">validate</property>

    </session-factory>

</hibernate-configuration>


Name and version of the database you are using:
hsqldb 1.8.0

The generated SQL (show_sql=true):
insert into public.events (id, name) values (null, ?)

The database table
CREATE MEMORY TABLE EVENTS(ID INTEGER NOT NULL PRIMARY KEY,NAME VARCHAR(255))


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 10:21 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I think you are having the same problem as
http://forum.hibernate.org/viewtopic.php?t=985693

You defined GenerationType.IDENTITY but your "create table" doesn't define an autoincremental PK.


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.