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

Cant use compsite keys for domain id when extending JpaRepository #1139

Closed
huhu72 opened this issue Nov 3, 2023 · 19 comments
Closed

Cant use compsite keys for domain id when extending JpaRepository #1139

huhu72 opened this issue Nov 3, 2023 · 19 comments
Assignees
Labels
for: eclipse something that is specific for Eclipse for: vscode something that is specific for VSCode theme: validation type: bug

Comments

@huhu72
Copy link

huhu72 commented Nov 3, 2023

Describe the bug
For Spring Boot Tools extension V1.50.0 on vscode)
When creating a interface that extends JpaRepository, it will throw an error saying "Expected Domain ID type is 'java.lang.String'vscode-spring-boot(DOMAIN_ID_FOR_REPOSITORY)". When I switch back to v1.49.0 the error is no longer there.
bug

To Reproduce

  1. Create an entity with a composite key
  2. Create a repository interface that extends JpaRepository<T,ID>

Sample

package sample.entities;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "customer_account_relationship")
@IdClass(CustomerAccountId.class)
public class CustomerAccountRel implements Serializable, ErrorLoggable {

  @Id
  @NotNull
  @Size(max = 20)
  @Column(name = "acct_nbr", length = 20)
  private String acct_nbr;

  @Id
  @Size(max = 50)
  @Column(name = "customer_id", length = 50)
  private String customerId;

  @NotNull
  @Size(max = 5)
  @Column(name = "relation_code", length = 5)
  private String relation_code;

  public void setAcct_nbr(String acct_nbr) {
    this.acct_nbr = StringUtils.truncate(acct_nbr, 20);
  }

  public void setCustomerId(String customerId) {
    this.customerId = StringUtils.truncate(customerId, 50);
  }

  public void setRelation_code(String relation_code) {
    this.relation_code = StringUtils.truncate(relation_code, 5);
  }

  @Override
  public String getId() {
    return customerId + " - " + acct_nbr;
  }

}
package sample.entities;

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CustomerAccountId implements Serializable {

  private String acct_nbr;
  private String customerId;
}
package sample.repositories;

import sample.entities.CustomerAccountId;
import sample.entities.CustomerAccountRel;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CustomerAccountRelRepository
    extends JpaRepository<CustomerAccountRel, CustomerAccountId> {

  public List<CustomerAccountRel> findByCustomerId(String customerId);
}
@BoykoAlex
Copy link
Contributor

@huhu72 thanks for reporting this. While we are fixing the issue the workaround would be turning this validation off in the preferences/settings.

@BoykoAlex
Copy link
Contributor

Should be fixed with b1e1bf7

@chalikir
Copy link

chalikir commented Dec 14, 2023

latest version of Spring STS plugin for eclipse is still having this issue.

it gives error 'Expected Domain ID type is 'long''

@BoykoAlex
Copy link
Contributor

Is the code snippet to reproduce the issue still the same? If not, do you mind pasting the java code snippets for which you still observe the issue?

@chalikir
Copy link

chalikir commented Dec 14, 2023 via email

@BoykoAlex
Copy link
Contributor

@chalikir hmm... i wouldn't think that changing boot version could help with reconciling. The reconciling in this case isn't based on a boot version on the classpath... It seems though as i made a mistake in the fix to this issue. Someone else complained about the same issue here #1159. I have pushed a fix and would be great if you could try the snapshot distro build or nightly update site (see https://cdn.spring.io/spring-tools/snapshot/STS4/nightly-distributions.html)

@chalikir
Copy link

chalikir commented Dec 15, 2023 via email

@SajidhKareem
Copy link

Is this issue fixed? Right now I'm having this issue in my version of STS. Would like to know if a newer version has arrived with a fix.

@BoykoAlex
Copy link
Contributor

@SajidhKareem All fixes were put into STS 4.21.0 if this is the version you're using and still seeing issues then please raise a new issue for it.

As a workaround you can disable the validation for entity keys... it is somewhat experimental.
Go to Settings -> Spring -> Validation -> Boot 2.x Best Practices page. Find "Invalid Domain ID Type for Spring Data Repository" and switch it to "IGNORE" using the drop down.

@neo7BF
Copy link

neo7BF commented Feb 7, 2024

I just downloaded sts but this problem continues to occur.
image

@BoykoAlex
Copy link
Contributor

BoykoAlex commented Feb 7, 2024

@neo7BF Can you paste your NotificatioEntity in here please? You can simply disable this validation in the preferences as a workaround (Spring -> Validations)

@neo7BF
Copy link

neo7BF commented Feb 21, 2024

This is the entity:

@Entity
@Table(name = "notification")
@Data
public class NotificationEntity implements Serializable {

    @Id
    @NotNull
    @NotBlank
    @Size(max = 255)
    @Column(name = "notification_k", length = 255)
    private String key;

    @NotNull
    @Column(name = "sent_at")
    private OffsetDateTime sentAt;

    @Size(max = 255)
    @Column(name = "error_message", length = 255)
    private String errorMessage;

    public NotificationEntity() {
    }

    public NotificationEntity(@NotNull @NotBlank @Size(max = 255) String key) {
        this.key = key;
    }
}

@BoykoAlex
Copy link
Contributor

If your id is String in the NotificationEntity then I'd expect NotificationRepository extends JPARepository<Notification, String> rather than JPARepository<Notification, Long> - i think Long is underlined as expected... Or am I missing something?

@neo7BF
Copy link

neo7BF commented Feb 23, 2024

Yes correct. I was misled by the fact that I had downloaded sources
and I had worked on it in another editor and it didn't highlight it to me as a compilation error as happens in STS. Assuming that the source was correct because it compiled from there, I thought it was a problem with STS, but instead it was a lack of the other editor.
I am sorry !

@BoykoAlex
Copy link
Contributor

@neo7BF no problem at all! This validation is very flaky unfortunately. Don't be shy to bring up any issues with this validation in the future even if they don't turn out to be issues.

@WeiMao33
Copy link

WeiMao33 commented Apr 2, 2024

Sorry, I can't find the Boot 2.x Best Practices page. this is my screen shot.
ENV:
eclipse Version: 2023-12 (4.30.0)

image

@BoykoAlex
Copy link
Contributor

Screenshot 2024-04-02 at 11 14 47
If this validation gives you issues please paste your domain class and repository implementations with the short description of the issue.

@WeiMao33
Copy link

WeiMao33 commented Apr 3, 2024

thanks @BoykoAlex
here are the codes.
image

Spring Tool Suite 4 
Version: 4.22.0.RELEASE
Build Id: 202403071427
Revision: 44291b464093d161f115d8e16059e5272ed3c81d
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

@Setter
@Getter
@EqualsAndHashCode
@MappedSuperclass
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder(toBuilder = true)
public abstract class BaseEntity {

  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
}
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import jakarta.persistence.EntityManager;

public abstract class RdsJpaRepository<T extends BaseEntity, ID>
    extends SimpleJpaRepository<T, ID> {

  public RdsJpaRepository(Class<T> domainClass, EntityManager em) {
    super(domainClass, em);
  }

  public RdsJpaRepository(
      JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
    super(entityInformation, entityManager);
  }
}

@BoykoAlex
Copy link
Contributor

Last comment extracted in #1220

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: eclipse something that is specific for Eclipse for: vscode something that is specific for VSCode theme: validation type: bug
Projects
None yet
Development

No branches or pull requests

7 participants