ここに私のエンティティのスニペットがあります(Javaによって生成されたデフォルトのものであるハッシュコードとequalsも作成されています
@Entity
@Table(name = "media_tspec_external_registry")
public class Registry implements Serializable {
public Registry() {
//for hibernate :D
}
public Registry(String show, String protocol, String externalEndpoint, String userURI, String version) {
super();
this.show = show;
this.protocol = protocol;
this.externalEndpoint = externalEndpoint;
this.userURI = userURI;
this.version = version;
}
@Id
@Column(name = "show", nullable = false)
private String show;
@Id
@Column(name = "protocol", nullable = false)
private String protocol;
@Column(name = "external_endpoint", nullable = true)
private String externalEndpoint;
これらのキー値に基づいて、存在しないエンティティをロードしようとしている私のメソッドは次のとおりです
Registry reg = new Registry("invalid", "idvalue", null, null, null);
Registry reg2 = null;
try {
reg2 = (Registry) session.load(Registry.class, reg);
} catch (HibernateException e) {
throw new UserException("A registry entry does not exist for this show: " + show + " and protocol: " + protocol);
}
例外はスローされず、reg2 はすべてのフィールドが null に設定されたレジストリ オブジェクトに設定されます。
また、ロードは既存のエンティティをロードしないことにも注意しました。
ただし、代わりにgetを使用すると、期待どおりに機能します(存在しないオブジェクトに対してnullを返す有効なオブジェクトをロードします)
reg2 = (Registry) session.get(Registry.class, reg);
説明をいただければ幸いです。
ありがとう