Skip to content
Snippets Groups Projects
Commit 73b7ebb0 authored by Goik Martin's avatar Goik Martin
Browse files

replace load() by get() to get rid of exceptions when loading objects by

inexistent primary key values
parent c4049240
No related branches found
No related tags found
No related merge requests found
package hibintro.v1.run;
import hibintro.util.HibernateUtil;
import hibintro.v1.model.User;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
* @author goik
*
* Retrieval of a single {@link User} object from a database.
*
*/
public class GetSingleUser {
/**
* Retrieving a single {@link User} instance by means
* of its primary key value "goik".
* @param args unused
*/
public static void main(String[] args) {
final Session session = HibernateUtil.createSessionFactory("hibernate.cfg.xml").openSession();
final Transaction transaction = session.beginTransaction();
final User u = (User) session.load(User.class, "goik");
if (null == u ) {
System.out.println("No such user 'goik'");
} else {
System.out.println("Found user '" + u.getCname() + "'");
}
transaction.commit();
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment