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 8428805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:09:27+00:00 2026-06-10T05:09:27+00:00

What I am trying to do is read a text file in the code

  • 0

What I am trying to do is read a text file in the code and insert it into a table called employee1. And I am getting this error:

Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values
(?, ?, ?, ?) identifier of an instance of com.Employee altered from 1
to 2 Exception in thread “main” org.hibernate.HibernateException:
identifier of an instance of com.Employee altered from 1 to 2 at
org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:51)
at
org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:82)
at
org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
at
org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
at
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669) at
roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:98)

Code of the Employee.java

public class Employee 
{

    private int id;
    private String name;
    private double salary;
    private String manager;


    public int getId() 
    {
        return id;
    }

    public void setId(int s) 
    {
        id = s;
    }
    //***************************************************//  
    public String getName() 
    {
        return name;
    }

    public void setName(String s) 
    {
        name = s;
    }
    //***************************************************//  
    public double getSalary() 
    {
        return salary;
    }

    public void setSalary(double s) 
    {
        salary = s;
    }
    //***************************************************//
    public String getManager() 
    {
        return manager;
    }

    public void setManager(String s) 
    {
        manager = s;
    }
}

Code of the EmployeeEx.java(exection)

import java.io.File;
import java.util.Scanner;

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

import com.Employee;

public class EmployeeEx 
{

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

    try
    {       
        Transaction transaction = null; 

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        session =sessionFactory.openSession();

        Employee em = new Employee();   

        File f=new File("c:/Class/Employee1.txt") ;
        Scanner scan=new Scanner(f);
        transaction = session.beginTransaction();
        while(scan.hasNext())
        {               

            String line=scan.nextLine();            
            String empArray[]=line.split(" ");


            em.setId(Integer.parseInt(empArray[0]));
            em.setName(empArray[1]);
            em.setSalary(Double.parseDouble(empArray[2]));
            em.setManager(empArray[3]);

            session.save(em);
            transaction.commit();
        }


}
catch(Exception e)
{
    System.out.println(e.getMessage());
}
finally
{
    session.flush();
    session.close();

}


}

}

The config file:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
  <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
  <property name="hibernate.connection.username">system</property>
  <property name="hibernate.connection.password">system</property>
  <property name="hibernate.connection.pool_size">10</property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.OracleDialect</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  <!-- Mapping files -->
  <mapping resource="contact.hbm.xml"/>
  <mapping resource="com.hbm.xml"/>
</session-factory>
</hibernate-configuration>

The mapping file:

<?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>
<class name="com.Employee" table="EMPLOYEE1">
   <id name="id" type="int" column="ID" >
   <generator class="assigned"/>
  </id>

  <property name="name">
     <column name="NAME" />
  </property>
  <property name="salary">
    <column name="SALARY"/>
  </property>
  <property name="manager">
    <column name="MANAGER"/>
  </property>
</class>

</hibernate-mapping>
  • 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-10T05:09:28+00:00Added an answer on June 10, 2026 at 5:09 am

    Effectively you are having just one Employee instance and you are persisting the same one again and again…

    Employee em = new Employee();    // Not a right place..
    
    while(scan.hasNext())
    {               
       // Employee em = new Employee(); // Should be here...
       em.setId(Integer.parseInt(empArray[0]));
       em.setName(empArray[1]);
       em.setSalary(Double.parseDouble(empArray[2]));
       em.setManager(empArray[3]);
       ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read values from a text file using the Qt code
I am trying to write some simple code which will read a text file
I'm trying to read values from a text file into a hashtable, I want
I am trying to read a simple text file into a String. Of course
I am trying to read a text file using the code (pasted below), but
I'm trying to read a text file containing the name of the image, and
I'm trying to read a simple text file from my native code. I placed
I am trying to read a text file using jquery, like this: // LOAD
i am trying to read jpg file and convert into byte array this is
I'm trying to read a text file that looks like this 0,-16,-4,12,10,4,-14,8,44,8,8,12,-4 1,-16,-4,12,10,4,-14,6,43,10,10,12,-4 2,-16,-6,10,11,2,-13,4,43,10,11,12,-4

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.