• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with optimistic Locking

 
vishesh Chaudhary
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am new to the Java Ranch.

I have started learning with hibernate and I have stucked with the optimistic locking , below I have mention the code like---


1.Here is my pojo class-

package com.org.dto;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;

import org.hibernate.annotations.GenericGenerator;

@Entity
public class User {

@Id
private int userId;

@Version
private int version;

private String userName;

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

}




2.Here is My main class---

package com.org.controller;

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

import com.org.dto.User;

public class UserController {

public static void main(String a[]) {

try {

@SuppressWarnings("deprecation")
SessionFactory sessionFactory = new AnnotationConfiguration()
.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();

User user = new User();
user.setUserId(0);
user.setUserName("Ankit");
session.save(user);
session.flush();
session.getTransaction().commit();
Session session1 = sessionFactory.openSession();
session1.beginTransaction();
User use = (User) session1.get(User.class, new Integer(0));
use.setUserId(1);
use.setUserName("Shubham");
session1.saveOrUpdate(use);
session1.getTransaction().commit();
System.out.println(use.getUserName());
session.close();
session1.close();

} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}

}
}

While doing this operation I am getting this exception like--


Hibernate:
drop table if exists User
Hibernate:
create table User (
userId integer not null,
userName varchar(255),
version integer not null,
primary key (userId)
)
Hibernate:
/* insert com.org.dto.User
*/ insert
into
User
(userName, version, userId)
values
(?, ?, ?)
Hibernate:
/* load com.org.dto.User */ select
user0_.userId as userId0_0_,
user0_.userName as userName0_0_,
user0_.version as version0_0_
from
User user0_
where
user0_.userId=?
identifier of an instance of com.org.dto.User was altered from 0 to 1
org.hibernate.HibernateException: identifier of an instance of com.org.dto.User was altered from 0 to 1
at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:194)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:156)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:227)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1212)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:400)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at com.org.controller.UserController.main(UserController.java:33)

Please Advice !!

Sorry if I would have done any mistake while posting this because I am new to the code ranch.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic