-->
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: Problem with the method save()
PostPosted: Wed Mar 24, 2010 3:03 am 
Newbie

Joined: Wed Mar 24, 2010 2:58 am
Posts: 10
Location: Livorno,Italy
Hi, i'm trying to use for the first time hibernate for the java persistence, i've tryed to map one simple pojo, i've created some object and i saved it on my mysql database with the command save().
I have a problem: if i use the program in a second time, changing the data of the object, the old data is overwrite with the new, why?
thank you

p.s. sorry for the bad english, i'm italian :D

This is the code
Code:
public static void main(String args[]){
       // Session session=HibernateUtil.getSessionFactory().getCurrentSession();
        SessionFactory sessionFactory = new Configuration().configure() .buildSessionFactory();
        Auto a=new Auto(4,"renault","19",1991);
        Auto b=new Auto(5,"fiat","punto ",1999);
        Auto c=new Auto(6,"fiat","barchetta ",2000);
        ArrayList<Auto> au=new ArrayList<Auto>();
        au.add(b);
        au.add(a);
        au.add(c);
        Iterator<Auto> it;
        it=au.iterator();
        Session session;
        while(it.hasNext()){
            session =sessionFactory.openSession();
            session.beginTransaction();
        session.save(it.next());
        //session.flush();
        session.getTransaction().commit();
        }


Top
 Profile  
 
 Post subject: Re: Problem with the method save()
PostPosted: Wed Mar 24, 2010 4:06 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Can you post your Auto class as well as the configuration?


Top
 Profile  
 
 Post subject: Re: Problem with the method save()
PostPosted: Wed Mar 24, 2010 8:45 am 
Newbie

Joined: Wed Mar 24, 2010 2:58 am
Posts: 10
Location: Livorno,Italy
This is the class:

Code:
public class Auto {

    private int id;
    private String marca;
    private String modello;
    private int anno;
    private ArrayList<Parte> componenti;

    public Auto () {
    }

    public Auto (int id,String marca,String modello,int anno) {

        this.id=id;
        this.anno=anno;
        this.marca=marca;
        this.modello=modello;

    }

    public int getAnno () {
        return anno;
    }

    public void setAnno (int val) {
        this.anno = val;
    }

   
    public ArrayList<Parte> getComponenti () {
        return componenti;
    }

public void setComponenti() (ArrayList<Parte> val) {
        this.componenti = val;
    }

   
    public String getMarca () {
        return marca;
    }

    public void setMarca (String val) {
        this.marca = val;
    }

    public String getModello () {
        return modello;
    }

    public void setModello (String val) {
        this.modello = val;
    }
}


And this is the configuration file:
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/Catalogo</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">123</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.hbm2ddl.auto">create</property>
    <mapping resource="Auto.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Thank you


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.