-->
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: How to generate auto increment id
PostPosted: Wed Jul 18, 2007 3:06 am 
Newbie

Joined: Wed Jul 18, 2007 2:49 am
Posts: 3
Location: delhi
i need to generate auto increment id
i use the following code--

@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
Integer id;

public Integer getId() {
return id;
}


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

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into employee (name) values (?)
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [net.roseindia.Employee]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy8.save(Unknown Source)
at net.roseindia.Example1.main(Example1.java:23)
Caused by: java.sql.SQLException: General error message from server: "Field 'ID' doesn't have a default value"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2251)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1772)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1619)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 22 more


plz any body help me


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 3:31 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
You don't need the @column annotation and it might actually be the cause of your problem.

regards
Jens


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 4:25 am 
Newbie

Joined: Wed Jul 18, 2007 2:49 am
Posts: 3
Location: delhi
thanks schauder

i remove @Column annotation.
problem now remains same.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 4:32 am 
Newbie

Joined: Wed Jul 18, 2007 2:49 am
Posts: 3
Location: delhi
palsushil wrote:
thanks schauder

i remove @Column annotation.
problem now remains same.


i use database table --

employee CREATE TABLE `employee` (
`id` smallint(6) NOT NULL,
`name` varchar(30) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

plz give me some sugetions


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 1:30 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Let hibernate generate the schema and check the difference.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 3:25 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Do you have a sequence named HIBERNATE_SEQUENCE in your schema? This normally gets created by hibernate in order to generate IDs.

Jens


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.