DummyEntity renamed to Message

This commit is contained in:
Marcos Lopez Gonzalez 2018-11-12 19:54:39 +01:00
parent f7135b4a40
commit 8d745e684e
4 changed files with 13 additions and 17 deletions

View File

@ -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;
}

View File

@ -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" />

View File

@ -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());
}

View File

@ -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" />