package org.behrang.misc.hibiss;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.time.LocalDate;
class Tests {
private static EntityManagerFactory mysqlFactory;
private static EntityManagerFactory postgresFactory;
@BeforeAll
static void setup() {
mysqlFactory = Persistence.createEntityManagerFactory("mysql");
postgresFactory = Persistence.createEntityManagerFactory("postgres");
}
@AfterAll
static void teardown() {
if (mysqlFactory.isOpen()) {
mysqlFactory.close();
}
if (postgresFactory.isOpen()) {
postgresFactory.close();
}
}
@Test
void testPostgres() {
final EntityManager entityManager = postgresFactory.createEntityManager();
entityManager.getTransaction().begin();
final PostgresPost post1 = new PostgresPost();
post1.setTitle("Simple title (unique)");
post1.setBody("Simple post 1");
post1.setPublishedAt(LocalDate.of(2019, 1, 1));
final PostgresPost post2 = new PostgresPost();
post2.setTitle("Simple title (unique)");
post2.setBody("Simple post 2");
post2.setPublishedAt(LocalDate.of(2019, 1, 2));
entityManager.persist(post1);
entityManager.persist(post2);
entityManager.getTransaction().commit();
entityManager.close();
}
@Test
void testMySQL() {
final EntityManager entityManager = mysqlFactory.createEntityManager();
entityManager.getTransaction().begin();
final MySQLPost post1 = new MySQLPost();
post1.setTitle("Simple title (unique)");
post1.setBody("Simple post");
post1.setPublishedAt(LocalDate.of(2019, 1, 1));
final MySQLPost post2 = new MySQLPost();
post2.setTitle("Simple title (unique)");
post2.setBody("Simple post");
post2.setPublishedAt(LocalDate.of(2019, 1, 1));
entityManager.persist(post1);
entityManager.persist(post2);
entityManager.getTransaction().commit();
entityManager.close();
}
}
package org.behrang.misc.hibiss;
import org.hibernate.annotations.SQLInsert;
import javax.persistence.*;
import java.time.LocalDate;
import static javax.persistence.GenerationType.IDENTITY;
@Entity
@Table(name = "custom_post")
@SQLInsert(sql = "INSERT INTO custom_post(body, published_at, title) VALUES (?, ?, ?) ON CONFLICT DO NOTHING")
public class PostgresPost {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@Column(unique = true)
private String title;
private String body;
@Column(name = "published_at")
private LocalDate publishedAt;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public LocalDate getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(LocalDate publishedAt) {
this.publishedAt = publishedAt;
}
}
package org.behrang.misc.hibiss;
import org.hibernate.annotations.SQLInsert;
import javax.persistence.*;
import java.time.LocalDate;
import static javax.persistence.GenerationType.IDENTITY;
@Entity
@Table(name = "custom_post")
@SQLInsert(sql = "INSERT IGNORE INTO custom_post(body, published_at, title) VALUES (?, ?, ?)")
public class MySQLPost {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@Column(unique = true)
private String title;
private String body;
@Column(name = "published_at")
private LocalDate publishedAt;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public LocalDate getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(LocalDate publishedAt) {
this.publishedAt = publishedAt;
}
}