Merge pull request #5672 from asturcon/string-cast-jpa
BAEL-2239 - JPA error "java.lang.String cannot be cast to [Ljava.lang.String;"
This commit is contained in:
commit
7de531a320
|
@ -3,13 +3,12 @@ package com.baeldung.jpa.stringcast;
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
|
||||||
@SqlResultSetMapping(name = "textQueryMapping", classes = {
|
@SqlResultSetMapping(name = "textQueryMapping", classes = {
|
||||||
@ConstructorResult(targetClass = DummyEntity.class, columns = {
|
@ConstructorResult(targetClass = Message.class, columns = {
|
||||||
@ColumnResult(name = "text")
|
@ColumnResult(name = "text")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "dummy")
|
public class Message {
|
||||||
public class DummyEntity {
|
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@ -17,11 +16,11 @@ public class DummyEntity {
|
||||||
|
|
||||||
private String text;
|
private String text;
|
||||||
|
|
||||||
public DummyEntity() {
|
public Message() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DummyEntity(String text) {
|
public Message(String text) {
|
||||||
this.text = text;
|
this.text = text;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
<persistence-unit name="jpa-h2">
|
<persistence-unit name="jpa-h2">
|
||||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||||
<class>com.baeldung.jpa.stringcast.DummyEntity</class>
|
<class>com.baeldung.jpa.stringcast.Message</class>
|
||||||
<properties>
|
<properties>
|
||||||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
|
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
|
||||||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
|
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
|
||||||
|
|
|
@ -3,10 +3,7 @@ package com.baeldung.jpa.stringcast;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.*;
|
||||||
import javax.persistence.EntityManagerFactory;
|
|
||||||
import javax.persistence.EntityTransaction;
|
|
||||||
import javax.persistence.Persistence;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
@ -22,18 +19,18 @@ public class SpringCastUnitTest {
|
||||||
em = emFactory.createEntityManager();
|
em = emFactory.createEntityManager();
|
||||||
|
|
||||||
// insert an object into the db
|
// insert an object into the db
|
||||||
DummyEntity dummyEntity = new DummyEntity();
|
Message message = new Message();
|
||||||
dummyEntity.setText("text");
|
message.setText("text");
|
||||||
|
|
||||||
EntityTransaction tr = em.getTransaction();
|
EntityTransaction tr = em.getTransaction();
|
||||||
tr.begin();
|
tr.begin();
|
||||||
em.persist(dummyEntity);
|
em.persist(message);
|
||||||
tr.commit();
|
tr.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ClassCastException.class)
|
@Test(expected = ClassCastException.class)
|
||||||
public void givenExecutorNoCastCheck_whenQueryReturnsOneColumn_thenClassCastThrown() {
|
public void givenExecutorNoCastCheck_whenQueryReturnsOneColumn_thenClassCastThrown() {
|
||||||
List<String[]> results = QueryExecutor.executeNativeQueryNoCastCheck("select text from dummy", em);
|
List<String[]> results = QueryExecutor.executeNativeQueryNoCastCheck("select text from message", em);
|
||||||
|
|
||||||
// fails
|
// fails
|
||||||
for (String[] row : results) {
|
for (String[] row : results) {
|
||||||
|
@ -43,13 +40,13 @@ public class SpringCastUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenExecutorWithCastCheck_whenQueryReturnsOneColumn_thenNoClassCastThrown() {
|
public void givenExecutorWithCastCheck_whenQueryReturnsOneColumn_thenNoClassCastThrown() {
|
||||||
List<String[]> results = QueryExecutor.executeNativeQueryWithCastCheck("select text from dummy", em);
|
List<String[]> results = QueryExecutor.executeNativeQueryWithCastCheck("select text from message", em);
|
||||||
assertEquals("text", results.get(0)[0]);
|
assertEquals("text", results.get(0)[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenExecutorGeneric_whenQueryReturnsOneColumn_thenNoClassCastThrown() {
|
public void givenExecutorGeneric_whenQueryReturnsOneColumn_thenNoClassCastThrown() {
|
||||||
List<DummyEntity> results = QueryExecutor.executeNativeQueryGeneric("select text from dummy", "textQueryMapping", em);
|
List<Message> results = QueryExecutor.executeNativeQueryGeneric("select text from message", "textQueryMapping", em);
|
||||||
assertEquals("text", results.get(0).getText());
|
assertEquals("text", results.get(0).getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
<persistence-unit name="jpa-h2">
|
<persistence-unit name="jpa-h2">
|
||||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||||
<class>com.baeldung.jpa.stringcast.DummyEntity</class>
|
<class>com.baeldung.jpa.stringcast.Message</class>
|
||||||
<properties>
|
<properties>
|
||||||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
|
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
|
||||||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
|
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
|
||||||
|
|
Loading…
Reference in New Issue