当前位置: 首页 > 知识库问答 >
问题:

无法定位具有给定名称的属性-Spring Data JPA Projections、Hibernate和BaseEntity

黄凌龙
2023-03-14

我正在用JPA和Spring数据映射Hibernate中的实体,当我运行应用程序时,我得到

Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [id] on this ManagedType [p.s.t..entity.BaseEntity]
    at org.hibernate.metamodel.internal.AbstractManagedType.checkNotNull(AbstractManagedType.java:128) ~[hibernate-core-5.3.12.Final.jar:5.3.12.Final]
    at org.hibernate.metamodel.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:113) ~[hibernate-core-5.3.12.Final.jar:5.3.12.Final]
    at org.hibernate.metamodel.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:111) ~[hibernate-core-5.3.12.Final.jar:5.3.12.Final]
    at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:633) ~[spring-data-jpa-2.1.11.RELEASE.jar:2.1.11.RELEASE]
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.complete(JpaQueryCreator.java:175) ~[spring-data-jpa-2.1.11.RELEASE.jar:2.1.11.RELEASE]

我有一个超类baseEntity:

@MappedSuperclass
@Getter
@Setter
public abstract class BaseEntity implements Serializable {

    @Id
    @GeneratedValue
    private Long Id;

    private String uuid = UUID.randomUUID().toString();

    @Override
    public boolean equals(Object that) {
        return this == that ||
            that instanceof BaseEntity && Objects.equals(uuid, ((BaseEntity) that).uuid);
    }

    @Override
    public int hashCode() {
        return Objects.hash(uuid);
    }
}
@Getter
@Setter
@Table(name = "task")
@Entity
@NoArgsConstructor
@NamedEntityGraph(
    name = "Task.detail",
    attributeNodes = {
        @NamedAttributeNode("attachments"),
        @NamedAttributeNode("tags")
    }
)
public class Task extends BaseEntity {
    private String title;
    private String description;
    private LocalDateTime createdAt;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "task_id")
    private Set<Attachment> attachments = new HashSet<>();

    @ManyToMany
    @JoinTable(
        name = "tags_tasks",
        joinColumns = @JoinColumn(name = "task_id"),
        inverseJoinColumns = @JoinColumn(name = "tag_id")
    )
    private Set<Tag> tags = new HashSet<>();

    public Task(String title, String description, LocalDateTime createdAt) {
        this.title = title;
        this.description = description;
        this.createdAt = createdAt;
    }

    public void addAttachment(String filename, String comment) {
        attachments.add(new Attachment(filename, comment));
    }

    public Set<Attachment> getAttachments() {
        return attachments;
    }

    public void addTag(Tag tag) {
        tags.add(tag);
    }

    public void removeTag(Tag tag) {
        tags.remove(tag);
    }
}

public interface TaskView {
    Long getId();
    String getUuid();
    String getTitle();
    String getDescription();
    LocalDateTime getCreatedAt();
}

interface TasksCrudRepository extends JpaRepository<Task, Long> {
    @EntityGraph(value = "Task.detail", type = EntityGraphType.LOAD)
    List<Task> findAll();

    List<TaskView> findAllProjectedBy();
}
  • Spring Boot 2.1.9.发布
  • Java 11
  • Hibernate Core 5.3.12.final
  • JPA 2.2

共有1个答案

郑锦
2023-03-14

定义ID字段时,baseEntity中有一个错误。应该是camelcaseID而不是ID

@MappedSuperclass
@Getter
@Setter
public abstract class BaseEntity implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    private String uuid = UUID.randomUUID().toString();

    @Override
    public boolean equals(Object that) {
        return this == that ||
            that instanceof BaseEntity && Objects.equals(uuid, ((BaseEntity) that).uuid);
    }

    @Override
    public int hashCode() {
        return Objects.hash(uuid);
    }
}
 类似资料: