Skip to content

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

Closed

Description

@huhu72

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);
}

Activity

  1. added
    for: eclipse
    for: vscode
    and removed on Nov 3, 2023
  2. added this to the 4.21.0.RELEASE milestone on Nov 3, 2023
  3. BoykoAlex commented on Nov 3, 2023

    @BoykoAlex
    Contributor

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

  4. BoykoAlex commented on Nov 3, 2023

    @BoykoAlex
    Contributor

    Should be fixed with b1e1bf7

  5. chalikir commented on Dec 14, 2023

    @chalikir

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

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

  6. BoykoAlex commented on Dec 14, 2023

    @BoykoAlex
    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?

  7. 2 remaining items

  8. chalikir commented on Dec 15, 2023

    @chalikir
  9. SajidhKareem commented on Jan 7, 2024

    @SajidhKareem

    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.

  10. BoykoAlex commented on Jan 7, 2024

    @BoykoAlex
    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.

  11. neo7BF commented on Feb 7, 2024

    @neo7BF

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

  12. BoykoAlex commented on Feb 7, 2024

    @BoykoAlex
    Contributor

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

  13. neo7BF commented on Feb 21, 2024

    @neo7BF

    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;
        }
    }
    
  14. BoykoAlex commented on Feb 21, 2024

    @BoykoAlex
    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?

  15. neo7BF commented on Feb 23, 2024

    @neo7BF

    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 !

  16. BoykoAlex commented on Feb 23, 2024

    @BoykoAlex
    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.

  17. WeiMao33 commented on Apr 2, 2024

    @WeiMao33

    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
  18. BoykoAlex commented on Apr 2, 2024

    @BoykoAlex
    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.

  19. WeiMao33 commented on Apr 3, 2024

    @WeiMao33

    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);
      }
    }
  20. BoykoAlex commented on Apr 3, 2024

    @BoykoAlex
    Contributor

    Last comment extracted in #1220

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

Metadata

Metadata

Assignees

Labels

for: eclipsefor: vscodetheme: validationtype: bug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions