Skip to content

Commit 61ffac2

Browse files
committed
jpa cast error
1 parent a8ee73c commit 61ffac2

5 files changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.jpa.stringcast;
2+
3+
import javax.persistence.*;
4+
5+
@SqlResultSetMapping(name = "textQueryMapping", classes = {
6+
@ConstructorResult(targetClass = DummyEntity.class, columns = {
7+
@ColumnResult(name = "text")
8+
})
9+
})
10+
@Entity
11+
@Table(name = "dummy")
12+
public class DummyEntity {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long id;
17+
18+
private String text;
19+
20+
public DummyEntity() {
21+
22+
}
23+
24+
public DummyEntity(String text) {
25+
this.text = text;
26+
}
27+
28+
public String getText() {
29+
return text;
30+
}
31+
32+
public void setText(String text) {
33+
this.text = text;
34+
}
35+
36+
public Long getId() {
37+
return id;
38+
}
39+
40+
public void setId(Long id) {
41+
this.id = id;
42+
}
43+
44+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.jpa.stringcast;
2+
3+
import com.sun.istack.internal.Nullable;
4+
5+
import javax.persistence.EntityManager;
6+
import javax.persistence.Query;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
public class QueryExecutor {
12+
13+
public static List<String[]> executeNativeQueryNoCastCheck(String statement, EntityManager em) {
14+
Query query = em.createNativeQuery(statement);
15+
return query.getResultList();
16+
}
17+
18+
public static List<String[]> executeNativeQueryWithCastCheck(String statement, EntityManager em) {
19+
Query query = em.createNativeQuery(statement);
20+
List results = query.getResultList();
21+
22+
if (results.isEmpty()) {
23+
return new ArrayList<>();
24+
}
25+
26+
if (results.get(0) instanceof String) {
27+
return ((List<String>) results)
28+
.stream()
29+
.map(s -> new String[] { s })
30+
.collect(Collectors.toList());
31+
} else {
32+
return (List<String[]>) results;
33+
}
34+
}
35+
36+
public static <T> List<T> executeNativeQueryGeneric(String statement, String mapping, EntityManager em) {
37+
Query query = em.createNativeQuery(statement, mapping);
38+
return query.getResultList();
39+
}
40+
41+
}

‎persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@
2020
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
2121
</properties>
2222
</persistence-unit>
23+
24+
<persistence-unit name="jpa-h2">
25+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
26+
<class>com.baeldung.jpa.stringcast.DummyEntity</class>
27+
<properties>
28+
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
29+
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
30+
<property name="javax.persistence.jdbc.user" value="sa" />
31+
<property name="javax.persistence.jdbc.password" value="" />
32+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
33+
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
34+
<property name="show_sql" value="true"/>
35+
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
36+
</properties>
37+
</persistence-unit>
2338

2439
<persistence-unit name="jpa-db">
2540
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.jpa.stringcast;
2+
3+
import org.junit.BeforeClass;
4+
import org.junit.Test;
5+
6+
import javax.persistence.EntityManager;
7+
import javax.persistence.EntityManagerFactory;
8+
import javax.persistence.EntityTransaction;
9+
import javax.persistence.Persistence;
10+
import java.util.List;
11+
12+
import static org.junit.Assert.assertEquals;
13+
14+
public class SpringCastTest {
15+
16+
private static EntityManager em;
17+
private static EntityManagerFactory emFactory;
18+
19+
@BeforeClass
20+
public static void setup() {
21+
emFactory = Persistence.createEntityManagerFactory("jpa-h2");
22+
em = emFactory.createEntityManager();
23+
24+
// insert an object into the db
25+
DummyEntity dummyEntity = new DummyEntity();
26+
dummyEntity.setText("text");
27+
28+
EntityTransaction tr = em.getTransaction();
29+
tr.begin();
30+
em.persist(dummyEntity);
31+
tr.commit();
32+
}
33+
34+
@Test(expected = ClassCastException.class)
35+
public void givenExecutorNoCastCheck_whenQueryReturnsOneColumn_thenClassCastThrown() {
36+
List<String[]> results = QueryExecutor.executeNativeQueryNoCastCheck("select text from dummy", em);
37+
38+
// fails
39+
for (String[] row : results) {
40+
// do nothing
41+
}
42+
}
43+
44+
@Test
45+
public void givenExecutorWithCastCheck_whenQueryReturnsOneColumn_thenNoClassCastThrown() {
46+
List<String[]> results = QueryExecutor.executeNativeQueryWithCastCheck("select text from dummy", em);
47+
assertEquals("text", results.get(0)[0]);
48+
}
49+
50+
@Test
51+
public void givenExecutorGeneric_whenQueryReturnsOneColumn_thenNoClassCastThrown() {
52+
List<DummyEntity> results = QueryExecutor.executeNativeQueryGeneric("select text from dummy", "textQueryMapping", em);
53+
assertEquals("text", results
54+
.get(0)
55+
.getText());
56+
}
57+
58+
}

‎persistence-modules/java-jpa/src/test/resources/persistence.xml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,40 @@
1717
<property name="hibernate.show_sql" value="true" />
1818
</properties>
1919
</persistence-unit>
20+
21+
<persistence-unit name="jpa-h22">
22+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
23+
<class>com.baeldung.jpa.stringcast.DummyEntity</class>
24+
<properties>
25+
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
26+
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
27+
<property name="javax.persistence.jdbc.user" value="sa" />
28+
<property name="javax.persistence.jdbc.password" value="" />
29+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
30+
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
31+
<property name="show_sql" value="true"/>
32+
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
33+
</properties>
34+
</persistence-unit>
35+
36+
<persistence-unit name="h2" transaction-type="RESOURCE_LOCAL">
37+
38+
<!-- add classes -->
39+
<class>com.baeldung.jpa.stringcast.DummyEntity</class>
40+
41+
<properties>
42+
<!-- Configuring JDBC properties -->
43+
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
44+
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
45+
46+
<!-- Hibernate properties -->
47+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
48+
<property name="hibernate.hbm2ddl.auto" value="validate"/>
49+
<property name="hibernate.format_sql" value="false"/>
50+
<property name="hibernate.show_sql" value="true"/>
51+
52+
</properties>
53+
</persistence-unit>
54+
2055
</persistence>
2156

0 commit comments

Comments
 (0)