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
<persistence> | |
<persistence-unit name="my-persistence-unit" transaction-type="JTA"> | |
… | |
<properties> | |
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> | |
<property name="javax.persistence.schema-generation.create-source" value="script"/> | |
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create.sql"/> | |
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 | |
@Table(name = "purchaseOrder") | |
public class Order implements Serializable { | |
@OneToMany(mappedBy = "order", fetch = FetchType.EAGER) | |
private Set<OrderItem> items = new HashSet<OrderItem>(); | |
... | |
} |
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
Book b = (Book) em.createNativeQuery("SELECT * FROM book b WHERE id = 1", Book.class).getSingleResult(); |
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
PersonEntity p = em.find(PersonEntity.class, 1L); | |
log.info("Detach PersonEntity"); | |
em.flush(); | |
em.detach(p); | |
em.createNativeQuery("UPDATE person p SET firstname = firstname || '-changed'").executeUpdate(); | |
p = em.find(PersonEntity.class, 1L); |
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
05:22:13,930 DEBUG [org.hibernate.SQL] – | |
select authors0_.bookId as bookId1_2_0_, | |
authors0_.authorId as authorId2_2_0_, | |
author1_.id as id1_0_1_, | |
author1_.firstName as firstNam2_0_1_, | |
author1_.lastName as lastName3_0_1_, | |
author1_.version as version4_0_1_ | |
from BookAuthor authors0_ inner join Author author1_ | |
on authors0_.authorId=author1_.id | |
where authors0_.bookId=? |
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(); | |
Author a = em.createQuery("SELECT a FROM Author a JOIN FETCH a.books WHERE a.id = 1", Author.class).getSingleResult(); | |
em.getTransaction().commit(); | |
em.close(); | |
log.info(a.getFirstName()+" "+a.getLastName()+" wrote "+a.getBooks().size()+" books."); |
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
@Id | |
@GeneratedValue | |
@Column(name = "id", updatable = false, nullable = false) | |
private Long 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
@Entity | |
@Immutable | |
public class BookView { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Column(name = "id", updatable = false, nullable = false) | |
private Long id; | |
@Version | |
@Column(name = "version") |
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
Session s = (Session) em.getDelegate(); | |
Query q = s.createQuery("SELECT a FROM Author a WHERE id = :id"); | |
q.setParameter("id", 1L); | |
q.setCacheable(true); | |
log.info(q.uniqueResult()); |