I have this sample hibernate project:
Main.java
Code:
package vaannila.student;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import vaannila.util.HibernateUtil;
public class Main {
public boolean insert(Student student ){
Student newRecord = new Student() ;
newRecord = student;
Transaction transaction = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
transaction = session.beginTransaction();
session.save(newRecord);
//session.saveOrUpdate ( newRecord );
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
return false;
} finally {
session.flush ( );
session.close ( );
}
return true;
}
public static void main(String[] args) {
Main uu = new Main ( );
Address address1 = new Address("ammsan ", "north-marka", "jordan", "00000000");
Student student1 = new Student("moha", address1);
uu.insert ( student1 );
}
}
Address.java
Code:
package vaannila.student;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "ADDRESS")
public class Address {
private long addressId;
private String street;
private String city;
private String state;
private String zipcode;
public Address() {
}
public Address(String street, String city, String state, String zipcode) {
this.street = street;
this.city = city;
this.state = state;
this.zipcode = zipcode;
}
@Id
@GeneratedValue
@Column(name = "ADDRESS_ID")
public long getAddressId() {
return this.addressId;
}
public void setAddressId(long addressId) {
this.addressId = addressId;
}
@Column(name = "ADDRESS_STREET", nullable = false, length=250)
public String getStreet() {
return this.street;
}
public void setStreet(String street) {
this.street = street;
}
@Column(name = "ADDRESS_CITY", nullable = false, length=50)
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
@Column(name = "ADDRESS_STATE", nullable = false, length=50)
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
@Column(name = "ADDRESS_ZIPCODE", nullable = false, length=10)
public String getZipcode() {
return this.zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
Student.java
Code:
package vaannila.student;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "STUDENT")
public class Student {
private long studentId;
private String studentName;
private Address studentAddress;
public Student() {
}
public Student(String studentName, Address studentAddress) {
this.studentName = studentName;
this.studentAddress = studentAddress;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "STUDENT_ID")
public long getStudentId() {
return this.studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
@Column(name = "STUDENT_NAME", nullable = false, length = 100)
public String getStudentName() {
return this.studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@OneToOne(cascade = CascadeType.ALL)
public Address getStudentAddress() {
return this.studentAddress;
}
public void setStudentAddress(Address studentAddress) {
this.studentAddress = studentAddress;
}
}
hibernate.cfg.xml
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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">*******</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="vaannila.student.Student" />
<mapping class="vaannila.student.Address" />
</session-factory>
</hibernate-configuration>
the issue is when i run the main class and invoke insert() it stores record in student, address table. this is correct
but when i run insert() again i expect to insert another record in the tables and the result should be 2 records.
but this did not work with me
and when i add 3 different objects and invoke:
insert(obj1);
insert(obj2);
insert(obj3);
it adds 3 new records in the tables once i removed the above statements and re- run the class it removes the added 3 records of obj1,obj2,obj3
i expect keeping the records i added in the tables not deleting them
I appreciate your reply