-->
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: Hibernate_KeyGenerationStrategyNative
PostPosted: Wed Mar 30, 2011 8:26 am 
Newbie

Joined: Sat Dec 11, 2010 8:49 am
Posts: 19
Hello,

I am using Hibernate and in my bean i define key generation strategy as follows:
@org.hibernate.annotations.GenericGenerator(name="hibernate-native", strategy="native")
public class A implements java.io.Serializable, Cloneable {
@Id
@GeneratedValue(generator="hibernate-native")
private Integer id;
...
}

Bean B has set of beans A:
@OneToMany(targetEntity=A.class, fetch=FetchType.LAZY, mappedBy="blah")
@Cascade({ org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
private Set<A> profiles = new HashSet<A>();

Now when i insert new instance of A in set of profiles, and call merge on object B i got exception:
Provided id of the wrong type for class somepackage.A. Expected: class java.lang.Integer, got class org.hibernate.action.DelayedPostInsertIdentifier

Everything works fine when i set strategy="increment" and all data are persisted in database via cascade,
but i got these excpetion when using strategy="native" or strategy="identity".

I want to avoid using strategy="increment" beacuse it may be more than one user which will insert data
in database in the same time.

Any idea why i got that exception.

Best regards.


Top
 Profile  
 
 Post subject: Re: Hibernate_KeyGenerationStrategyNative
PostPosted: Wed Mar 30, 2011 10:15 am 
Newbie

Joined: Sat Dec 11, 2010 8:49 am
Posts: 19
In addition to previous post i got strange situation with persisting my object.
Say we have beans operator and operatorProfiles defined as follows:

@org.hibernate.annotations.GenericGenerator(name="hibernate-native", strategy="increment")
public class Operator implements java.io.Serializable, Cloneable {
@Id
@GeneratedValue(generator="hibernate-native")
private Integer id;

@OneToMany(targetEntity=OperatorProfile.class, fetch=FetchType.LAZY, mappedBy="operator")
@Cascade({ org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
private Set<Operatorprofile> profiles = new HashSet<Operatorprofile>();
...
}

@org.hibernate.annotations.GenericGenerator(name="hibernate-native", strategy="increment")
public class Operatorprofile implements java.io.Serializable, Cloneable {
@Id
@GeneratedValue(generator="hibernate-native")
private Integer id;

@ManyToOne(targetEntity=Subscriber.class, fetch=FetchType.LAZY)
@Cascade({ org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.MERGE })
@JoinColumn(name="OperatorID", nullable=false)
private Operator operator;
...
}

Everything worked as expected until i manually inserted row in operatorProfile table with ID 0!
Now every time when i try to add new profile to operator (add bean to profiles set and merge operator)
i got my previous profile overwritten with new one! I checked hibernate logs and hibernate indeed
call update on existing profile although i added new profile.
But if i add new profile to operator which doesn't have that profile with id 0 in his set, everything is
persisted properly (not overwritten) and hibernate calls insert on profile instead update.

I am very curious why this happened? Is there any chance that hibernate key generate strategy INCREMENT
don't work if there is some row with primary key 0, manually inserted?


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.