views:

19

answers:

1

Hi ...

I wanna create query by primary key. Supposed I have class primary key, PersonKey, the properties is name and id.

I have Person class, the property is PersonKey, address, DOB.

Now, I wanna search person by primary key.

First, I create instance of PersonKey, and set the name become: joe, and id become:007

can I get the person by ID, by pass the key variable ???

person.findByKey(someKey); , but the logic do not criteria

+1  A: 

Yes you can. Assuming the PersonKey is Serializable, simply pass it to the get method:

PersonKey pk = new PersonKey(007l, "joe");
Person person = (Person) session.get(Person.class, pk);
Pascal Thivent
@Pascal, Why PersonKey needs to be serializable?
Reddy
@Reddy Because of the signature of `Session.get(Class, Serializable)`
Pascal Thivent
Thanks Pascal, but why hibernate forcing serialization?As per my understanding hibernate will generate SQL statements and then only sends over network to Database, in this case serialization is unnecessary.. right?
Reddy