Hibernate Primary Key Request

I want to create a query with a primary key. Suppose I have the primary key of a class, PersonKey, the properties are the name and id.

I have a Person class, PersonKey property, address, DOB.

Now I want to search for a person by primary key.

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

Can I get a person by ID by passing a key variable ???

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

+3
source share
1 answer

Yes, you can. Assuming that PersonKey Serializable, just pass it to a method get:

PersonKey pk = new PersonKey(007l, "joe");
Person person = (Person) session.get(Person.class, pk);
+5
source

Source: https://habr.com/ru/post/1745751/


All Articles