1

here is a snippet of my entity (it also has hashcode and equals created which are the default ones generated by 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;

here is my method which is trying to load an entity which does not exist, based on these key values

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);
        }

it never throws the exception and reg2 is now set to a registry object with all the fields set to null.

i have also noted that the load will not even load a existing entity.

however if i use get instead it works as expected (loading valid object returning null for non existing objects)

reg2 = (Registry) session.get(Registry.class, reg);

any explanation would be appreciated.

Thanks

Shaun Stone
  • 626
  • 1
  • 5
  • 19
  • Strange. Registry is both entity and composite key at the same time? – Piotr Kochański Apr 25 '12 at 17:58
  • @PiotrKochański based on the [hibnate docs](http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifier) section 2.2.3.2.2. i took that to mean that the entity itself was the serializable identifier (similar to an idclass or embedded id) – Shaun Stone Apr 25 '12 at 18:16
  • Look at answer to question http://stackoverflow.com/q/608947/971040 It may direct you in the right way. – viktor Apr 25 '12 at 21:20
  • @viktor thanks looks like i am seeing a behavior mentioned in that post **specfically Using load() has a further implication: The application may retrieve a valid reference (a proxy) to a persistent instance without hitting the database to retrieve its persistent state. So load() might not throw an exception when it doesn’t find the persistent object in the cache or database; the exception would be thrown later, when the proxy is accessed.** sounds like a bug to me, but at least i have an answer... thanks for you help – Shaun Stone Apr 25 '12 at 23:37

1 Answers1

1

this is expected behavior. session.load is meant to get an object which can be used to satisfy references, e.g.

User u = new User();
u.setRole((Role)session.load(Role.class, 5));
session.save(u);

load will not generate a roundtrip. If there is no reference to the object available it will create a proxyobject or in your case will recycle your compositekey object and relies on you that the entity exists since it can not ask the database if it is so.

Firo
  • 30,626
  • 4
  • 55
  • 94