Last active
August 30, 2016 17:34
-
-
Save thjanssen/1a2c8119702dae4b72e7ad8a98e90c68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Book { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Column(name = “id”, updatable = false, nullable = false) | |
private Long id; | |
@NaturalId | |
private String isbn; | |
… | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EntityManager em = emf.createEntityManager(); | |
em.getTransaction().begin(); | |
Session session = em.unwrap(Session.class); | |
Book b = session.bySimpleNaturalId(Book.class).load(“978-0321356680”); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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”); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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”); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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