日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java baseentity_如何在JPA的BaseEntity中实现equals()和hashcode()方法?

發(fā)布時(shí)間:2025/3/20 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java baseentity_如何在JPA的BaseEntity中实现equals()和hashcode()方法? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我有一個(gè)BaseEntity類,它是我的應(yīng)用程序中所有JPA實(shí)體的超類.

@MappedSuperclass

public abstract class BaseEntity implements Serializable {

private static final long serialVersionUID = -3307436748176180347L;

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

@Column(name = "ID", nullable=false, updatable=false)

protected long id;

@Version

@Column(name="VERSION", nullable=false, updatable=false, unique=false)

protected long version;

}

每個(gè)JPA實(shí)體都從BaseEntity擴(kuò)展并繼承BaseEntity的id和version屬性.

在BaseEntity中實(shí)現(xiàn)equals()和hashCode()方法的最佳方法是什么? BaseEntity的每個(gè)子類都將從BaseEntity繼承equals()和hashCode()行為.

我想做這樣的事情:

public boolean equals(Object other){

if (other instanceof this.getClass()){ //this.getClass() gives class object but instanceof operator expect ClassType; so it does not work

return this.id == ((BaseEntity)other).id;

} else {

return false;

}

}

但是instanceof運(yùn)算符需要classtype而不是class對(duì)象;那是:

> if(其他instanceof BaseEntity)

這將起作用,因?yàn)锽aseEntity在這里是classType

> if(this.getClass的其他實(shí)例)

這不起作用,因?yàn)閠his.getClass()返回此對(duì)象的類對(duì)象

總結(jié)

以上是生活随笔為你收集整理的java baseentity_如何在JPA的BaseEntity中实现equals()和hashcode()方法?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。