Skip to content

Commit 716db5d

Browse files
Sannebrmeyer
authored andcommitted
HHH-8611 DelayedPostInsertIdentifier needs to implement Comparable
1 parent 7df8873 commit 716db5d

File tree

5 files changed

+200
-7
lines changed

5 files changed

+200
-7
lines changed

hibernate-core/src/main/java/org/hibernate/action/internal/DelayedPostInsertIdentifier.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package org.hibernate.action.internal;
2525

2626
import java.io.Serializable;
27+
import java.util.concurrent.atomic.AtomicLong;
2728

2829
/**
2930
* Acts as a stand-in for an entity identifier which is supposed to be
@@ -36,22 +37,29 @@
3637
* the entity instance or returned to the client...
3738
*
3839
* @author Steve Ebersole
40+
* @author Sanne Grinovero
3941
*/
40-
public class DelayedPostInsertIdentifier implements Serializable {
41-
private static long sequence;
42+
public class DelayedPostInsertIdentifier implements Serializable, Comparable<DelayedPostInsertIdentifier> {
43+
44+
private static final AtomicLong sequence = new AtomicLong( 0 );
4245

4346
private final long identifier;
4447

4548
/**
4649
* Constructs a DelayedPostInsertIdentifier
4750
*/
4851
public DelayedPostInsertIdentifier() {
49-
synchronized( DelayedPostInsertIdentifier.class ) {
50-
if ( sequence == Long.MAX_VALUE ) {
51-
sequence = 0;
52+
long value = sequence.incrementAndGet();
53+
if ( value < 0 ) {
54+
synchronized ( sequence ) {
55+
value = sequence.incrementAndGet();
56+
if ( value < 0 ) {
57+
sequence.set( 0 );
58+
value = 0;
59+
}
5260
}
53-
this.identifier = sequence++;
5461
}
62+
this.identifier = value;
5563
}
5664

5765
@Override
@@ -76,4 +84,17 @@ public String toString() {
7684
return "<delayed:" + identifier + ">";
7785

7886
}
87+
88+
@Override
89+
public int compareTo(DelayedPostInsertIdentifier that) {
90+
if ( this.identifier < that.identifier ) {
91+
return -1;
92+
}
93+
else if ( this.identifier > that.identifier ) {
94+
return 1;
95+
}
96+
else {
97+
return 0;
98+
}
99+
}
79100
}

hibernate-core/src/main/java/org/hibernate/action/internal/EntityIdentityInsertAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ protected EntityKey getEntityKey() {
236236
return entityKey != null ? entityKey : delayedEntityKey;
237237
}
238238

239-
private static synchronized DelayedPostInsertIdentifier generateDelayedPostInsertIdentifier() {
239+
private static DelayedPostInsertIdentifier generateDelayedPostInsertIdentifier() {
240240
return new DelayedPostInsertIdentifier();
241241
}
242242

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat, Inc. and/or its affiliates or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat, Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
25+
package org.hibernate.id;
26+
27+
import org.hibernate.Session;
28+
import org.hibernate.Transaction;
29+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
30+
import org.junit.Test;
31+
import org.hibernate.testing.TestForIssue;
32+
33+
@TestForIssue(jiraKey = "HHH-8611")
34+
public class FlushIdGenTest extends BaseCoreFunctionalTestCase {
35+
36+
@Test
37+
public void testPersistBeforeTransaction() {
38+
Session session = openSession();
39+
RootEntity ent1_0 = new RootEntity();
40+
RootEntity ent1_1 = new RootEntity();
41+
42+
session.persist( ent1_0 );
43+
session.persist( ent1_1 );
44+
45+
Transaction tx = session.beginTransaction();
46+
tx.commit(); //flush
47+
}
48+
49+
@Override
50+
public Class[] getAnnotatedClasses() {
51+
return new Class[]{
52+
RootEntity.class,
53+
RelatedEntity.class,
54+
};
55+
}
56+
57+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat, Inc. and/or its affiliates or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat, Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
25+
package org.hibernate.id;
26+
27+
import javax.persistence.Column;
28+
import javax.persistence.GeneratedValue;
29+
import javax.persistence.Id;
30+
31+
@javax.persistence.Entity
32+
@javax.persistence.Table(name = "entity2")
33+
public class RelatedEntity {
34+
35+
@Id
36+
@GeneratedValue
37+
@Column(name = "universalid")// "uid" is a keywork in Oracle
38+
private long uid;
39+
40+
@javax.persistence.ManyToOne
41+
private RootEntity linkedRoot;
42+
43+
public long getUid() {
44+
return uid;
45+
}
46+
public void setUid(long uid) {
47+
this.uid = uid;
48+
}
49+
50+
public void setLinkedRoot(RootEntity linkedRoot) {
51+
this.linkedRoot = linkedRoot;
52+
}
53+
public RootEntity getLinkedRoot() {
54+
return linkedRoot;
55+
}
56+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2013, Red Hat, Inc. and/or its affiliates or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat, Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
25+
package org.hibernate.id;
26+
27+
import java.io.Serializable;
28+
29+
import javax.persistence.Column;
30+
import javax.persistence.GeneratedValue;
31+
import javax.persistence.GenerationType;
32+
import javax.persistence.Id;
33+
34+
@javax.persistence.Entity
35+
public class RootEntity implements Serializable {
36+
37+
@Id
38+
@GeneratedValue(strategy= GenerationType.IDENTITY)
39+
@Column(name = "universalid")// "uid" is a keywork in Oracle
40+
private long uid;
41+
42+
@javax.persistence.OneToMany(mappedBy = "linkedRoot")
43+
private java.util.List<RelatedEntity> linkedEntities = new java.util.ArrayList<RelatedEntity>();
44+
45+
public long getUid() {
46+
return uid;
47+
}
48+
public void setUid(long uid) {
49+
this.uid = uid;
50+
}
51+
52+
public void setLinkedEntities(java.util.List<RelatedEntity> linkedEntities) {
53+
this.linkedEntities = linkedEntities;
54+
}
55+
public java.util.List<RelatedEntity> getLinkedEntities() {
56+
return linkedEntities;
57+
}
58+
59+
}

0 commit comments

Comments
 (0)