These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: save() does not do what i expect
PostPosted: Fri Dec 17, 2010 1:09 am 
Newbie

Joined: Thu Dec 16, 2010 2:40 pm
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: save() does not do what i expect
PostPosted: Fri Dec 17, 2010 3:04 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
mhmdawwad,

You have to learn the configurations first. See the cfg.xml file.

Quote:
<property name="hbm2ddl.auto">create</property>


Comment the above one. You will get it right. The above statement used to create tables using hbm files. I understand this codes are downloaded from third party sites.

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
 Post subject: Re: save() does not do what i expect
PostPosted: Fri Dec 17, 2010 3:44 am 
Newbie

Joined: Thu Dec 16, 2010 2:40 pm
Posts: 3
thank you sooo much i appreciate your help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.