I am getting an error when trying to pull from an Amazon Redshift DB and can't figure out how to fix it.
Error:
com.amazon.support.exceptions.ErrorException: [Amazon](500310) Invalid operation:
column notation .id applied to type character, which is not a composite type;
application.properties:
spring.jpa.show-sql=true
spring.datasource.url=jdbc:redshift://mydb.blah.region.redshift.amazonaws.com:5439/dev?currentSchema=myschema
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.dbcp2.validation-query=SELECT 1
Controller:
@RestController
@RequestMapping("/")
public class ContactController {
private RedshiftRepo repo;
@Autowired
public ContactController(RedshiftRepo repo) {
this.repo = repo;
}
@GetMapping(value = "/get")
public ResponseEntity<List<Contact>> getTest(){
List<Contact> list = repo.findAll();
return new ResponseEntity<List<Contact>>(list, HttpStatus.OK);
}
@GetMapping(value = "/getByEmail")
public ResponseEntity<List<Contact>> getByEmail(@RequestParam String email){
List<Contact> list = repo.getContactByEmail(email);
return new ResponseEntity<List<Contact>>(list, HttpStatus.OK);
}
}
RedshiftRepo:
@Repository
public interface RedshiftRepo extends JpaRepository<Contact, Integer>{
@Query("select id, firstname, lastname, accountid from Contact c where c.email = ?1")
public Contact getContactByEmail(String email);
}
Entity:
@Entity
@Table(name ="user")
public class Contact {
@Id
private String id;
private String firstname;
private String lastname;
private String accountid;
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.contactAPI</groupId>
<artifactId>pocContactAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>pocContactAPI</name>
<description>Contact Getter</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-redshift</artifactId>
<version>1.11.999</version>
</dependency>
<dependency>
<groupId>com.amazon.redshift</groupId>
<artifactId>redshift-jdbc42-no-awssdk</artifactId>
<version>1.2.41.1065</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>redshift</id>
<url>http://redshift-maven-repository.s3-website-us-east-1.amazonaws.com/release</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
When I try to GET localhost:8080/get:
Hibernate: select c1_0.id,c1_0.firstname,c1_0.lastname,c1_0.accountid from user c1_0
com.amazon.support.exceptions.ErrorException: [Amazon](500310) Invalid operation:
column notation .id applied to type character, which is not a composite type;
Any idea what I am doing wrong?
You need to add the following dependency to your pom.xml:
(It is my understanding that this first step has already been completed by you, however I am including it for others as well.)
This will allow you to use the Redshift JDBC driver. You can find more information on this here: https://docs.aws.amazon.com/redshift/latest/mgmt/configure-jdbc-connection.html
You also need to specify the Redshift driver in your application.properties:
You can find more information on this here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc
After making these changes, I was able to run your application and get the following response:
I hope this helps!