gpt4 book ai didi

java - 使用 Spring Data JPA 的模型库(继承)

转载 作者:行者123 更新时间:2023-12-02 00:23:19 24 4
gpt4 key购买 nike

我会为每个类/实体使用带有 ID 和时间戳的 ModelBase。但是当我使用Long时在 JPARepository<> 中键入主键界面我收到消息

Not an entity: class java.lang.Long

代码:

@MappedSuperclass
public class ModelBase implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(nullable = false, columnDefinition = "datetime")
private Date lastModified;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Date getLastModified() {
return lastModified;
}

public void setLastModified(Date lastModified) {
this.lastModified = lastModified;
}

@PreUpdate
@PrePersist
public void updateLastModified() {
lastModified = new Date();
}

}

从模型库继承的模型类

@Entity
@Table(name = "Name")
public class Name extends ModelBase implements Serializable {}

repo 协议(protocol)

public interface NameRepository extends JpaRepository<Long, Name>{}

我做错了什么?

谢谢

最佳答案

反了:

JpaRepository<Name, Long>

首先是实体,然后是 ID。检查 JPARepository javadoc。

关于java - 使用 Spring Data JPA 的模型库(继承),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10517994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com