Skip to content

Commit 9dad717

Browse files
committed
Issue 1711: ClassCastException while updating foreignkeys with identity generation
Signed-off-by: Will Dazey <[email protected]>
1 parent fd9e4ec commit 9dad717

File tree

4 files changed

+97
-5
lines changed

4 files changed

+97
-5
lines changed

foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/queries/CallQueryMechanism.java

-4
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ protected void configureDatabaseCall(DatabaseCall call) {
162162
call.setIsNativeConnectionRequired(true);
163163
}
164164

165-
if (this.query.isInsertObjectQuery()) {
166-
call.setShouldReturnGeneratedKeys(this.query.shouldReturnGeneratedKeys());
167-
}
168-
169165
if (this.query.isReadQuery()) {
170166
ReadQuery readQuery = (ReadQuery)this.query;
171167
// Some DB don't support FirstRow in SELECT statements in spite of supporting MaxResults(Symfoware).

foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/queries/StatementQueryMechanism.java

+17
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import org.eclipse.persistence.exceptions.DatabaseException;
2222
import org.eclipse.persistence.exceptions.QueryException;
2323
import org.eclipse.persistence.expressions.Expression;
24+
import org.eclipse.persistence.internal.databaseaccess.DatabaseCall;
2425
import org.eclipse.persistence.internal.databaseaccess.DatasourceCall;
2526
import org.eclipse.persistence.internal.expressions.SQLModifyStatement;
2627
import org.eclipse.persistence.internal.expressions.SQLStatement;
28+
import org.eclipse.persistence.internal.expressions.SQLUpdateStatement;
2729
import org.eclipse.persistence.internal.helper.DatabaseField;
30+
import org.eclipse.persistence.queries.Call;
2831
import org.eclipse.persistence.queries.DatabaseQuery;
2932
import org.eclipse.persistence.sessions.SessionProfiler;
3033
/**
@@ -93,6 +96,20 @@ public DatabaseQueryMechanism clone(DatabaseQuery queryClone) {
9396
return clone;
9497
}
9598

99+
@Override
100+
protected void configureDatabaseCall(DatabaseCall call) {
101+
// ReturnGeneratedKeys is only applicable for insert queries
102+
if (this.query.isInsertObjectQuery()) {
103+
if(!(this.sqlStatement instanceof SQLUpdateStatement)) {
104+
// Some InsertQuerys spawn UpdateStatements that execute within the Insert scope
105+
// ReturnGeneratedKeys is not applicable for UpdateStatements
106+
call.setShouldReturnGeneratedKeys(this.query.shouldReturnGeneratedKeys());
107+
}
108+
}
109+
110+
super.configureDatabaseCall(call);
111+
}
112+
96113
/**
97114
* INTERNAL:
98115
* delete the object

jpa/eclipselink.jpa.test.jse/src/it/java/org/eclipse/persistence/jpa/test/sequence/TestIdentityGeneration.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.eclipse.persistence.jpa.test.framework.EmfRunner;
2828
import org.eclipse.persistence.jpa.test.framework.Property;
2929
import org.eclipse.persistence.jpa.test.sequence.model.Coffee;
30+
import org.eclipse.persistence.jpa.test.sequence.model.StepExecutionEntity;
3031
import org.eclipse.persistence.jpa.test.sequence.model.Tea;
3132
import org.eclipse.persistence.jpa.test.sequence.model.TeaShop;
3233
import org.eclipse.persistence.platform.database.DatabasePlatform;
@@ -37,7 +38,7 @@
3738

3839
@RunWith(EmfRunner.class)
3940
public class TestIdentityGeneration {
40-
@Emf(createTables = DDLGen.DROP_CREATE, classes = { Coffee.class, Tea.class, TeaShop.class },
41+
@Emf(createTables = DDLGen.DROP_CREATE, classes = { Coffee.class, Tea.class, TeaShop.class, StepExecutionEntity.class },
4142
properties = {
4243
@Property(name="eclipselink.logging.level", value="FINE")})
4344
private EntityManagerFactory emf;
@@ -95,6 +96,34 @@ public void testPersistWithSecondaryTables() {
9596
}
9697
}
9798

99+
/**
100+
* Test to cover updating foreign key field after insert.
101+
*
102+
* https://github.com/eclipse-ee4j/eclipselink/issues/1711
103+
*/
104+
@Test
105+
public void testPersistWithSecondaryTables2() {
106+
if (emf == null)
107+
return;
108+
109+
EntityManager em = emf.createEntityManager();
110+
try {
111+
StepExecutionEntity e1 = new StepExecutionEntity();
112+
e1.setChildEntity(e1);
113+
114+
em.getTransaction().begin();
115+
em.persist(e1);
116+
em.getTransaction().commit();
117+
} finally {
118+
if (em.getTransaction().isActive()) {
119+
em.getTransaction().rollback();
120+
}
121+
if(em.isOpen()) {
122+
em.close();
123+
}
124+
}
125+
}
126+
98127
@Test
99128
public void testRefreshWithTriggers() {
100129
if (emf == null)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
// Contributors:
14+
// dminsky - initial implementation
15+
package org.eclipse.persistence.jpa.test.sequence.model;
16+
17+
import jakarta.persistence.Entity;
18+
import jakarta.persistence.GeneratedValue;
19+
import jakarta.persistence.GenerationType;
20+
import jakarta.persistence.Id;
21+
import jakarta.persistence.JoinColumn;
22+
import jakarta.persistence.ManyToOne;
23+
24+
@Entity
25+
public class StepExecutionEntity {
26+
27+
@Id
28+
@GeneratedValue(strategy = GenerationType.IDENTITY)
29+
private long id;
30+
31+
@ManyToOne
32+
@JoinColumn(name = "FK_ID")
33+
private StepExecutionEntity childEntity;
34+
35+
public long getId() {
36+
return id;
37+
}
38+
39+
public void setId(long id) {
40+
this.id = id;
41+
}
42+
43+
public StepExecutionEntity getChildEntity() {
44+
return childEntity;
45+
}
46+
47+
public void setChildEntity(StepExecutionEntity childEntity) {
48+
this.childEntity = childEntity;
49+
}
50+
}

0 commit comments

Comments
 (0)