facebook

Hibernate mapping to Integer

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #244954 Reply

    awamser
    Member

    I’m guessing that I’m probably missing something simple, but I don’t understand why the mapping tools maps to java.lang.Integer and not int. Then when using the setter method you have to add and extra step of creating a new integer. For example…

    You have the following method in your abstract class…

    public setAge(java.lang.Integer age) {
    this.age = age
    }

    Create a new object and call the setter method would require creating a Integer.

    setAge(new Integer(25))

    if it was declared as int.

    setAge(25)

    #244972 Reply

    Riyad Kalla
    Member

    AFAIK this is preferred for Hibernate. Hibernate deals only in objects and when you map to native fields yourself, I believe (seems I read this somewhere, or looked at the code) that Hibernate is actually internally mapping from an Object to the native value to populate your POJO, so if you just leave it in Integer it saves you that conversion.

    To ease your mind you can write a contrived micro-benchmark of creating millions of random Integers and see how much time it actually costs the VM. It’s typically so insignifigant for 10s of thousands of instances that it would never be an issue in real life creating 10-20 or even 100 of them for a query.

    #244977 Reply

    snpe
    Member

    hibernate use object internal (same type for Integer and int,for example), but there is more important reason for using obejct wrapper except primitive type – handling null value – you can’t have null for primitive type
    I don’t use primitive type anywhere

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: Hibernate mapping to Integer

You must be logged in to post in the forum log in