-->
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.  [ 5 posts ] 
Author Message
 Post subject: Long cast to int
PostPosted: Fri Jun 18, 2010 2:25 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hello,

I have a puzzling message from Hibernate. On a findBy method it tries to use a Long instead of an integer.

I didn't specify any Long in the domain class, nor in the mapping, nor in the test..

Any clue ?

Thanks

Stephane

Code:
Hibernate:
    insert
    into
        contact_referer
        (id, version, list_order, description)
    values
        (null, ?, ?, ?)
08:04:28,460 DEBUG IntegerType:133 - binding '0' to parameter: 1
08:04:28,461 DEBUG IntegerType:133 - binding '2' to parameter: 2
08:04:28,461 DEBUG TextType:133 - binding 'Google' to parameter: 3
Hibernate:
    call identity()
Hibernate:
    insert
    into
        contact_referer
        (id, version, list_order, description)
    values
        (null, ?, ?, ?)
08:04:28,467 DEBUG IntegerType:133 - binding '0' to parameter: 1
08:04:28,467 DEBUG IntegerType:133 - binding '1' to parameter: 2
08:04:28,467 DEBUG TextType:133 - binding 'Facebook' to parameter: 3
Hibernate:
    call identity()
Hibernate:
    select
        this_.id as id94_0_,
        this_.version as version94_0_,
        this_.list_order as list3_94_0_,
        this_.description as descript4_94_0_
    from
        contact_referer this_
    where
        this_.list_order>?
    order by
        this_.list_order asc
08:04:28,474 DEBUG IntegerType:133 - binding '0' to parameter: 1
08:04:28,475  INFO IntegerType:140 - could not bind value '0' to parameter: 1; java.lang.Long cannot be cast to java.lang.Integer


The test that fails:

Code:
   @Test
   public void testFindNext() {
      contactReferer0 = contactRefererDao.makePersistent(contactReferer0);
      contactReferer1 = contactRefererDao.makePersistent(contactReferer1);
      ContactReferer contactReferer = contactRefererDao.findNextByListOrder(0);
      assertEquals(1, contactReferer.getListOrder());
      assertEquals("Facebook", contactReferer.getDescription());
      contactReferer = contactRefererDao.findNextByListOrder(1);
      assertEquals(2, contactReferer.getListOrder());
      assertEquals("Google", contactReferer.getDescription());
   }


The mapping file:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 18, 2010 1:03:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="core.domain.ContactReferer" table="contact_referer">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <version name="version" type="int">
            <column name="version" not-null="true" />
        </version>
        <property name="listOrder" type="int">
            <column name="list_order" not-null="true" />
        </property>
        <property name="description" type="text">
            <column name="description" length="65535" not-null="false" />
        </property>
    </class>
</hibernate-mapping>


The domain class:

Code:
public class ContactReferer implements java.io.Serializable {

   private Integer id;
   private int version;
   private int listOrder;
   private String description;

   public ContactReferer() {
   }

   public ContactReferer(int listOrder, String description) {
      this.listOrder = listOrder;
      this.description = description;
   }

   public Integer getId() {
      return this.id;
   }

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

   public int getVersion() {
      return this.version;
   }

   public void setVersion(int version) {
      this.version = version;
   }

   public int getListOrder() {
      return this.listOrder;
   }

   public void setListOrder(int listOrder) {
      this.listOrder = listOrder;
   }

   public String getDescription() {
      return this.description;
   }

   public void setDescription(String description) {
      this.description = description;
   }

}


Top
 Profile  
 
 Post subject: Re: Long cast to int
PostPosted: Fri Jun 18, 2010 2:53 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
My mistake...

The Dao class was using a long...

Code:
   @Override
   public ContactReferer findNextByListOrder(long listOrder) {
      Criteria criteria = getSession().createCriteria(ContactReferer.class);
      criteria.add(Restrictions.gt("listOrder", listOrder)).addOrder(Order.asc("listOrder"));
      return (ContactReferer) criteria.uniqueResult();
   }


On the side I also saw something interesting.

As the MySql table column is an unsigned int, the JDBC driver will use a Java long, because there is no unsigned keyword in Java.

Correct me if I'm wrong but is it that in Java, as there are no unsigned data types and a regular (java) int can't hold the same range of values as an unsigned int in MySQL, the MySql JDBC driver automatically converts a unsigned int to (java) long ?

Cheers


Top
 Profile  
 
 Post subject: Re: Long cast to int
PostPosted: Fri Jun 18, 2010 3:37 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
The MySql documentation says at the page

http://dev.mysql.com/doc/refman/5.0/en/ ... sions.html

INT,INTEGER[(M)] [UNSIGNED] INTEGER [UNSIGNED] java.lang.Integer, if UNSIGNED java.lang.Long


Top
 Profile  
 
 Post subject: Re: Long cast to int
PostPosted: Fri Jun 18, 2010 3:51 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
I tried with using a Java long, changed the mapping file, the domain class and the Dao class for that, but Hibernate complains and says the MySql table column should be a bigint.

Caused by: org.hibernate.HibernateException: Wrong column type in db_thalasoft.contact_referer for column list_order. Found: int, expected: bigint

The list_order property was defined by:
Code:
create table if not exists contact_referer
(
id int unsigned not null auto_increment,
version int unsigned not null,
list_order int unsigned not null default 0,
description text not null default '',
primary key (id), unique (id)
) type = INNODB;


A show create table sql statement gives:
Code:
mysql> show create table contact_referer;
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table           | Create Table                                                                                                                                                                                                                                                                                                          |
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| contact_referer | CREATE TABLE `contact_referer` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `version` int(10) unsigned NOT NULL,
  `list_order` int(10) unsigned NOT NULL default '0',
  `description` text NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 |
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


A desc sql statement gives:
Code:
mysql> desc contact_referer;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| version     | int(10) unsigned | NO   |     |         |                |
| list_order  | int(10) unsigned | NO   |     | 0       |                |
| description | text             | NO   |     |         |                |
+-------------+------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)


This seems in contradiction with the above link to the MySql documentation, in which one can read that a column of type unsigned int is mapped to a Java long.

Maybe, the issue comes from the length of the int.. Mine is 10.


Top
 Profile  
 
 Post subject: Re: Long cast to int
PostPosted: Fri Jun 18, 2010 3:58 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
So I will stay in Java int as it works fine.

I still wonder why the Java long would not work with the MySql column type unsigned int.


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