4 回答

TA貢獻1816條經驗 獲得超6個贊
您必須更改存儲庫中 ID 類型參數的類型,以匹配實體上的 id 屬性類型。
來自 Spring 文檔:
Interface Repository<T,ID>
Type Parameters:
T - the domain type the repository manages
ID - the type of the id of the entity the repository manages
基于
@Entity // This tells Hibernate to make a table out of this class
@Table(name = "users")
public class XmppUser {
@Id
private java.lang.String username;
//...
}
它應該是
public interface UserRepository extends CrudRepository<XmppUser, String> {
//..
}

TA貢獻1909條經驗 獲得超7個贊
我認為有一種方法可以解決這個問題。
比方說,Site 是我們的@Entity。
@Id private String id; getters setters
然后你可以調用 findById 如下
Optional<Site> site = getSite(id);
注意:這對我有用,我希望它能幫助別人。

TA貢獻1784條經驗 獲得超2個贊
你可以嘗試這樣的事情:
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "PR_KEY")
private String prKey;

TA貢獻1820條經驗 獲得超9個贊
JpaRepository 是 CrudRepository 的特例。JpaRepository 和 CrudRepository 都聲明了兩個類型參數,T 和 ID。您將需要提供這兩種類類型。例如,
public interface UserRepository extends CrudRepository<XmppUser, java.lang.String> {
//..
}
或者
public interface UserRepository extends JpaRepository<XmppUser, java.lang.String> {
//..
}
請注意,第二種類型java.lang.String必須與主鍵屬性的類型相匹配。在這種情況下,您不能將其指定為Stringor Integer,而是指定為java.lang.String。
盡量不要將自定義類命名為String. 使用與 JDK 中已經存在的類名相同的類名是一種不好的做法。
添加回答
舉報