I am trying to run sample Hibernate example. I have created to Objects "event" and "contact" in Event.java and Contact.java respectively with the client code and hibernate mapping file, configuration as below -
Code:
package com.pal.data;
public class Event {
private long id;
private String title;
private String venue;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getVenue() {
return venue;
}
public void setVenue(String venue) {
this.venue = venue;
}
}
Code:
package com.pal.data;
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;
}
}
Configuration and mapping -
Code:
<?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.pal.data.Event" table="event">
<id name="id" column="id" type="long">
<generator class="assigned" />
</id>
<property name="title">
<column name="eventtitle"></column>
</property>
<property name="venue">
<column name="eventvenue"></column>
</property>
</class>
</hibernate-mapping>
Code:
<?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.pal.data.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>
</hibernate-mapping>
Code:
<?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/EventsInfo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="event.hbm.xml"/>
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
And client code creating session and persisting object to DB is:-
Code:
package com.pal.dao.impl;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import com.pal.data.Contact;
import com.pal.data.Event;
public class DAOClient {
Session session=null;
public DAOClient() {
session = new Configuration().configure().buildSessionFactory().openSession();
Event evt = new Event();
evt.setId(1);
evt.setTitle("event1");
evt.setVenue("mumbai");
// session.save(evt);
Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Pras");
contact.setLastName("Tiwari");
contact.setEmail("pras@yahoo.com");
session.save(contact);
session.flush();
session.close();
}
public static void main(String[] args) {
DAOClient dao= new DAOClient();
}
}
My Problem is that whenever I try to save contact Object to DB using
Code:
session.save(contact);
in above client code, it works fine and I can see my contact object persisted in DB. But when I try to save event Object through
Code:
session.save(evt);
in Client class above, I am not able to see Event Object in DB. Also I can't see any error in console while storing event Object.
Both Event and Contact Objects are almost of same structure and DB schema for both object is in place. Still I am only able to persist Contact Object in DB and not an Event Object. Why so?
Let me know if you need console log also for both scenarios.
many Thanks in Advance.
regards,
Pras