Hibernate mapping problem.

Discussion in 'Java' started by shaji.cc@gmail.com, Aug 25, 2005.

  1. Guest

    Hi hibernate guys,

    I am facing a problem with data type problem in hibernate mapping.
    Below is a standard example from hibernate.org. It works perfectly
    fine.

    public class Person {

    private Long id;
    private int age;
    private String firstname;
    private String lastname;

    Person() {}

    // Accessor methods for all properties, private setter for 'id'

    }


    <hibernate-mapping>

    <class name="Person" table="PERSON">
    <id name="id" column="PERSON_ID">
    <generator class="increment"/>
    </id>
    <property name="age"/>
    <property name="firstname"/>
    <property name="lastname"/>
    </class>

    </hibernate-mapping>



    In my case data type of id is int.
    In database also it is int. I am using MSSQL server and hibernate3.0
    I am getting an exception like
    java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Value
    can not be converted to requested type

    Is there any other way?
     
    , Aug 25, 2005
    #1
    1. Advertising

  2. <> schrieb im Newsbeitrag
    news:...
    > <hibernate-mapping>

    [snip]
    >
    > <class name="Person" table="PERSON">
    > <id name="id" column="PERSON_ID">
    > <generator class="increment"/>
    > </id>

    [snip]
    > In my case data type of id is int.
    > In database also it is int. I am using MSSQL server and hibernate3.0
    > I am getting an exception like
    > java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Value
    > can not be converted to requested type
    >
    > Is there any other way?


    You should use long or better Long as the type for your id. The database
    type int my have another domain than the Java int and may be mapped to long
    by Hibernate. Note that in the example above there is no type defined. So
    Hibernate derives the type of your POJO property from the database what
    probably leads to long instead of int.
    Nevertheless your int id should work, if you define id like

    <id name="id" column="PERSON_ID" type="int">
    <generator class="increment"/>
    </id>

    Regards
    Sebastian
     
    Sebastian Scheid, Aug 25, 2005
    #2
    1. Advertising

  3. Guest

    , Aug 25, 2005
    #3
  4. Roedy Green Guest

    On 25 Aug 2005 07:24:32 -0700, wrote or quoted :

    > private Long id;
    > private int age;


    There is something puzzling about that example. You have an Object
    Long but a primitive int. I would think Hibernate/the database would
    demand one way or the other.

    I have long advocated that you should be able to access SQL with an
    iterator of custom objects for the result set. It looks as if
    Hibernate is doing that. Am I correct?

    Does Hibernate generate XML from a class description? Does it do the
    reverse? Does it generate SQL queries? Does it turn SQL queries into
    XML or Class descriptions?

    Usually there is a ton of detailed documentation, but very little
    overview documentation that answer the "obvious" questions you need to
    know to have a clue what a tool is for. I try to fill in that gap
    with the Java glossary entries.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Aug 26, 2005
    #4
  5. Adam Maass Guest

    "Roedy Green" <> wrote:
    >
    >
    > I have long advocated that you should be able to access SQL with an
    > iterator of custom objects for the result set. It looks as if
    > Hibernate is doing that. Am I correct?
    >
    > Does Hibernate generate XML from a class description? Does it do the
    > reverse? Does it generate SQL queries? Does it turn SQL queries into
    > XML or Class descriptions?
    >
    > Usually there is a ton of detailed documentation, but very little
    > overview documentation that answer the "obvious" questions you need to
    > know to have a clue what a tool is for. I try to fill in that gap
    > with the Java glossary entries.
    >


    Hibernate is an Object/Relational Mapper.

    Conceptually, you have entities in the database, and objects in memory.
    Objects map to rows in tables. Properties of those objects map to columns of
    the row. Conceptually, you start a (Hibernate) transaction, fish one or more
    POJOs out of the database (somehow), do stuff with them (that might change
    their state) and commit the Hibernate transaction. Hibernate translates the
    state modifications in the POJOs into the correct SQL DML to write the
    changes back to the database.

    Yes, you can iterate a SQL query with POJOs. It's not the first use-case
    that Hibernate attempts to solve, but it is in there. You need to be aware
    of what the mapping between the database world and the POJO world is,
    though: it won't generate classes on the fly.

    There is an experimental feature that does not require the memory
    representation to be POJOs, but can be a DOM4J DOM instance instead. Or just
    a tree of Map instances.

    I don't think there's a straightforward way to generate the DOM from the
    POJOs, or POJOs from the DOM without going to the database first, but that
    seems like it's an obvious extension... the mapping document can be
    one-and-the-same.
     
    Adam Maass, Aug 28, 2005
    #5
  6. Roedy Green Guest

    On Sat, 27 Aug 2005 17:11:38 -0700, "Adam Maass"
    <> wrote or quoted :

    >Conceptually, you have entities in the database, and objects in memory.
    >Objects map to rows in tables. Properties of those objects map to columns of
    >the row. Conceptually, you start a (Hibernate) transaction, fish one or more
    >POJOs out of the database (somehow), do stuff with them (that might change
    >their state) and commit the Hibernate transaction. Hibernate translates the
    >state modifications in the POJOs into the correct SQL DML to write the
    >changes back to the database.


    Is Hibernate a server side only beast or do you typically ship these
    POJQs over the wire to Applets, and send only the changes back?

    This is similar to what I faked in Java 1.0 with Symantec dbAnywhere.
    I am glad to see the idea catching on. I went a step further
    integrating the GUI object with the database object, so that on commit
    all "smart" GUI objects whose values had changed triggered an SQL
    update.

    There is a lot of pointless overhead going on that could be bypassed
    if Hibernate talked directly to the database engine, bypassing SQL.
    Perhaps some day there will be Hibernate bypass into the engine.
    Ideally the database would deliver binary format records that could be
    instantly converted to Objects. The list of changes could be stream of
    binary field numbers.



    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Aug 28, 2005
    #6
  7. Adam Maass Guest

    "Roedy Green" <> wrote:
    > On Sat, 27 Aug 2005 17:11:38 -0700, "Adam Maass"
    > <> wrote or quoted :
    >
    >>Conceptually, you have entities in the database, and objects in memory.
    >>Objects map to rows in tables. Properties of those objects map to columns
    >>of
    >>the row. Conceptually, you start a (Hibernate) transaction, fish one or
    >>more
    >>POJOs out of the database (somehow), do stuff with them (that might change
    >>their state) and commit the Hibernate transaction. Hibernate translates
    >>the
    >>state modifications in the POJOs into the correct SQL DML to write the
    >>changes back to the database.

    >
    > Is Hibernate a server side only beast or do you typically ship these
    > POJQs over the wire to Applets, and send only the changes back?
    >


    The POJOs can be "detached" from the Hibernate session in which they
    originate, and provided they all implement Serializable, can be shipped to a
    client which makes the changes, then serialized back to the server, where
    they can be re-attached to another Hibernate session; the second session
    then makes the updates.

    There are some tricky bits to this: some of the semantics that Hibernate
    guarantees while you're operating in the context of a single session aren't
    true any more. And Hibernate expends a fair amount of effort to enable lazy
    fetching from the database; you can't fetch lazily from the database if the
    objects have already been detached... you have to know the extent of the
    object graph you want to send and ensure that it has all been fetched before
    detachment.

    >
    > There is a lot of pointless overhead going on that could be bypassed
    > if Hibernate talked directly to the database engine, bypassing SQL.
    > Perhaps some day there will be Hibernate bypass into the engine.
    > Ideally the database would deliver binary format records that could be
    > instantly converted to Objects. The list of changes could be stream of
    > binary field numbers.
    >


    This has been tried before. They were called object databases.

    IMO, object databases were brilliant technology that were dead before they
    arrived; the RDBMSs have too much entrenched technology, query performance,
    secondary tools, and just general industry know-how to have much of a
    competitive threat from OODBMSs. The purchasing manager's thinking goes
    something like this: OODBMSs don't buy you all that much more than RDBMSs,
    and you give up an awful lot to get there. So why should he take the risk?
     
    Adam Maass, Aug 28, 2005
    #7
  8. lars.egarots

    Joined:
    Jul 22, 2008
    Messages:
    1
    Sometimes its hard and tedious to get the mapping correct. I like to use this tool to have it done automatically: http://www.javatosql.com

    It analyzes my Java objects and creates the sql and hibernate config files.
     
    lars.egarots, Jul 22, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    0
    Views:
    1,743
    Ilias Lazaridis
    Dec 27, 2004
  2. garhone

    Hibernate mapping problem

    garhone, Aug 24, 2006, in forum: Java
    Replies:
    0
    Views:
    440
    garhone
    Aug 24, 2006
  3. ducnbyu
    Replies:
    2
    Views:
    6,180
    ducnbyu
    Sep 8, 2006
  4. Replies:
    0
    Views:
    386
  5. Replies:
    1
    Views:
    687
    David Portabella
    Sep 9, 2007
Loading...

Share This Page