-->
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.  [ 2 posts ] 
Author Message
 Post subject: save() does not throws an HibernateException (Object exists)
PostPosted: Tue Mar 31, 2009 8:08 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi,

it may be a very simple question, but have no idea left what's wrong.

First of all, I want to ask the user, if he (or she) wants to override an already stored object.

So I assumed that , when I call save and the object already exits, I would get an HibernateException.
So I can catch this HibernateException, ask the user and call saveOrUpdate if needed.


I can see, In the debug messages, that save is cancelling the requested action, but does not throws an HibernateException.
Code:
13:55:53,781 DEBUG DefaultSaveOrUpdateEventListener:99 - ignoring persistent instance
126281 [AWT-EventQueue-0] DEBUG org.hibernate.event.def.DefaultSaveOrUpdateEventListener  - ignoring persistent instance
13:55:53,796 DEBUG DefaultSaveOrUpdateEventListener:136 - object already associated with session: [de.zew.vitax.modell.country.Laendermodell#component[comment,name,type,year]{name=BARBADOS, year=2009, type=CURRENT_LAW, comment=}]
126296 [AWT-EventQueue-0] DEBUG org.hibernate.event.def.DefaultSaveOrUpdateEventListener  - object already associated with session: [de.zew.vitax.modell.country.Laendermodell#component[comment,name,type,year]{name=BARBADOS, year=2009, type=CURRENT_LAW, comment=}]


But why doesn't it throws an exception?

In a book I had read that it depends on the way the ID's are generated (by hibernate or not), if an exception is thrown or not.

Normally I use generated keys but in this case, I had to use my own key:

So how can I achieve this behaviour? Do I have to change a config parameter of hibernate? Or is there another way?

Thanks a lot in advance.

Greetings Michael


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 02, 2009 11:07 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi,

if the object was loaded before with the same session, I can avoid this problem with s.contains(item). (s == org.hibernate.Session)

Hibernate.isInitialized() may help if it was loaded outside the current session. But I'm not sure about this.

If the object wasn't loaded at all before, but is still in the database. I could try to load it before.

But there I'm facing two problems:
1) I normally don't expose the ID and I want to use a general function with Object as Argument, so I can't use
"Object get(Class clazz, Serializable id)" , cause of the lack of the id.

getIdentifier(Object object) works only if the object was loaded by Hibernate before.

A way around this problem would be to expose the ID by using an interface which would replace the Object as argument for my function, but this isn't my preferred solution at the moment. Cause it makes it possible to manipulate the id accidentally.

Ok, I could avoid this by returning new instances with thegiven value, instead of returning the id-object. (setID is not provided.) But is this good practice?

But this may be the only one and the effort isn't too big.

Also I have a compound naturally key, which I would have to generate for the above solution.

Code:
@Embeddable
public class LaendermodellPK implements Serializable {

    @Enumerated(value = EnumType.STRING)
    private KnownCountries name;

    @Basic
    private int year;

    @Enumerated(value = EnumType.STRING)
    private Variations type;

    @Lob
    private String comment;

    private LaendermodellPK() {

    }

    @Override
    public boolean equals(final Object obj) {
   ....
    }


    @Override
    public int hashCode() {
   ...
    }
}

Code:
@Entity
@IdClass(LaendermodellPK.class)
public class Laendermodell implements Comparable<Laendermodell> {

    @Version
    @Column(name = "VERSION_OPTLOCK")
    @SuppressWarnings("unused")
    private long version;

    @Enumerated(value = EnumType.STRING)
    @Id
    private KnownCountries name;

    @Id
    private int year;

    @Enumerated(value = EnumType.STRING)
    @Id
    private Variations type;

 
    @Id
    @Lob
    private String comment;

    ...
}


So I would have to add an new constructor and implement an method like this

Code:
LaendermodellPK(final KnownCountries name, final int year, final Variations type, final String comment) {
   this.name = name;
   this.year = year;
   this.type = type;
   this.comment = comment;
    }




Code:
public interface SupportsDatabaseID {
    public Serializable getId();
}




Code:

public Serializable getId() {
   return new LaendermodellPK(this.name, this.year, this.type, this.comment);
    }



Is this good pratice?




2) How could I check for difference between the two objects behind equals and key identity?
In other words: is the current object dirty?
So I can decide If I have to ask the user or not.

I don't want to do this by hand, but I haven't found an appropriate function yet. I don't want to update the data in any case, so megre, update and saveOrUpdate aren't helpful here.


Thanks a lot.

Greetings Michael


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