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

  • Home
  • SEARCH
  • 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 6954255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:34:57+00:00 2026-05-27T14:34:57+00:00

I have programmed a simple hibernate app which isn’t updating the table. The following

  • 0

I have programmed a simple hibernate app which isn’t updating the table. The following is my code. The table gets created but the it isn’t updating

** Contact.java**

 public class Contact {
private String firstName;
private String lastName;
private String email;
private long id;

public String getEmail() {
    return email;
}

public String getFirstName() {
    return firstName;
}

public String getLastName() {
    return lastName;
}

public void setEmail(String string) {
    email = string;
}

public void setFirstName(String string) {
    firstName = string;
}

public void setLastName(String string) {
    lastName = string;
}

public long getId() {
    return id;
}

public void setId(long l) {
    id = l;
}

}

FirstExample.java

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 {

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

        System.out.println("Inserting Record");
        Contact contact = new Contact();
        contact.setId(3);
        contact.setFirstName("Tausif");
        contact.setLastName("rahmathullah");
        contact.setEmail("t.r@vl.net");
        session.save(contact);
        System.out.println("updated");
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.flush();
            session.close();
        }
    }
}
}

hibernate.cfg.xml

<?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">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/simpHiber</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.connection.pool_size">10</property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  <mapping resource="contact.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

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">

<hibernate-mapping>
 <class name="Contact" table="CONTACT">
  <id name="id" type="long" column="ID" >
  <generator class="assigned"/>
  </id>

 <property name="firstName">
 <column name="FIRSTNAME" />
 </property>
 <property name="lastName">
 <column name="LASTNAME"/>
 </property>
 <property name="email">
 <column name="EMAIL"/>
 </property>
</class>

  • 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-05-27T14:34:58+00:00Added an answer on May 27, 2026 at 2:34 pm

    Since you are using then assigned as id you need to set different ids every time you save.

        Contact contact = new Contact();
        contact.setId(3);//Change the id every time you insert.
        contact.setFirstName("Tausif");
        contact.setLastName("rahmathullah");
        contact.setEmail("t.r@vl.net");
        session.save(contact);
    

    Now, If you want update an entry instead of inserting it again use,

        session.saveOrUpdate(contact);
    

    This will fire an update query if the id exist in data base and will insert if it is not.

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

Sidebar

Related Questions

I have programmed a simple hibernate app which leaves me with errors. The following
I have programmed a simple app that every X minutes checks if an image
I have programmed an emulator, but I have some doubts about how to organizate
I have never programmed a gadget for Vista or Seven, but I would like
I have a webservice programmed in coldfusion which I'm attempting to consume using c#.net.
I have a rather simple web-service which exposes data from sql server. It will
I have programmed in C, Pascal, GWBasic, TCL, Perl, Lisp, but Visual Basic is
I'm working on a textbased simple roleplaying game for my exame, but i have
I have programmed a very simple script with jQuery. The idea is to fade
I have not programmed in c++ in a long time and want some simple

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.