-->
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: Problems with Using a custom Primary Key
PostPosted: Sat Jan 20, 2007 4:36 pm 
Newbie

Joined: Tue Jan 24, 2006 7:50 pm
Posts: 11
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.2.1 GA
Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
Caused by: java.sql.BatchUpdateException: Data truncated for column 'BANK_ID' at row 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:647)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 18 more

Name and version of the database you are using:
MYSQL Server 1.2.2

The generated SQL (show_sql=true):
Hibernate: insert into BANK (BANK_NAME, BANK_CODE, LCOUNT, BANK_ID) values (?, ?, ?, ?)

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html

I have recently started using Hibernate. I have managed to get everything to work. I made a small change so that the one of the DAO's primary key is of type DAOPrimary key from Long. DAOPrimary is simply a Integer wrapper class. The problem is when i try to do a save i get the following error

org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
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.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at dao.HibernateDAOBase.commit(HibernateDAOBase.java:116)
at dao.HibernateDAOBase.persist(HibernateDAOBase.java:17)
at dao.HibernateDAOBase.save(HibernateDAOBase.java:30)
at hib.client.HibernateClient.save(HibernateClient.java:112)
at hib.client.HibernateClient.<init>(HibernateClient.java:29)
at hib.client.HibernateClient.main(HibernateClient.java:22)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: java.sql.BatchUpdateException: Data truncated for column 'BANK_ID' at row 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:647)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 18 more


The code from the xml is as so

<id name="primaryKey" type="dao.DAOPrimaryKey">
<column name="BANK_ID" sql-type="NUMERIC" not-null="true"/>
<generator class="dao.TableIDGenerator">
<param name="TABLE_NAME">BANK</param>
<param name="COLUMN_NAME">BANK_ID</param>
</generator>
</id>

The TableIDGenerator class returns an ID of type DAOPrimaryKey, which is being persisted directly, I believe this should be converted into an SQLType.

If i configure TableIDGenerator to return a Long, Int, or Double everything works fine.

In IBatis for example you have Type Handlers which allows you to convert Custom objects into SQL Types.

Thanks in Advance[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 20, 2007 5:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hibernate have similar things; they are called UserTypes.

but you probably need to have a custom key generator to support your "wrap integer wrapper for fun" id type ,)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 20, 2007 6:17 pm 
Newbie

Joined: Tue Jan 24, 2006 7:50 pm
Posts: 11
Hi Max,
I have created a ID Generator which returns the Customised ID.

Could you give an example of how to use "UserTypes"

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 20, 2007 7:39 pm 
Newbie

Joined: Tue Jan 24, 2006 7:50 pm
Posts: 11
Flicked through the forums finding info about the Types, I came across



Made the following changes including making the Primary Key class a UserType. Now works. Great!!!!

Code:
<typedef name="int" class="dao.DAOPrimaryKey"/>
    <class name="hib.domain.Bank" table="BANK">
        <id name="primaryKey" type="int" column="BANK_ID">
            <generator class="dao.TableIDGenerator">
                <param name="TABLE_NAME">BANK</param>
                <param name="COLUMN_NAME">BANK_ID</param>
            </generator>
        </id>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 21, 2007 7:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
reading the docs about usertypes would also be a good start ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 5:02 am 
Newbie

Joined: Tue Jan 24, 2006 7:50 pm
Posts: 11
Will do Cheers


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.