Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8583983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:39:51+00:00 2026-06-11T21:39:51+00:00

I am learning spring data access and I’ve tried to insert data through hibernateTempate.

  • 0

I am learning spring data access and I’ve tried to insert data through hibernateTempate. Here is my code:

UPDATED

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <context:annotation-config/>

    <context:component-scan base-package="com.eric.mvnlab"/>

    <context:property-placeholder properties-ref="properties"/>

    <bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:application.properties</value>
            </list>
        </property>
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages"/>
        <property name="defaultEncoding" value="${source.encoding}"/>
    </bean>


    <!-- DAO layer -->
    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        ...
    </bean>

    <!-- Hibernate session factory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--<property name="packagesToScan" value="com.eric.mvnlab.model.*" />-->
        <property name="annotatedClasses">
            <list>
                <value>com.eric.mvnlab.model.Machine</value>
            </list>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

</beans>

MachineDAO.java

package com.eric.mvnlab.server.dao.impl;

import com.eric.mvnlab.model.Machine;
import org.springframework.stereotype.Repository;

@Repository("machineDao")
public class MachineDAO extends GenericHibernateDAOImpl<Machine, Integer> {
    public void addMachine(Machine machine) {
        getHibernateTemplate().save(machine);
    }

    public Machine findById(int id) {
        return (Machine) getHibernateTemplate().get(Machine.class, id);
    }
}  

Main.java

package com.eric.mvnlab;

import com.eric.mvnlab.model.Machine;
import com.eric.mvnlab.server.dao.impl.MachineDAO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 9/25/12
 * Time: 3:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        MachineDAO machineDAO = (MachineDAO)context.getBean("machineDao");
        machine.setHostname("MyLaptop");
        machine.setIpaddress("127.0.0.1");
        machineDAO.addMachine(machine);
    }
}

Machine.java

package com.eric.mvnlab.model;

import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

@Entity
@Table(name="MACHINE")
public class Machine {
    @Column(name="MID")
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)

    private int mid;

    @Column(name="HOSTNAME")
    private String hostname;

    @Column(name="IPADDRESS")
    private String ipaddress;

    public String getIpaddress() {
        return ipaddress;
    }

    public void setIpaddress(String ipaddress) {
        this.ipaddress = ipaddress;
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public int getMid() {
        return mid;
    }

    public void setMid(int machineId) {
        this.mid = machineId;
    }
}

Table Machine has three columns: mid, hostname and ipaddress. mid is the primary key and is auto increment.

When I run Main, I got below output:

Hibernate: insert into MACHINE (MID, HOSTNAME, IPADDRESS) values (null, ?, ?)
Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [com.eric.mvnlab.model.Machine]; SQL [insert into MACHINE (MID, HOSTNAME, IPADDRESS) values (null, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.eric.mvnlab.model.Machine]
WARN  - JDBCExceptionReporter      - SQL Error: -798, SQLState: 428C9
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:629)
ERROR - JDBCExceptionReporter      - DB2 SQL Error: SQLCODE=-798, SQLSTATE=428C9, SQLERRMC=MID, DRIVER=4.7.85

Could anyone tell me why the entity attribute data is not passed to sql statement?

NOTE:
If you are using DB2 9.7, and you are likely to met this error:

org.hibernate.HibernateException: The database returned no natively generated identity value

It’s a DB2 jdbc driver bug, and the solution is to use a later version driver, such as 10.1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:39:52+00:00Added an answer on June 11, 2026 at 9:39 pm

    Note that you configured Hibernate to use HSQLDB dialect, whereas your error message comes from DB2. You need to configre a dialect that matches your DBMS, see 3.4.1. SQL Dialects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I first started learning about Spring, things were configured in the applicationContext.xml file.
I'm learning spring for android with androidannotations and I'm having trouble casting JSON data
I'm slowly learning C# and bit confused about the code. I have here a
I am learning Spring-MVC in my first project here. After reading the documentation on
Learning Spring (3.1.0) and Hibernate (4.1.1). Just wondering what most developers do when handling
I am learning Spring and building a sample app. I am getting the error:
I've been converting a Java Servlet based webapp as a means of learning Spring.
Learning some VBA. So far, I've constructed this piece of code which should allow
Ok I can successfully connect to my Access database but I am still learning
I am working on a Data Access Layer Design, we have not finalized what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.