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.*;
|
||||
|
||||
@SqlResultSetMapping(name = "textQueryMapping", classes = {
|
||||
@ConstructorResult(targetClass = DummyEntity.class, columns = {
|
||||
@ConstructorResult(targetClass = Message.class, columns = {
|
||||
@ColumnResult(name = "text")
|
||||
})
|
||||
})
|
||||
@Entity
|
||||
@Table(name = "dummy")
|
||||
public class DummyEntity {
|
||||
public class Message {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
@ -17,11 +16,11 @@ public class DummyEntity {
|
|||
|
||||
private String text;
|
||||
|
||||
public DummyEntity() {
|
||||
public Message() {
|
||||
|
||||
}
|
||||
|
||||
public DummyEntity(String text) {
|
||||
public Message(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
<persistence-unit name="jpa-h2">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.stringcast.DummyEntity</class>
|
||||
<class>com.baeldung.jpa.stringcast.Message</class>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
|
||||
<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.Test;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.EntityTransaction;
|
||||
import javax.persistence.Persistence;
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -22,18 +19,18 @@ public class SpringCastUnitTest {
|
|||
em = emFactory.createEntityManager();
|
||||
|
||||
// insert an object into the db
|
||||
DummyEntity dummyEntity = new DummyEntity();
|
||||
dummyEntity.setText("text");
|
||||
Message message = new Message();
|
||||
message.setText("text");
|
||||
|
||||
EntityTransaction tr = em.getTransaction();
|
||||
tr.begin();
|
||||
em.persist(dummyEntity);
|
||||
em.persist(message);
|
||||
tr.commit();
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
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
|
||||
for (String[] row : results) {
|
||||
|
@ -43,13 +40,13 @@ public class SpringCastUnitTest {
|
|||
|
||||
@Test
|
||||
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]);
|
||||
}
|
||||
|
||||
@Test
|
||||
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());
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
<persistence-unit name="jpa-h2">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.stringcast.DummyEntity</class>
|
||||
<class>com.baeldung.jpa.stringcast.Message</class>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
|
||||
|
|
Loading…
Reference in New Issue