Skip to content

Commit

Permalink
Issue 1711: ClassCastException while updating foreignkeys with identi…
Browse files Browse the repository at this point in the history
…ty generation

Signed-off-by: Will Dazey <[email protected]>
  • Loading branch information
dazey3 committed Sep 26, 2022
1 parent fd9e4ec commit 9dad717
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ protected void configureDatabaseCall(DatabaseCall call) {
call.setIsNativeConnectionRequired(true);
}

if (this.query.isInsertObjectQuery()) {
call.setShouldReturnGeneratedKeys(this.query.shouldReturnGeneratedKeys());
}

if (this.query.isReadQuery()) {
ReadQuery readQuery = (ReadQuery)this.query;
// Some DB don't support FirstRow in SELECT statements in spite of supporting MaxResults(Symfoware).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.exceptions.QueryException;
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.internal.databaseaccess.DatabaseCall;
import org.eclipse.persistence.internal.databaseaccess.DatasourceCall;
import org.eclipse.persistence.internal.expressions.SQLModifyStatement;
import org.eclipse.persistence.internal.expressions.SQLStatement;
import org.eclipse.persistence.internal.expressions.SQLUpdateStatement;
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.queries.Call;
import org.eclipse.persistence.queries.DatabaseQuery;
import org.eclipse.persistence.sessions.SessionProfiler;
/**
Expand Down Expand Up @@ -93,6 +96,20 @@ public DatabaseQueryMechanism clone(DatabaseQuery queryClone) {
return clone;
}

@Override
protected void configureDatabaseCall(DatabaseCall call) {
// ReturnGeneratedKeys is only applicable for insert queries
if (this.query.isInsertObjectQuery()) {
if(!(this.sqlStatement instanceof SQLUpdateStatement)) {
// Some InsertQuerys spawn UpdateStatements that execute within the Insert scope
// ReturnGeneratedKeys is not applicable for UpdateStatements
call.setShouldReturnGeneratedKeys(this.query.shouldReturnGeneratedKeys());
}
}

super.configureDatabaseCall(call);
}

/**
* INTERNAL:
* delete the object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.persistence.jpa.test.framework.EmfRunner;
import org.eclipse.persistence.jpa.test.framework.Property;
import org.eclipse.persistence.jpa.test.sequence.model.Coffee;
import org.eclipse.persistence.jpa.test.sequence.model.StepExecutionEntity;
import org.eclipse.persistence.jpa.test.sequence.model.Tea;
import org.eclipse.persistence.jpa.test.sequence.model.TeaShop;
import org.eclipse.persistence.platform.database.DatabasePlatform;
Expand All @@ -37,7 +38,7 @@

@RunWith(EmfRunner.class)
public class TestIdentityGeneration {
@Emf(createTables = DDLGen.DROP_CREATE, classes = { Coffee.class, Tea.class, TeaShop.class },
@Emf(createTables = DDLGen.DROP_CREATE, classes = { Coffee.class, Tea.class, TeaShop.class, StepExecutionEntity.class },
properties = {
@Property(name="eclipselink.logging.level", value="FINE")})
private EntityManagerFactory emf;
Expand Down Expand Up @@ -95,6 +96,34 @@ public void testPersistWithSecondaryTables() {
}
}

/**
* Test to cover updating foreign key field after insert.
*
* https://github.com/eclipse-ee4j/eclipselink/issues/1711
*/
@Test
public void testPersistWithSecondaryTables2() {
if (emf == null)
return;

EntityManager em = emf.createEntityManager();
try {
StepExecutionEntity e1 = new StepExecutionEntity();
e1.setChildEntity(e1);

em.getTransaction().begin();
em.persist(e1);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if(em.isOpen()) {
em.close();
}
}
}

@Test
public void testRefreshWithTriggers() {
if (emf == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

// Contributors:
// dminsky - initial implementation
package org.eclipse.persistence.jpa.test.sequence.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

@Entity
public class StepExecutionEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@ManyToOne
@JoinColumn(name = "FK_ID")
private StepExecutionEntity childEntity;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public StepExecutionEntity getChildEntity() {
return childEntity;
}

public void setChildEntity(StepExecutionEntity childEntity) {
this.childEntity = childEntity;
}
}

0 comments on commit 9dad717

Please sign in to comment.