Skip to content

Commit

Permalink
problems with session customizer when upgrading to 2.7.9 - bugfix + u…
Browse files Browse the repository at this point in the history
…nit test (eclipse-ee4j#1364)

Standalone usage ReturnUpdate annotation was broken in PR eclipse-ee4j#1016.
fixes eclipse-ee4j#1363

Signed-off-by: Radek Felcman <radek.felcman@oracle.com>
(cherry picked from commit fb1cc69)
  • Loading branch information
rfelcman committed Nov 25, 2021
1 parent 434e53f commit df80cb6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -837,12 +837,12 @@ protected SQLUpdateStatement buildUpdateStatement(DatabaseTable table) {
if (getDescriptor().hasReturningPolicies() && getDescriptor().getReturnFieldsToGenerateUpdate() != null) {
// In case of RelationalDescriptor only return fields for current table must be used.
List<DatabaseField> returnFieldsForTable = new ArrayList<>();
for (DatabaseField item: getDescriptor().getReturnFieldsToGenerateInsert()) {
for (DatabaseField item: getDescriptor().getReturnFieldsToGenerateUpdate()) {
if (table.equals(item.getTable())) {
returnFieldsForTable.add(item);
}
if (!returnFieldsForTable.isEmpty()) {
updateStatement.setReturnFields(getDescriptor().getReturnFieldsToGenerateInsert());
updateStatement.setReturnFields(getDescriptor().getReturnFieldsToGenerateUpdate());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.fail;

/**
* TestSuite to test entities, that has a @ReturnInsert and @ReturnUpdate annotations.
Expand Down Expand Up @@ -107,7 +108,9 @@ public void setup() {
" COL4 VARCHAR (15) NOT NULL," +
" COL4_VIRTUAL VARCHAR (100) AS ( COL4 || '_col4' ) VIRTUAL," +
" COL5 VARCHAR (15) NOT NULL," +
" COL5_VIRTUAL VARCHAR (100) AS ( COL5 || '_col5' ) VIRTUAL)");
" COL5_VIRTUAL VARCHAR (100) AS ( COL5 || '_col5' ) VIRTUAL," +
" COL6 VARCHAR (15) NOT NULL," +
" COL6_VIRTUAL VARCHAR (100) AS ( COL6 || '_col6' ) VIRTUAL)");
session.executeNonSelectingSQL("ALTER TABLE JPA22_RETURNINSERT_DETAIL ADD CONSTRAINT PKJPA22_RETURNINSERT_DETAIL PRIMARY KEY ( ID_VIRTUAL, ID, COL1, COL2 )");
session.executeNonSelectingSQL("ALTER TABLE JPA22_RETURNINSERT_DETAIL ADD CONSTRAINT FKJPA22_RETURNINSERT_MASTER_DETAIL FOREIGN KEY ( ID_VIRTUAL, ID, COL1 ) REFERENCES JPA22_RETURNINSERT_MASTER ( ID_VIRTUAL, ID, COL1 ) NOT DEFERRABLE");
session.executeNonSelectingSQL("CREATE TABLE JPA22_RETURNINSERT_MASTER_JOINED (" +
Expand Down Expand Up @@ -143,6 +146,8 @@ private void testCreate() {
returnInsertDetail = insertReturnInsertDetail(em, returnInsertMaster);

em.getTransaction().commit();
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand Down Expand Up @@ -171,6 +176,8 @@ private void testCreateJoined() {
returnInsertDetailJoined = em.merge(returnInsertDetailJoined);

em.getTransaction().commit();
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand All @@ -196,6 +203,8 @@ private void testUpdateMasterDetailJoined() {
returnInsertDetailJoined = em.merge(returnInsertDetailJoined);

em.getTransaction().commit();
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand All @@ -220,6 +229,8 @@ private void testUpdateDetailJoined() {
returnInsertDetailJoined = em.merge(returnInsertDetailJoined);

em.getTransaction().commit();
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand Down Expand Up @@ -252,9 +263,12 @@ private void testFindUpdate() {
assertEquals("abc_col2", returnInsertDetailFindResult.getReturnInsertDetailEmbedded().getCol2Virtual());
//Test update
returnInsertDetailFindResult.getReturnInsertDetailEmbedded().setCol3("ijk");
returnInsertDetailFindResult.setCol6("rst");
returnInsertDetailMerge = em.merge(returnInsertDetailFindResult);

em.getTransaction().commit();
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand All @@ -264,6 +278,7 @@ private void testFindUpdate() {
}
}
assertEquals("ijk_col3", returnInsertDetailMerge.getReturnInsertDetailEmbedded().getCol3Virtual());
assertEquals("rst_col6", returnInsertDetailMerge.getCol6Virtual());
}

private void testQuery() {
Expand All @@ -278,6 +293,8 @@ private void testQuery() {
Query query = em.createQuery("select t from ReturnInsertDetail t where t.id = :returnInsertDetailId");
query.setParameter("returnInsertDetailId", returnInsertDetailPK);
returnInsertDetailQueryResult = (ReturnInsertDetail) query.getSingleResult();
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
Expand Down Expand Up @@ -311,7 +328,9 @@ private ReturnInsertMasterPK createReturnInsertMasterPK() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = dateFormat.parse("1970-01-01 00:00:00.0");
ReturnInsertMasterPK.setId(date);
} catch (Exception e) { }
} catch (Exception e) {
fail(e.getMessage());
}
ReturnInsertMasterPK.setCol1(1L);
return ReturnInsertMasterPK;
}
Expand All @@ -338,6 +357,8 @@ private ReturnInsertDetail insertReturnInsertDetail(EntityManager em, ReturnInse
//Inherited field
returnInsertDetail.setCol4("opq");

returnInsertDetail.setCol6("rst");

em.persist(returnInsertDetail);
return returnInsertDetail;
}
Expand All @@ -349,7 +370,9 @@ private ReturnInsertDetailPK createReturnInsertDetailPK() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = dateFormat.parse("1970-01-01 00:00:00.0");
returnInsertDetailPK.setId(date);
} catch (Exception e) { }
} catch (Exception e) {
fail(e.getMessage());
}
returnInsertDetailPK.setCol1(1L);
returnInsertDetailPK.setCol2("abc");
return returnInsertDetailPK;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021 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
Expand All @@ -17,6 +17,8 @@
import org.eclipse.persistence.annotations.ReturnInsert;

import jakarta.persistence.*;
import org.eclipse.persistence.annotations.ReturnUpdate;

import java.io.Serializable;

/**
Expand All @@ -43,6 +45,13 @@ public class ReturnInsertDetail extends ReturnInsertDetailParent implements Seri
@Embedded
private ReturnInsertDetailEmbedded returnInsertDetailEmbedded;

@Column(name = "COL6")
private String col6;

@ReturnUpdate
@Column(name = "COL6_VIRTUAL", insertable = false)
private String col6Virtual;

@OneToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST })
@JoinColumns({
@JoinColumn(name = "ID_VIRTUAL", referencedColumnName = "ID_VIRTUAL", insertable = false, updatable = false),
Expand Down Expand Up @@ -78,6 +87,22 @@ public void setReturnInsertDetailEmbedded(ReturnInsertDetailEmbedded returnInser
this.returnInsertDetailEmbedded = returnInsertDetailEmbedded;
}

public String getCol6() {
return col6;
}

public void setCol6(String col6) {
this.col6 = col6;
}

public String getCol6Virtual() {
return col6Virtual;
}

public void setCol6Virtual(String col6Virtual) {
this.col6Virtual = col6Virtual;
}

public ReturnInsertMaster getReturnInsertMaster() {
return returnInsertMaster;
}
Expand Down

0 comments on commit df80cb6

Please sign in to comment.