Skip to content

Commit

Permalink
Inverse relationship type calculation (#3172) (#3173)
Browse files Browse the repository at this point in the history
* Inverse relationship type calculation (#3172)

* Sytle fixes.

---------

Co-authored-by: Gavin Matthews <gmatthews@axway.com>
  • Loading branch information
bladedancer and gmatthewsAxway committed Mar 23, 2024
1 parent f93582d commit 61e9ded
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,8 @@ public String getRelationInverse(Type<?> cls, String relation) {
String inverseRelationName = inverseMapping.getKey();
String inverseMappedBy = inverseMapping.getValue();

if (relation.equals(inverseMappedBy)
&& getParameterizedType(inverseType, inverseRelationName).equals(clsBinding.entityClass)) {
if (relation.equals(inverseMappedBy) && getParameterizedType(inverseType, inverseRelationName)
.isAssignableFrom(clsBinding.entityClass)) {
return inverseRelationName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ public void testInMemoryDataStore() {
"example.PrimitiveId",
"example.Publisher",
"example.Right",
"example.Righter",
"example.StringId",
"example.UpdateAndCreate",
"example.User",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import example.Price;
import example.Publisher;
import example.Right;
import example.Righter;
import example.StringId;
import example.User;
import example.models.generics.Employee;
Expand Down Expand Up @@ -116,6 +117,7 @@ private void init() {
bindEntity(User.class);
bindEntity(Left.class);
bindEntity(Right.class);
bindEntity(Righter.class);
bindEntity(StringId.class);
bindEntity(Friend.class);
bindEntity(FieldAnnotations.class);
Expand Down Expand Up @@ -489,6 +491,22 @@ public void testGetInverseRelationshipOwnedSide() {
"The inverse relationship of children should be parents");
}

@Test
public void testGetInverseRelationshipOnPolymorphicBaseType() {
assertEquals(
"one2many",
getRelationInverse(ClassType.of(Right.class), "many2one"),
"The inverse relationship of one2many should be many2one");
}

@Test
public void testGetInverseRelationshipOnPolymorphicDerivedType() {
assertEquals(
"one2many",
getRelationInverse(ClassType.of(Righter.class), "many2one"),
"The inverse relationship of one2many should be many2one");
}

@Test
public void testComputedAttributeIsExposed() {
List<String> attributes = getAttributes(ClassType.of(User.class));
Expand Down Expand Up @@ -1097,7 +1115,7 @@ public void testGetBoundByVersion() {
assertTrue(models.contains(ClassType.of(BookV2.class)));

models = getBoundClassesByVersion(NO_VERSION);
assertEquals(21, models.size());
assertEquals(22, models.size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ public void testGetAllClasses() {
@Test
public void testGetAnnotatedClasses() {
Set<Class<?>> classes = scanner.getAnnotatedClasses("example", Include.class);
assertEquals(32, classes.size(), "Actual: " + classes);
assertEquals(33, classes.size(), "Actual: " + classes);
classes.forEach(cls -> assertTrue(cls.isAnnotationPresent(Include.class)));
}

@Test
public void testGetAllAnnotatedClasses() {
Set<Class<?>> classes = scanner.getAnnotatedClasses(Include.class);
assertEquals(44, classes.size(), "Actual: " + classes);
assertEquals(45, classes.size(), "Actual: " + classes);
classes.forEach(cls -> assertTrue(cls.isAnnotationPresent(Include.class)));
}

@Test
public void testGetAnyAnnotatedClasses() {
Set<Class<?>> classes = scanner.getAnnotatedClasses(Include.class, Entity.class);
assertEquals(55, classes.size());
assertEquals(56, classes.size());
for (Class<?> cls : classes) {
assertTrue(cls.isAnnotationPresent(Include.class)
|| cls.isAnnotationPresent(Entity.class));
Expand Down
6 changes: 6 additions & 0 deletions elide-core/src/test/java/example/Right.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
import com.fasterxml.jackson.annotation.JsonIgnore;

import jakarta.persistence.CascadeType;
import jakarta.persistence.DiscriminatorColumn;
import jakarta.persistence.DiscriminatorType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
Expand All @@ -26,6 +30,8 @@
@UpdatePermission(expression = "Prefab.Role.None")
@Entity
@Table(name = "xright") // right is SQL keyword
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public class Right {
@JsonIgnore
private long id;
Expand Down
28 changes: 28 additions & 0 deletions elide-core/src/test/java/example/Righter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2015, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package example;

import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.UpdatePermission;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;


@Include(name = "righter")
@UpdatePermission(expression = "Prefab.Role.None")
@Entity
@DiscriminatorValue("righter")
public class Righter extends Right {
private String moreRight;

public String getMoreRight() {
return moreRight;
}

public void setMoreRight(String moreRight) {
this.moreRight = moreRight;
}
}

0 comments on commit 61e9ded

Please sign in to comment.