我正在使用JPA和Spring Data映射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);
}
}
常规类Task,它扩展了 BaseClass
@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);
}
}
TaskView 用于JPA查询投影:
public interface TaskView {
Long getId();
String getUuid();
String getTitle();
String getDescription();
LocalDateTime getCreatedAt();
}
和JpaRepository接口:
interface TasksCrudRepository extends JpaRepository<Task, Long> {
@EntityGraph(value = "Task.detail", type = EntityGraphType.LOAD)
List<Task> findAll();
List<TaskView> findAllProjectedBy();
}
TaskCrudRepository 中的最后一个方法 findAllProjectedBy()-导致在此帖子开头粘贴该异常。
当我从getId()
中删除TaskView
方法时,它开始了,但是我无法在投影中显示id
中的Task
。
所以问题是我在整个类结构中缺少什么?
我正在使用:
答案 0 :(得分:0)
在定义ID字段时,BaseEntity
中有一个错字。应该是驼色id
,而不是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);
}
}