diff --git a/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/stringcast/DummyEntity.java b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/stringcast/DummyEntity.java new file mode 100644 index 0000000000..12b57ff112 --- /dev/null +++ b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/stringcast/DummyEntity.java @@ -0,0 +1,44 @@ +package com.baeldung.jpa.stringcast; + +import javax.persistence.*; + +@SqlResultSetMapping(name = "textQueryMapping", classes = { + @ConstructorResult(targetClass = DummyEntity.class, columns = { + @ColumnResult(name = "text") + }) +}) +@Entity +@Table(name = "dummy") +public class DummyEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String text; + + public DummyEntity() { + + } + + public DummyEntity(String text) { + this.text = text; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + +} diff --git a/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/stringcast/QueryExecutor.java b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/stringcast/QueryExecutor.java new file mode 100644 index 0000000000..6f1e2ee5ca --- /dev/null +++ b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/stringcast/QueryExecutor.java @@ -0,0 +1,41 @@ +package com.baeldung.jpa.stringcast; + +import com.sun.istack.internal.Nullable; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class QueryExecutor { + + public static List executeNativeQueryNoCastCheck(String statement, EntityManager em) { + Query query = em.createNativeQuery(statement); + return query.getResultList(); + } + + public static List executeNativeQueryWithCastCheck(String statement, EntityManager em) { + Query query = em.createNativeQuery(statement); + List results = query.getResultList(); + + if (results.isEmpty()) { + return new ArrayList<>(); + } + + if (results.get(0) instanceof String) { + return ((List) results) + .stream() + .map(s -> new String[] { s }) + .collect(Collectors.toList()); + } else { + return (List) results; + } + } + + public static List executeNativeQueryGeneric(String statement, String mapping, EntityManager em) { + Query query = em.createNativeQuery(statement, mapping); + return query.getResultList(); + } + +} diff --git a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml index 3d881673b2..6345e7f364 100644 --- a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml @@ -20,6 +20,21 @@ + + + org.hibernate.jpa.HibernatePersistenceProvider + com.baeldung.jpa.stringcast.DummyEntity + + + + + + + + + + + org.hibernate.jpa.HibernatePersistenceProvider diff --git a/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/stringcast/SpringCastTest.java b/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/stringcast/SpringCastTest.java new file mode 100644 index 0000000000..f66bd5d4e4 --- /dev/null +++ b/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/stringcast/SpringCastTest.java @@ -0,0 +1,58 @@ +package com.baeldung.jpa.stringcast; + +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class SpringCastTest { + + private static EntityManager em; + private static EntityManagerFactory emFactory; + + @BeforeClass + public static void setup() { + emFactory = Persistence.createEntityManagerFactory("jpa-h2"); + em = emFactory.createEntityManager(); + + // insert an object into the db + DummyEntity dummyEntity = new DummyEntity(); + dummyEntity.setText("text"); + + EntityTransaction tr = em.getTransaction(); + tr.begin(); + em.persist(dummyEntity); + tr.commit(); + } + + @Test(expected = ClassCastException.class) + public void givenExecutorNoCastCheck_whenQueryReturnsOneColumn_thenClassCastThrown() { + List results = QueryExecutor.executeNativeQueryNoCastCheck("select text from dummy", em); + + // fails + for (String[] row : results) { + // do nothing + } + } + + @Test + public void givenExecutorWithCastCheck_whenQueryReturnsOneColumn_thenNoClassCastThrown() { + List results = QueryExecutor.executeNativeQueryWithCastCheck("select text from dummy", em); + assertEquals("text", results.get(0)[0]); + } + + @Test + public void givenExecutorGeneric_whenQueryReturnsOneColumn_thenNoClassCastThrown() { + List results = QueryExecutor.executeNativeQueryGeneric("select text from dummy", "textQueryMapping", em); + assertEquals("text", results + .get(0) + .getText()); + } + +} diff --git a/persistence-modules/java-jpa/src/test/resources/persistence.xml b/persistence-modules/java-jpa/src/test/resources/persistence.xml index d94221b54f..f65f2c7c62 100644 --- a/persistence-modules/java-jpa/src/test/resources/persistence.xml +++ b/persistence-modules/java-jpa/src/test/resources/persistence.xml @@ -17,5 +17,40 @@ + + + org.hibernate.jpa.HibernatePersistenceProvider + com.baeldung.jpa.stringcast.DummyEntity + + + + + + + + + + + + + + + + com.baeldung.jpa.stringcast.DummyEntity + + + + + + + + + + + + + + +