Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ManyToMany join table with objEntity. No cascade del #586

Open
wants to merge 2 commits into
base: STABLE-4.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
****************************************************************/

package org.apache.cayenne.access;

import org.apache.cayenne.Cayenne;
import org.apache.cayenne.PersistenceState;
import org.apache.cayenne.di.Inject;
import org.apache.cayenne.query.ObjectSelect;
import org.apache.cayenne.test.jdbc.DBHelper;
import org.apache.cayenne.test.jdbc.TableHelper;
import org.apache.cayenne.testdo.many_to_many_joinTable_objEntity.Enrollments;
import org.apache.cayenne.testdo.many_to_many_joinTable_objEntity.Student;
import org.apache.cayenne.unit.di.server.CayenneProjects;
import org.apache.cayenne.unit.di.server.ServerCase;
import org.apache.cayenne.unit.di.server.UseServerRuntime;
import org.junit.Before;
import org.junit.Test;

import java.util.Collection;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

@UseServerRuntime(CayenneProjects.MANY_TO_MANY_JOIN_TABLE_OBJ_ENTITY_PROJECT)
public class ManyToManyJoinTableWithObjEntityIT extends ServerCase {

@Inject
protected DataContext context;
@Inject
protected DBHelper dbHelper;
protected TableHelper tStudent;
protected TableHelper tCourse;
protected TableHelper tEnrollments;

@Before
public void setUp() throws Exception {
tStudent = new TableHelper(dbHelper, "student");
tStudent.setColumns("id");

tCourse = new TableHelper(dbHelper, "course");
tCourse.setColumns("id");

tEnrollments = new TableHelper(dbHelper, "enrollments");
tEnrollments.setColumns("student_id","course_id");

}

protected void createObjectsDataSet() throws Exception {
tStudent.insert(1 );

tCourse.insert(1);
tCourse.insert(2);
tCourse.insert(3);

tEnrollments.insert(1,1);
tEnrollments.insert(1,2);
tEnrollments.insert(1,3);

}


@Test
public void testDeleteObjects() throws Exception {
createObjectsDataSet();

assertEquals(1, tStudent.getRowCount());
assertEquals(1, tStudent.selectAll().size());
assertEquals(3, tEnrollments.getRowCount());
assertEquals(3, tEnrollments.selectAll().size());

Student student = Cayenne.objectForPK(context, Student.class, 1);

List<Enrollments> select = ObjectSelect.query(Enrollments.class).select(context);
assertEquals(3,select.size());

assertEquals(PersistenceState.COMMITTED, student.getPersistenceState());
context.deleteObject(student);

assertEquals(PersistenceState.DELETED, student.getPersistenceState());
context.commitChanges();

for (Enrollments enrollments : select) {
assertEquals(PersistenceState.TRANSIENT, enrollments.getPersistenceState());
assertNull(enrollments.getObjectContext());
}

assertEquals(PersistenceState.TRANSIENT, student.getPersistenceState());
assertNull(student.getObjectContext());

assertEquals(0, tStudent.getRowCount());
assertEquals(0, tStudent.selectAll().size());
assertEquals(0, tEnrollments.getRowCount());
assertEquals(0, tEnrollments.selectAll().size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.cayenne.ObjectId;
import org.apache.cayenne.PersistenceState;
Expand Down Expand Up @@ -119,6 +121,56 @@ public void mergeSameObjectsId_ReplacementId() {
assertThat(merged, not(hasItem(sameInstance(op[1]))));
}

@Test
public void mergeSameObjectIdCompoundId() {
Map<String, Object> keyMap1_1 = new LinkedHashMap<>();
keyMap1_1.put("student_id", 1);
keyMap1_1.put("course_id", 1);

Map<String, Object> keyMap1_2 = new LinkedHashMap<>();
keyMap1_2.put("student_id", 1);
keyMap1_2.put("course_id", 2);

Map<String, Object> keyMap1_3 = new LinkedHashMap<>();
keyMap1_3.put("student_id", 1);
keyMap1_3.put("course_id", 3);

ObjectId studentObjectId1 = ObjectId.of("student", "id", 1);

ObjectId enrollmentObjectId1_1 = ObjectId.of("enrollment", keyMap1_1);
enrollmentObjectId1_1.getReplacementIdMap().put("student_id",1);

ObjectId enrollmentObjectId1_2 = ObjectId.of("enrollment", keyMap1_2);
enrollmentObjectId1_2.getReplacementIdMap().put("student_id",1);

ObjectId enrollmentObjectId1_3 = ObjectId.of("enrollment", keyMap1_3);
enrollmentObjectId1_3.getReplacementIdMap().put("student_id",1);

DbEntity student = mockEntity("student");
DbEntity enrollment = mockEntity("enrollment");

List<DbRowOp> ops = new ArrayList<>();

ops.add(new DeleteDbRowOp(mockObject(studentObjectId1), student, studentObjectId1));
ops.add(new UpdateDbRowOp(mockObject(enrollmentObjectId1_1), enrollment, enrollmentObjectId1_1));
ops.add(new UpdateDbRowOp(mockObject(enrollmentObjectId1_3), enrollment, enrollmentObjectId1_3));
ops.add(new UpdateDbRowOp(mockObject(enrollmentObjectId1_2), enrollment, enrollmentObjectId1_2));

ops.add(new DeleteDbRowOp(mockObject(enrollmentObjectId1_3), enrollment, enrollmentObjectId1_3));
ops.add(new DeleteDbRowOp(mockObject(enrollmentObjectId1_2), enrollment, enrollmentObjectId1_2));
ops.add(new DeleteDbRowOp(mockObject(enrollmentObjectId1_1), enrollment, enrollmentObjectId1_1));


DefaultDataDomainFlushAction action = mock(DefaultDataDomainFlushAction.class);
when(action.mergeSameObjectIds((List<DbRowOp>) any(List.class))).thenCallRealMethod();

Collection<DbRowOp> merged = action.mergeSameObjectIds(ops);
assertEquals(4, merged.size());

merged.forEach(dbRowOp -> assertThat(dbRowOp, instanceOf(DeleteDbRowOp.class)));
}


@Test
public void createQueries() {
ObjectId id1 = ObjectId.of("test", "id", 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.apache.cayenne.testdo.many_to_many_joinTable_objEntity;

import org.apache.cayenne.testdo.many_to_many_joinTable_objEntity.auto._Course;

public class Course extends _Course {

private static final long serialVersionUID = 1L;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.apache.cayenne.testdo.many_to_many_joinTable_objEntity;

import org.apache.cayenne.testdo.many_to_many_joinTable_objEntity.auto._Enrollments;

public class Enrollments extends _Enrollments {

private static final long serialVersionUID = 1L;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.apache.cayenne.testdo.many_to_many_joinTable_objEntity;

import org.apache.cayenne.testdo.many_to_many_joinTable_objEntity.auto._Student;

public class Student extends _Student {

private static final long serialVersionUID = 1L;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.apache.cayenne.testdo.many_to_many_joinTable_objEntity.auto;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import org.apache.cayenne.BaseDataObject;
import org.apache.cayenne.exp.property.ListProperty;
import org.apache.cayenne.exp.property.PropertyFactory;
import org.apache.cayenne.testdo.many_to_many_joinTable_objEntity.Enrollments;

/**
* Class _Course was generated by Cayenne.
* It is probably a good idea to avoid changing this class manually,
* since it may be overwritten next time code is regenerated.
* If you need to make any customizations, please use subclass.
*/
public abstract class _Course extends BaseDataObject {

private static final long serialVersionUID = 1L;

public static final String ID_PK_COLUMN = "id";

public static final ListProperty<Enrollments> ENROLLMENTS = PropertyFactory.createList("enrollments", Enrollments.class);


protected Object enrollments;

public void addToEnrollments(Enrollments obj) {
addToManyTarget("enrollments", obj, true);
}

public void removeFromEnrollments(Enrollments obj) {
removeToManyTarget("enrollments", obj, true);
}

@SuppressWarnings("unchecked")
public List<Enrollments> getEnrollments() {
return (List<Enrollments>)readProperty("enrollments");
}

@Override
public Object readPropertyDirectly(String propName) {
if(propName == null) {
throw new IllegalArgumentException();
}

switch(propName) {
case "enrollments":
return this.enrollments;
default:
return super.readPropertyDirectly(propName);
}
}

@Override
public void writePropertyDirectly(String propName, Object val) {
if(propName == null) {
throw new IllegalArgumentException();
}

switch (propName) {
case "enrollments":
this.enrollments = val;
break;
default:
super.writePropertyDirectly(propName, val);
}
}

private void writeObject(ObjectOutputStream out) throws IOException {
writeSerialized(out);
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
readSerialized(in);
}

@Override
protected void writeState(ObjectOutputStream out) throws IOException {
super.writeState(out);
out.writeObject(this.enrollments);
}

@Override
protected void readState(ObjectInputStream in) throws IOException, ClassNotFoundException {
super.readState(in);
this.enrollments = in.readObject();
}

}
Loading