Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Created November 1, 2016 11:17
Show Gist options
  • Save thjanssen/d8118e11e2d3c6506ea2b262f88a60ae to your computer and use it in GitHub Desktop.
Save thjanssen/d8118e11e2d3c6506ea2b262f88a60ae to your computer and use it in GitHub Desktop.
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="my-persistence-unit">
<!-- enable selective 2nd level cache -->
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
</persistence-unit>
</persistence>
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
@Column(name = "version")
private int version;
@Column
private String firstName;
@Column
private String lastName;
@ManyToMany(mappedBy="authors")
private Set<Book> books = new HashSet<Book>();
// constructors, getters/setters,
// and everything else is as usual
}
@Entity
@Cacheable
public class Author {
}
public void updateAuthor() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Author a = em.find(Author.class, 1L);
a.setFirstName("New first name");
// execute operations that need to happen
// during the transaction and between
// updating first and last name
a.setLastName("new last name");
em.getTransaction().commit();
em.close();
}
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
TypedQuery<Author> q = em.createQuery("SELECT a FROM Author a WHERE id = :id", Author.class);
q.setParameter("id", 1L);
Author a = q.getSingleResult();
em.getTransaction().commit();
em.close();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Author a = new Author();
a.setFirstName("John");
a.setLastName("Doe");
em.persist(a);
em.getTransaction().commit();
em.close();
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="my-persistence-unit">
<description>My Persistence Unit</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.generate_statistics" value="true" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
</properties>
</persistence-unit>
</persistence>
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Author a = em.find(Author.class, 1L);
a.setLastName("new last name");
em.getTransaction().commit();
em.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment