-->
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.  [ 3 posts ] 
Author Message
 Post subject: Insert problem
PostPosted: Wed Aug 02, 2006 3:21 pm 
Newbie

Joined: Wed Aug 02, 2006 10:17 am
Posts: 12
Hello everybody,

I'm new to hibernate.
In my very basic application i'm trying to add a new object to a database table. The mapping is ok, I already used update and select statments in hql.

The problem is I just dont understand how I should do an insert when only the INSERT INTO ... SELECT ... form is supported. How can I do it?
Is it possible at all to do this with HQL? Is there any substetution for the INSERT INTO ... VALUES ... form?

Can you please help me? I'm really stuck.

Thank you very much!

Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 3:28 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Create a Java object with all required properties and additional properties to be in the insert statement.
Then call session.save( object ), which will insert the new record in the database.
If the primary key information that you used in the object is already existing in the database, an exception will be thrown.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 3:45 am 
Newbie

Joined: Wed Aug 02, 2006 10:17 am
Posts: 12
Thank you for your answer.

I tried what you said, but it doesnt work. I'm still doing something wrong.

my code looks like:
Code:
try{
   session.save(user);
}
catch(Exception e){
   e.getClass();
   e.getMessage();
}


User is the new object I want to save.
The hbm.xml file looks like:

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">

<hibernate-mapping package="de.fum.ag.eanv.model.impl">

    <class name="User" table="users" lazy="false">
   
        <id name="id" column="ID" type="java.lang.Integer">
            <generator class="native"/>
        </id>
                 
        <property name="username"
                column="USERNAME"
                length="50"
                type="java.lang.String"
                not-null="true"/>
        <property name="password"
                column="PASSWORD"
                length="50"
                type="java.lang.String"
                not-null="true"/>
        <property name="enabled"
                column="ENABLED"
                type="java.lang.Boolean"
                not-null="true"/>
        <property name="firstname"
                column="VORNAME"
                length="45"
                type="java.lang.String"
                not-null="false"/>
        <property name="lastname"
                column="NAME"
                length="45"
                type="java.lang.String"
                not-null="false"/>
    </class>
   
</hibernate-mapping>



When I took a closer look at the exception object I realized that there was an sql field coming with the exception. It said:
Code:
insert into users (USERNAME, PASSWORD, ENABLED, VORNAME, NAME) values (?, ?, ?, ?, ?)


That cant be right. Where is the id field? Where are the values of the object?

Do you know what I'm doing wrong? Do you have any hints for me?

Thank you.

Stefan


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