-->
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.  [ 11 posts ] 
Author Message
 Post subject: SQL Server 2005 can read from db but not insert into db
PostPosted: Fri May 25, 2007 4:43 am 
Newbie

Joined: Fri May 25, 2007 4:03 am
Posts: 15
Hey!
Driver (tried both):
jtds-1.2, sqljdbc
Hibernate vers: 3.2.4

I get following exception if I try to insert into db:

Exception:
SCHWERWIEGEND: Parameter #9 has not been set.
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [at.pcd.wam.technologie.model.AdvancedAddressModel]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.save(Unknown Source)
at at.pcd.wam.technologie.test.TestHibernate.main(TestHibernate.java:52)
Caused by: java.sql.SQLException: Parameter #9 has not been set.
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.prepareSQL(ConnectionJDBC2.java:602)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:420)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 22 more

interface:
Code:
public interface IGeo {

   /** street */
   public String street = null;
   
   /** houseNumber */
   String houseNumber = null;
   
   /** zip code */
   String zip = null;
   
   /** locality */
   String locality = null;
   
   /** country */
   String country = null;
}


abstract class:
Code:

package at.pcd.wam.technologie.model;

import java.io.Serializable;

import javax.persistence.Transient;

public abstract class AbstractGeoModel implements IGeo, Serializable {


   /** street */
   protected String street;
   
   /** houseNumber */
   protected String houseNumber;
   
   /** zip code */
   protected String zip;
   
   /** locality */
   protected String locality;
   
   /** country */
   @Transient
   protected String country;

   /**
    * transform <code>this</code> into a valid string for geocoding
    * @return model data as string - format [street,house,zip,city,country]
    */
   public abstract String toString();

   /**
    * getter method
    * @return locality
    */
   public String getLocality() {
      return this.locality;
   }

   /**
    * setter method
    * @param locality locality
    */
   public void setLocality(final String locality) {
      this.locality = locality;
   }

   /**
    * getter method
    * @return country
    */
   public String getCountry() {
      return this.country;
   }

   /**
    * setter method
    * @param country country
    */
   public void setCountry(final String country) {
      this.country = country;
   }

   /**
    * getter method
    * @return houseNumber
    */
   public String getHouseNumber() {
      return this.houseNumber;
   }

   /**
    * setter method
    * @param houseNumber house number
    */
   public void setHouseNumber(final String houseNumber) {
      this.houseNumber = houseNumber;
   }

   /**
    * getter method
    * @return street
    */
   public String getStreet() {
      return this.street;
   }

   /**
    * setter method
    * @param street street
    */
   public void setStreet(final String street) {
      this.street = street;
   }

   /**
    * getter method
    * @return zip
    */
   public String getZip() {
      return this.zip;
   }

   /**
    * setter method
    * @param zip zip code
    */
   public void setZip(final String zip) {
      this.zip = zip;
   }
}



my model:
Code:
package at.pcd.wam.technologie.model;

public class AdvancedAddressModel extends AbstractGeoModel {
   
   /** primary key for database table */
   private int id;
   
   /** need for serializable - dynamic serial version UID */
   private static final long serialVersionUID = 7999991466190548693L;

   /** country code - cc */
   private String countryCode;
   
   /** geo coordinate lattidue */
   private String geoLat;
   
   /** geo coordinate longitude */
   private String geoLong;

   /**
    * empty constructor
    */
   public AdvancedAddressModel() {
      //nothin to do
   }
   
   /**
    * constructor for using fields
    * @param street street
    * @param houseNumber houseNumber
    * @param zip zip code
    * @param locality locality
    * @param country country
    */
   public AdvancedAddressModel(final String street, final String houseNumber, final String zip, final String locality,
         final String country) {
      this.street = street;
      this.houseNumber = houseNumber;
      this.zip = zip;
      this.locality = locality;
      this.country = country;
   }   

   @Override
   public String toString() {
      return null;
   }

   /**
    * getter method
    * @return country code
    */
   public String getCountryCode() {
      return this.countryCode;
   }

   /**
    * setter method
    * @param countryCode country code
    */
   public void setCountryCode(final String countryCode) {
      this.countryCode = countryCode;
   }

   /**
    * getter method
    * @return geoLat geo coordinate lattidude
    */
   public String getGeoLat() {
      return this.geoLat;
   }

   /**
    * setter method
    * @param geoLat geo coordinate lattidude
    */
   public void setGeoLat(final String geoLat) {
      this.geoLat = geoLat;
   }

   /**
    * getter method
    * @return geoLong geo coordinate longitude
    */
   public String getGeoLong() {
      return this.geoLong;
   }

   /**
    * setter method
    * @param geoLong geo coordinate longitude
    */
   public void setGeoLong(final String geoLong) {
      this.geoLong = geoLong;
   }

   /**
    * getter method
    * @return id primary key
    */
   public int getId() {
      return this.id;
   }

   /**
    * setter method
    * @param id primary key
    */
   public void setId(final int id) {
      this.id = id;
   }
}


Mapping:
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="at.pcd.wam.technologie.model">

  <class name="at.pcd.wam.technologie.model.AdvancedAddressModel" table="I_80050" schema="dbo" catalog="spc">
     
     <!-- primary key -->
     <id name="id" type="integer" column="ID" length="30">
     <generator class="native"></generator>
     </id>
   
   <property name="countryCode" type="string" column="CC"></property>
   
   <property name="geoLat" type="string" column="GEO_LAT"></property>
   
   <property name="geoLong" type="string" column="GEO_LONG"></property>
   
   <property name="street" type="string" column="STRASSE">
      <meta attribute="field-description">
         without house number.
      </meta>
   </property>
   
   <property name="zip" type="string" column="PLZ"></property>

  </class>
</hibernate-mapping>


main testing class:
Code:
public class TestHibernate {

   /**
    * @param args
    */
   public static void main(String[] args) {
      
      Session session = HibernateUtil.getCurrentSession();
      Transaction ta = session.beginTransaction();
   
      ArrayList<AdvancedAddressModel> aModel = (ArrayList<AdvancedAddressModel>)
         session.createQuery("select id, countryCode from AdvancedAddressModel").list();
      //***
      
      AdvancedAddressModel model = new AdvancedAddressModel();
      model.setZip("zip");
      model.setCountryCode("country code");
      session.saveOrUpdate(model);

      ta.commit();
   }

}


hibernate.cfg.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
   <property name="hibernate.connection.driver_class">
      com.microsoft.jdbc.sqlserver.SQLServerDriver
   </property>
   <property name="hibernate.connection.password">jhcbxr</property>
   <property name="hibernate.connection.url">
      jdbc:microsoft:sqlserver://mag2;DatabaseName=sbc
   </property>
   <property name="hibernate.connection.username">sa</property>
   <property name="hibernate.dialect">
      org.hibernate.dialect.SQLServerDialect
   </property>
   <property name="myeclipse.connection.profile">mag2-sa</property>
   <property name="connection.url">
      jdbc:sqlserver://mag2;databaseName=spc
   </property>
   <property name="connection.username">sa</property>
   <property name="connection.password">jhcbxr</property>
   <property name="connection.driver_class">
      com.microsoft.sqlserver.jdbc.SQLServerDriver
   </property>
   <property name="dialect">
      org.hibernate.dialect.SQLServerDialect
   </property>

   <!-- Enable Hibernate's automatic session context management -->
   <property name="current_session_context_class">thread</property>
   <mapping
      resource="at/pcd/wam/technologie/model/AdvancedAddressModel.hbm.xml" />

</session-factory>
</hibernate-configuration>


best regards


Last edited by y0dA on Fri May 25, 2007 5:30 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 4:58 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

try saveOrUpdate()


Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 5:27 am 
Newbie

Joined: Fri May 25, 2007 4:03 am
Posts: 15
save() or saveOrUpdate() makes no different.

Askings:

+)My Model class can contain objects which are not a db colum?
For example the object "country" does not exist in the db - so this object must be ignored in the mapping file?

+)my db table primary key is an identifier so I have to set in the mapping file:
Code:
   <!-- primary key -->
     <id name="id" type="integer" column="ID" length="30">
     <generator class="native"></generator>
     </id>

correct?

+) My model extends a abstract class and the abstract class implements an interface --> so I have to adapt the mapping file or makes that no different?

I edited my source above.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 5:31 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Sure. You should map only database fields to property value.

Code:
<id name="id" type="integer" column="ID" length="30">
     <generator class="identity"></generator>
    </id>



Amila

(Don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 5:39 am 
Newbie

Joined: Fri May 25, 2007 4:03 am
Posts: 15
+)My Model class can contain objects which are not a db colum?
For example the object "country" does not exist in the db - so this object must be ignored in the mapping file?

+)My Model class can contain objects which are not a db colum?
For example the object "country" does not exist in the db - so this object must be ignored in the mapping file?


That are my problems? Can it concerned with the exception:
SCHWERWIEGEND: Parameter #6 has not been set.
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [at.pcd.wam.technologie.model.AdvancedAddressModel]


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 5:47 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi


Yes you can have variables which are not in your db.

Table1
id
name


class Table{
int id;
String name;
String title;

//getter & setters

}

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>     
    <class
        name="Table"
        table="Table1"
        discriminator-value="0"
    >
        <id
            name="id"
            column="id"
            type="int"
        >
            <generator class=identity">
            </generator>
        </id>
      

        <property
            name="name"
            type="String"
            update="true"
            insert="true"
            column="name"
                   />   

</class>

</hibernate-mapping>


Like that you can ignore title in mapping file.


Amila

(Don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 5:55 am 
Newbie

Joined: Fri May 25, 2007 4:03 am
Posts: 15
I have to set following for all my properties:

<property
name="name"
type="String"
update="true"
insert="true""
column="name"
/>


I really dont recognize the exception "Parameter #6 has not been set."

Furthermore can it be that I must implement all colums from the db into my model class?
For example: Table columns: columA, columB, columC
--> must implement all columns or can I only implement columA and columB in my model?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 6:13 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Yes you can desde what columns you want to map. It means hibernate manipulating only mapped fields. But in some cases (insert or update situatios) exceptions will occurs. (i.e. can't insert null into column4 which does't mapped.


Amila

(Don't forget to rate)


Top
 Profile  
 
 Post subject: having the same problem, maybe 'identity' problem in 3.2.4
PostPosted: Tue Jun 05, 2007 4:00 pm 
Newbie

Joined: Thu Aug 03, 2006 11:40 am
Posts: 4
i'm having the same problem with 3.2.4. i have a SQLServer app using the jtds driver, and the same mapping worked consistently on 3.1.x. But once I upgrade to 3.2.4, i get the 'parameter #x is not set' when inserting.

when i turn on SQL logging, I see that the parameter the driver is complaining about is an 'identity' column property. i suspect that the persister class is forgetting to set a 'null' value for this column in the prepared statement (since the database generates the id) before execute.

this may be a bug (especially since exactly the same mapping works in 3.1).
however, i think there's some sensitivity to screaming 'it's a bug!' around here, so i'll try to investigate more before creating a report. were you able to solve your problem?


Top
 Profile  
 
 Post subject: dynamic-insert seems to be a reliable workaround for me
PostPosted: Tue Jun 05, 2007 4:20 pm 
Newbie

Joined: Thu Aug 03, 2006 11:40 am
Posts: 4
just as a follow-up -- i spent some time stepping through hibernate code with the debugger, and it seems that the AbstractEntityPersister class is not properly setting null values for the generated 'identity' column (at least in my case). the JDBC driver gets angry when you have a positional parameter in your statement, but never set a value for it.

adding 'dynamic-insert="true"' to my class mapping worked around the problem, since the identity columns were then not included in the prepared statement:

<class table="someTable" name="my.class.SomeTable" dynamic-insert="true">
....
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 5:23 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
You might try upgrading to 3.2.4 SP1, it addresses a bug around Identity columns that was introduced in 3.2.4 GA.


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