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

Issue with Join on Hierarchical Entities #134

Open
Pedrocaz opened this issue Oct 19, 2022 · 1 comment
Open

Issue with Join on Hierarchical Entities #134

Pedrocaz opened this issue Oct 19, 2022 · 1 comment

Comments

@Pedrocaz
Copy link

Pedrocaz commented Oct 19, 2022

Hello,

I have an hierarchical chain of 3 objects:

Abstract class "A" (MappedSuperclass) <|-- Abstract class "B" (@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)) <|-- Concrete Class "C"

Concrete class C has an One to One relationship with class D and Object D an One to One relationship with class E

I'm doing an Interface that extends Specification< B >

I do a join on D (looking from perspective of C, I think it's implicit).

When I add another join on D.E it fails with :
"Unable to locate Attribute with the the given name [attribute name of D on C class] on this ManagedType [class A]; nested exception is java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [attribute name of D on C class] on this ManagedType [class A]"

When I add another level (D.E) it fails to downcast and starts looking D on A.

Any idea on this?
Should I use Criteria API with treat to down cast?
Is something wrong in my logic?

Thank you in advance.

@klesniak1
Copy link
Contributor

klesniak1 commented Nov 21, 2022

Hi @Pedrocaz,

The provided information are not sufficient to determine is described behaviour is a bug or not.

I've tried to reproduce your logic and it looks that everything works fine. Please take a look and let me know if there is any difference between my implementation and yours.

public class HierarchicalTest extends E2eTestBase {

	@MappedSuperclass
	public abstract class A {

		@Id
		@GeneratedValue
		Long id;

		String note;

		public A(String note) {
			this.note = note;
		}

		public A() {

		}

		public Long getId() {
			return id;
		}

		public String getNote() {
			return note;
		}
	}

	@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
	@Entity
	public abstract class B extends A {
		public B(String note) {
			super(note);
		}

		public B() {
			super("default");
		}
	}

	@Entity
	public class C extends A {

		@OneToOne
		D d;

		public C(String note, D d) {
			super(note);
			this.d = d;
		}

		public C() {

		}

		public D getD() {
			return d;
		}
	}

	@Entity
	public class D {

		@Id
		@GeneratedValue
		Long id;

		@OneToOne
		E e;

		public D(E e) {
			this.e = e;
		}

		public D() {

		}

		public E getE() {
			return e;
		}
	}

	@Entity
	public class E {

		@Id
		@GeneratedValue
		Long id;

		String comment;

		public E(String comment) {
			this.comment = comment;
		}

		public E() {

		}

		public Long getId() {
			return id;
		}

		public String getComment() {
			return comment;
		}
	}

	@Controller
	public static class TestController {

		@Autowired
		CRepository cRepository;

		@RequestMapping(value = "/test", params = { "comment" })
		@ResponseBody
		public Object findByNote(
				@Join(path = "d", alias = "d")
				@Join(path = "d.e", alias = "e")
				@Spec(path = "e.comment", params = {"comment"}, spec = Equal.class) Specification<B> spec) {
			return cRepository.findAll(spec);
		}
	}

	@Test
	public void findsByNote() throws Exception {
		E e = new E("aaa");
		D d = new D(e);
		C c = new C("test", d);
		em.persist(e);
		em.persist(d);
		em.persist(c);
		mockMvc.perform(get("/test")
				.param("comment", "aaa")
				.accept(MediaType.APPLICATION_JSON))
				.andExpect(status().isOk())
				.andExpect(jsonPath("$").isArray())
				.andExpect(jsonPath("$[?(@.d.e.comment=='aaa')]").exists())
				.andReturn();

	}

}
public interface CRepository extends PagingAndSortingRepository<HierarchicalTest.C, Long>, JpaSpecificationExecutor<HierarchicalTest.B> {
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants