spring-projects / spring-projects/spring-data-rest

Allow support for method-based annotation instead of field only.

Open
#54 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

In my urge to show progress, I "hacked" JpaEntityMetada class so that it can find method based JPA annotations. (In my project, It would be a lot of domain classes to change to support field based JPA annotations, plus some refactor)

I added an if statement inside the for loop around the entityType.getAttributes() - line 42

for(Attribute attr : entityType.getAttributes()) {
  boolean exported = true;
  String memberName = attr.getJavaMember().getName();                // starts here:
  if(StringUtils.startsWithIgnoreCase(memberName, "get"))
  {
     memberName = memberName.substring(3);
     String firstChar = memberName.substring(0,1).toLowerCase();
     memberName = firstChar.concat(memberName.substring(1));
  }
  Field field = ReflectionUtils.findField(type, memberName)  //fix ends here

This works, however, I discovered that @oneToMany relationships do not map to resource links, instead collection resources are being "rendered" inside the owning resource.

BTW, if dependency injection was used instead of "newing" JpaEntityMetada I could have injected my own subclass avoiding the need to modify framework code. (Sorry I don't mean to be rude, just giving some feedback).

So, I'm guessing there is more work to do, besides the hack above. Right ?

In any case, I understand this area is being re-worked. I'm wondering if method based JPA annotations will be supported, or if I have to budget time to solve this on my own.

Thank you very much,

Paulo Avelar

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with JpaEntityMetada, especially the loop over entityType.getAttributes() and the ReflectionUtils.findField call described around line 42. Trace how method-based JPA annotations are detected and how @OneToMany relationships become resource links; done means method-based annotations are supported without rendering collection resources inside the owning resource.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.