-->
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.  [ 11 posts ] 
Author Message
 Post subject: Problem when save object, I have double object ?
PostPosted: Tue Feb 24, 2009 7:28 am 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
Hello,

Here is my problem :
I have an object that I save using Hibernate.
I create another object and inject the same data (id, etc... I annoted my id to
@Id
@GeneratedValue(strategy = GenerationType.AUTO)

I do not know if this replae the id that I specifiy when I create the new object ?

But the fact is that I override the equals method like this :
@Override
public boolean equals(Object obj) {
if (obj instanceof Dossier) {
Dossier obDossier = (Dossier)obj;
return getId().equals(obDossier.getId());
}
return false;
}

And it appears that when I save another object is created. How can I do to say that this two instance are the same to save one time in data base ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 7:36 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Can you post your example code?

Hibernate uses the Id to see if an object is new (transient) or just detached. So if you copy your object including the id-value, a merge() will result in an update of the "old" object's database-row.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 7:38 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Second time instead of save call saveOrUpdate

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 7:46 am 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
In fact, I do not understand :

I have my object, for example : Dossier
I have a model interface IDossier :
I have a implementation of this interface : DossierImpl

DossierImpl is like this :

DossierImpl implements IDossier {
...
private Dossier dossier;
...

And I update dossier data using my DossierImpl object.

I used this in the MVC context with a graphic interface.

So when I save my object, I used the interface method save();


IDossier dossier = new DossierImpl();
dossier.save();

And this method is like this :
Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
session.saveOrUpdate(dossier);
transaction.commit();
session.close();

When I exit and exucute again my application, I get my Dossier object and I inject it into a IDossier model :

public static IDossier createDossier(Dossier dossier) {
IDossier dosBean = new DossierImpl();
And I used the setter to put the same data that this dossier to the DossierImpl. So I have one dossier (argument) and another in DossierImpl().

In fact, I maybe I just need to create a DossierImpl(Dossier dossier) constructor...?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 7:51 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I do not really understand your problem, I guess. Do you mean you have a problem, because you have two different Java-objects, and want them to be the same ("==" should be true)? If yes, why?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 8:26 am 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
The method equals is not called... ? I put a log in and it never pass in.
And hibernate use when I save.
select nextval ('hibernate_sequence')


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 8:33 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Louevie wrote:
When I exit and exucute again my application, I get my Dossier object and I inject it into a IDossier model

Can you post the code which gets and inject the Dossier object?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 8:55 am 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
Ok this is my code...


A method to translate...
Code:
// This class aims all the method to transform a IObject to Object or the // reverse
public class BeanFactoryFromModel {
public static IDossier createDossier(Dossier dossier) {
      IDossier dosBean = new DossierImpl(enumTypeVehicule);
      if (null != dossier) {
         dosBean.setId(dossier.getId());         
      }
      
      return dosBean;
   }
}

public class DossierImpl implements IDossier {
   
   private Dossier dossier;
   
   
   public DossierImpl() {
      dossier = new Dossier();
   }

   public DossierImpl(IDossier dossier) {
      this.dossier = BeanFactoryFromModel.createDossier(dossier);
   }


   @Override
   public int getId() {
      return dossier.getId();
   }

        public void save() {
      System.out.println("Save Dossier");
      Session session = HibernateUtil.getSession();
      Transaction transaction = session.beginTransaction();
      session.saveOrUpdate(dossier);
      transaction.commit();
      session.close();
   }
...
}


public interface IDossier l {
        public void save();
   public int getId();
        public void setId(int id);
}

// This is the method called when I want to create a IDossier
public class Util {
public static IDossier createDossierVehiculeAgricole () {
      IDossier dossier = new DossierImpl();
      saveDossier(dossier);
      createAllDirectoryForDossier(dossier);
      return dossier;
   }
...
}

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 12:17 pm 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Here I am a bit confused nw.

public static IDossier createDossier(Dossier dossier) { ... }

public DossierImpl(IDossier dossier) {
this.dossier = BeanFactoryFromModel.createDossier(dossier);
}

So Dossier also implements IDossier ?
I thot Dossier is ur entity class.

Can u give the exact code which causes the creation of a new object where you expect update?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 12:54 pm 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
In fact, no. I have a IDossier that is an interface for my model (MVC).
DossierImpl is the implementation that contains my Dossier object that is related to Hibernate :

Code:

@Entity
public class Dossier implements Serializable {
...

   @CollectionOfElements
   @JoinTable(name = "dossier_document_join", joinColumns = @JoinColumn(name = "file"))   
   @Cascade(value = CascadeType.ALL)
   private List<Document> documents;

....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 12:32 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Then what about this code?
Code:
public static IDossier createDossier(Dossier dossier) { ... }

public DossierImpl(IDossier dossier) {
this.dossier = BeanFactoryFromModel.createDossier(dossier);
}


Is it that the BeanFactoryFromModel has an overloaded createDossier method?

Also plz give the exact code which causes the creation of a new object where you expect update?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.