Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Last active August 30, 2016 17:34
Show Gist options
  • Save thjanssen/1a2c8119702dae4b72e7ad8a98e90c68 to your computer and use it in GitHub Desktop.
Save thjanssen/1a2c8119702dae4b72e7ad8a98e90c68 to your computer and use it in GitHub Desktop.
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
private Long id;
@NaturalId
private String isbn;
}
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Session session = em.unwrap(Session.class);
Book b = session.byNaturalId(Book.class).using(Book_.isbn.getName(), “978-0321356680”).load();
06:14:40,705 DEBUG SQL:92 – select book_.id as id1_0_ from Book book_ where book_.isbn=?
06:14:40,715 DEBUG SQL:92 – select book0_.id as id1_0_0_, book0_.isbn as isbn2_0_0_, book0_.publishingDate as publishi3_0_0_, book0_.title as title4_0_0_, book0_.version as version5_0_0_ from Book book0_ where book0_.id=?
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Session session = em.unwrap(Session.class);
Book b = session.bySimpleNaturalId(Book.class).load(“978-0321356680”);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Session session = em.unwrap(Session.class);
Book b = session.bySimpleNaturalId(Book.class).with(LockOptions.UPGRADE).load(“978-0321356680”);
06:19:34,055 DEBUG SQL:92 – select book_.id as id1_0_ from Book book_ where book_.isbn=?
06:19:34,128 DEBUG SQL:92 – select book0_.id as id1_0_0_, book0_.isbn as isbn2_0_0_, book0_.publishingDate as publishi3_0_0_, book0_.title as title4_0_0_, book0_.version as version5_0_0_ from Book book0_ where book0_.id=? for update
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Session session = em.unwrap(Session.class);
session.byId(Book.class).load(1L);
log.info(“Get book by id”);
Book b = session.bySimpleNaturalId(Book.class).load(“978-0321356680”);
06:20:39,767 DEBUG SQL:92 – select book0_.id as id1_0_0_, book0_.isbn as isbn2_0_0_, book0_.publishingDate as publishi3_0_0_, book0_.title as title4_0_0_, book0_.version as version5_0_0_ from Book book0_ where book0_.id=?
06:20:39,785 INFO TestHibernateNaturalId:78 – Read book by id
06:20:39,788 INFO TestHibernateNaturalId:81 – Book title: Effective Java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment