Please explain me, whats the mistake I have committed for the below code:
Code:
<?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">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate2</property>
<property name="hibernate.connection.username">root</property>
<property name="connection.password">password</property>
<property name="connection.pool_size">10</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.cybage.main.Speaker"/>
<mapping class="com.cybage.main.Location"/>
<mapping class="com.cybage.main.Event"/>
</session-factory>
</hibernate-configuration>
Code:
package com.cybage.main;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="SPEAKER")
public class Speaker {
private int speakerId;
private String speakerName;
private Event event;
public Speaker() {
}
public Speaker(int speakerId , String speakerName) {
this.speakerId = speakerId;
this.speakerName = speakerName;
}
@Id
@GeneratedValue
@Column(name="SPEAKER_ID")
public int getSpeakerId() {
return speakerId;
}
public void setSpeakerId(int speakerId) {
this.speakerId = speakerId;
}
@Column(name="SPEAKER_NAME")
public String getSpeakerName() {
return speakerName;
}
public void setSpeakerName(String speakerName) {
this.speakerName = speakerName;
}
@ManyToOne(cascade=CascadeType.ALL , fetch=FetchType.LAZY )
@JoinColumn(name="Event_Id", referencedColumnName="Event_ID")
public Event getEvent() {
return event;
}
public void setEvent(Event event) {
this.event = event;
}
}
Code:
package com.cybage.main;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="LOCATION")
public class Location {
private int locationId;
private String locationName;
private Set<Event> events;
@Id
@GeneratedValue
@Column(name="LOCATION_ID")
public int getLocationId() {
return locationId;
}
public void setLocationId(int locationId) {
this.locationId = locationId;
}
@Column(name="LOCATION_NAME")
public String getLocationName() {
return locationName;
}
public void setLocationName(String locationName) {
this.locationName = locationName;
}
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(joinColumns=@JoinColumn(name="LOC_ID") , inverseJoinColumns=@JoinColumn(name="EVE_ID"))
public Set<Event> getEvents() {
return events;
}
public void setEvents(Set<Event> events) {
this.events = events;
}
}
Code:
package com.cybage.main;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="EVENT")
public class Event {
private int eventId;
private String eventName;
private Set<Speaker> speakers;
private Set<Location> locations;
@Id
@GeneratedValue
@Column(name="EVENT_ID")
public int getEventId() {
return eventId;
}
public void setEventId(int eventId) {
this.eventId = eventId;
}
@Column(name="EVENT_NAME")
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
@OneToMany(mappedBy="event" )
public Set<Speaker> getSpeakers() {
return speakers;
}
public void setSpeakers(Set<Speaker> speakers) {
this.speakers = speakers;
}
@ManyToMany(cascade=CascadeType.ALL , mappedBy="events")
@JoinTable(joinColumns=@JoinColumn(name="EVE_ID") , inverseJoinColumns=@JoinColumn(name="LOC_ID"))
public Set<Location> getLocations() {
return locations;
}
public void setLocations(Set<Location> locations) {
this.locations = locations;
}
}
Code:
package com.cybage.main;
import java.util.LinkedHashSet;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
public class Main {
static Session session;
public static void main(String[] args) {
try {
Set<Speaker> speakers = new LinkedHashSet<Speaker>();
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Event event = new Event();
event.setEventId(101);
event.setEventName("EVENT1");
Speaker speaker1 = new Speaker();
speaker1.setSpeakerId(10);
speaker1.setSpeakerName("Harishwar");
speaker1.setEvent(event);
Speaker speaker2 = new Speaker();
speaker2.setSpeakerId(20);
speaker2.setSpeakerName("Shiva");
// speaker2.setEvent(event);
speakers.add(speaker1);
speakers.add(speaker2);
event.setSpeakers(speakers);
/*speaker1.setEvent(event);
speaker2.setEvent(event);*/
/*Event event2 = new Event();
event2.setEventId(102);
event2.setEventName("EVENT2");*/
// if inverse = true otehrwise comment these 4 sentences
Set<Event> events = new LinkedHashSet<Event>();
events.add(event);
// events.add(event2);
// till here
Set<Location> locations = new LinkedHashSet<Location>();
Location location = new Location();
location.setLocationId(1);
location.setLocationName("Hyderabad");
location.setEvents(events);
Location location2 = new Location();
location2.setLocationId(2);
location2.setLocationName("Vizag");
location2.setEvents(events);
locations.add(location);
locations.add(location2);
event.setLocations(locations);
// event2.setLocations(locations);
/*session.save(location);
session.save(location2);*/
/*session.save(speaker1);
session.save(speaker2);*/
session.save(event);
transaction.commit();
} catch (HibernateException e) {
System.out.println(".................SOME PROBLEM OCCURED..............");
e.printStackTrace();
} finally {
if(session != null)
session.close();
}
}
}
Code:
and here is the exception, Im getting:
INFO: schema export complete
Hibernate: insert into EVENT (EVENT_NAME) values (?)
Hibernate: update LOCATION set LOCATION_NAME=? where LOCATION_ID=?
Hibernate: update LOCATION set LOCATION_NAME=? where LOCATION_ID=?
Sep 21, 2010 5:36:17 PM org.hibernate.jdbc.BatchingBatcher doExecuteBatch
SEVERE: Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0;
expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions
(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.cybage.main.Main.main(Main.java:85)
Sep 21, 2010 5:36:17 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0;
expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions
(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.cybage.main.Main.main(Main.java:85)
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0;
expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions
(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.cybage.main.Main.main(Main.java:85)
.................SOME PROBLEM OCCURED..............
Now please Tell me whats wrong I have written the code and provide me the solution for it.. and also I would like
to know, when it executes the "session.save(event);" so in which order it generates the SQL statements..??
Waiting for your replies...
Harishwar