4 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
您必須更改存儲(chǔ)庫(kù)中 ID 類型參數(shù)的類型,以匹配實(shí)體上的 id 屬性類型。
來(lái)自 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;
//...
}
它應(yīng)該是
public interface UserRepository extends CrudRepository<XmppUser, String> {
//..
}

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

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

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