-->
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.  [ 9 posts ] 
Author Message
 Post subject: saving object to table problems
PostPosted: Thu Sep 28, 2006 4:41 am 
Beginner
Beginner

Joined: Mon Sep 18, 2006 5:33 am
Posts: 25
Im getting an error which may have something to do with my hibernate code

Code:
org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: Could not save object; bad SQL grammar []; nested exception is java.sql.SQLException: Table not found in statement [select next_hi from hibernate_unique_key for update]   


Any ideas? What im trying to do is save an object to the table and im doing it like this in the dao

Code:
public void addPhotoinfo(Photo p) {
      getHibernateTemplate().save(p);
      
   }


my complete dao
Code:
package src.db;

import java.util.List;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import src.bus.Photo;

public class PhotoDaoHib extends HibernateDaoSupport implements PhotoDao  {

   public Photo getPhotoinfo(int id) {
      
      return (Photo) getHibernateTemplate().load(Photo.class, id);      
   }

   public List getPhotoinfoList() {
      return getHibernateTemplate().find("from Photo p");
   }

   public void addPhotoinfo(Photo p) {
      getHibernateTemplate().save(p);
      
   }   
}


and where i call the dao (controller)
Code:
package src.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import src.bus.Photo;
import src.bus.PhotoManager;

public class PhotoinfoController extends SimpleFormController{
   
   private PhotoManager pm;

   protected ModelAndView onSubmit(HttpServletRequest request,
         HttpServletResponse response, Object command, BindException errors) {
      
      Photo bean = (Photo)command;
      pm.setPhoto(bean); // write collected form data to the database table
      
      
      //return super.onSubmit(request, response, command, errors);
      return new ModelAndView(new RedirectView(getSuccessView()));

   }

   public PhotoManager getPhotoManager() {
      return pm;
   }

   public void setPhotoManager(PhotoManager pm) {
      this.pm = pm;
   }
}



Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 9:16 am 
Beginner
Beginner

Joined: Mon Sep 18, 2006 5:33 am
Posts: 25
here is my hbm.xml file

any ideas on how to fix the problem ?
photo.hbm.xml

Code:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
   <class name="src.bus.Photo" table="Photo">
      <id name="id" column="id"
         type="java.lang.Integer" unsaved-value="-1">
         <generator class="identity"></generator>
      </id>
      <property name="location" column="location" type="string" />
      <property name="photographer" column="photographer"
         type="string" />
      <property name="datetaken" column="datetaken" type="string" />
      <property name="description" column="description" type="string" />
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 6:39 pm 
Beginner
Beginner

Joined: Mon Sep 18, 2006 5:33 am
Posts: 25
hi,

And i use the IDENTITY type for my id. Here is my schema

Code:
Create Table Photo (
   id integer identity Primary key,
   location varchar(25) NOT NULL,
   photographer varchar(25) NOT NULL,
   datetaken varchar(25) NOT NULL,
   description varchar(25) NOT NULL
);


and here is my photo bean if it help
Code:
package src.bus;

import java.io.Serializable;
import java.util.Date;

public class Photo implements Serializable {
   
   private int id;
   private String location; //where photo was taken
   private String photographer; //map to username
   private String datetaken;
   private String description;
   
   public String getDatetaken() {
      return datetaken;
   }
   
   public void setDatetaken(String datetaken) {
      this.datetaken = datetaken;
   }
   
   public String getDescription() {
      return description;
   }
   
   public void setDescription(String description) {
      this.description = description;
   }      
   
   public String getLocation() {
      return location;
   }
   
   public void setLocation(String location) {
      this.location = location;
   }
   
   public String getPhotographer() {
      return photographer;
   }
   
   public void setPhotographer(String photographer) {
      this.photographer = photographer;
   }

   public int getId() {
      return id;
   }

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



complete error
Code:
exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: Could not save object; bad SQL grammar []; nested exception is java.sql.SQLException: Table not found in statement [select next_hi from hibernate_unique_key for update]
   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:406)
   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: Could not save object; bad SQL grammar []; nested exception is java.sql.SQLException: Table not found in statement [select next_hi from hibernate_unique_key for update]
   org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:224)
   org.springframework.orm.hibernate.HibernateAccessor.convertJdbcAccessException(HibernateAccessor.java:364)
   org.springframework.orm.hibernate.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:351)
   org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:369)
   org.springframework.orm.hibernate.HibernateTemplate.save(HibernateTemplate.java:553)
   src.db.PhotoDaoHib.addPhotoinfo(PhotoDaoHib.java:21)
   src.bus.PhotoManager.setPhoto(PhotoManager.java:22)
   src.controller.PhotoinfoController.onSubmit(PhotoinfoController.java:22)
   org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:258)
   org.springframework.web.servlet.mvc.AbstractFormController.handleInvalidSubmit(AbstractFormController.java:649)
   org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:256)
   org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
   org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)



Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 12:06 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
The hibernate_unique_key is an internal table that Hiberntae uses to store the next primary id to be used for cases of "identity". Have you configured hibernate to automatically create the tables when demand?
You might want to setup the hibernate.hbm2ddl.auto value to update so that it automatically updates any tables that may not have been created .


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 12:14 am 
Beginner
Beginner

Joined: Mon Sep 18, 2006 5:33 am
Posts: 25
Quote:
Have you configured hibernate to automatically create the tables when demand?


No i havnt, how can i do this?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 12:41 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Setting hibernate.hbm2ddl.auto=update in ur hibernate.cfg.xml should help. Check out the Reference docs for more options.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 1:12 am 
Beginner
Beginner

Joined: Mon Sep 18, 2006 5:33 am
Posts: 25
hi,

ive tried adding it to my photomapper-servlet.xml file (cause i dont have a hibernate.cfg.xml file) like so..

Code:
<beans>
   <bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      <property name="maxUploadSize" value="1000000000" />
   </bean>

   <bean id="myDataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      destroy-method="close">
      <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
      <property name="url" value="jdbc:hsqldb:mem:test" />
      <property name="username" value="sa" />
      <property name="password" value="" />
   </bean>

   <bean id="mySessionFactory"
      class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
      <property name="dataSource" ref="myDataSource" />
      <property name="mappingResources">
         <list>
            <value>src/db/Photo.hbm.xml</value>
         </list>
      </property>
      <property name="hibernateProperties">
         <list>
            <value>
               hibernate.dialect=org.hibernate.dialect.MySQLDialect
            </value>
            [b]<value>hibernate.hbm2ddl.auto=update</value>[/b]
         </list>
      </property>
   </bean>

.....


Im getting an error which means its prob. not the right place to put it. Where abouts do i put it? is there i site that shows this stuff.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 1:25 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Try this
Code:
<property name="hibernateProperties">
                        <props>
                                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                                <prop key="hibernate.hbm2ddl.auto">update</prop>
                        </props>
                </property>



Checkout spring examples(Petclinic) which is where i picked the above.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 3:47 am 
Beginner
Beginner

Joined: Mon Sep 18, 2006 5:33 am
Posts: 25
hmm im getting this exception now, do i need a jar file to fix?

Code:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/photomapper-servlet.xml]: Initialization of bean failed; nested exception is net.sf.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.MySQLDialect



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