Link to home
Start Free TrialLog in
Avatar of senecaglobal
senecaglobalFlag for India

asked on

"ID": invalid identifier in hibernate application

I developed a standalone  hibernate application using hibernate3.2 version. when i run the application i'm getting the following error.i attached all the source files.

 java.sql.BatchUpdateException: ORA-00904: "ID": invalid identifier
Any solution?

regards
rani
MyContact.java
---------------
package com.surya.hibernate;

public class MyContact {
	
	  private String firstName;
	  private String lastName;
	  private String email;
	  private long id;
	  
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}

	  
	  
}

=============================================
FirstExample .java

package com.surya.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class FirstExample {
  public static void main(String[] args) {
    Session session = null;

    try{
      // This step will read hibernate.cfg.xml and prepare hibernate for use
      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
       session =sessionFactory.openSession();
        //Create new instance of Contact and set values in it by reading them from form object
         System.out.println("Inserting Record");
        MyContact contact = new MyContact();
        contact.setId(333);
        contact.setFirstName("Deepak");
        contact.setLastName("Kumar");
        contact.setEmail("deepak_38@yahoo.com");
        session.save(contact);
        System.out.println("Done");
    }catch(Exception e){
      System.out.println(e.getMessage());
    }finally{
      // Actual contact insertion will happen at this step
      session.flush();
      session.close();

      }
    
  }
}
============================
contact.hbm.xml
----------------

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 5, 2010 10:14:49 AM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
    <class name="com.surya.hibernate.MyContact" table="MYCONTACT">
        <id name="id" type="long">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="firstName" type="java.lang.String">
            <column name="FIRSTNAME" />
        </property>
        <property name="lastName" type="java.lang.String">
            <column name="LASTNAME" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
    </class>
</hibernate-mapping>
===========================

hibernate.cfg.xml
------------------


<?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">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@//localhost:1521/DB1</property>
        <property name="hibernate.connection.username">scott</property>    
		<property name="hibernate.connection.password">tiger</property>     
		<property name="hibernate.connection.pool_size">10</property>    
		<property name="show_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> 
		<!-- Mapping files -->
		<mapping resource="contact.hbm.xml"/>

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

Open in new window

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Check if you have the column with name 'ID' in the MYCONTACT table

can you replace line 61 with
Long longVar = new Long(333);
 contact.setId(longVar);

Perhaps the ID column is a Identity column?  If so, you cannot update this column, its value is generated automatically...
Avatar of senecaglobal

ASKER

i tried in both ways.still i'm getting the same error
Please post the column names of MYCONTACT table
<generator class="assigned" />
is it Incremental filed ?
then
<generator class="increament" />  
>>> Please post the column names of MYCONTACT table
Yes thats correct only .
ASKER CERTIFIED SOLUTION
Avatar of Murali Murugesan
Murali Murugesan
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

In FirstExample .java
change the following from contact.setId(333); to <b>contact.setId(333L);

if you parameterize it then use a long variable.

this should solve your problem, share your thoughts :)