这是我的实体的片段(它还创建了哈希码和等号,它们是 java 生成的默认值
@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);
任何解释将不胜感激。
谢谢