亚马逊红移无效操作:column notation .id应用于类型字符,它不是复合类型;Spring数据JPA

ssm49v7z  于 2023-03-12  发布在  Spring
关注(0)|答案(1)|浏览(123)

我得到一个错误时,试图从亚马逊红移数据库拉,不知道如何修复它。
错误:

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

控制器:

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

红移回购:

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

当我尝试获取本地主机: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;

你知道我做错了什么吗?

aoyhnmkz

aoyhnmkz1#

您需要向pom.xml添加以下依赖项:

  • (据我所知,您已经完成了第一步,但我也为其他人提供了这一步。)*
<dependency>
    <groupId>com.amazon.redshift</groupId>
    <artifactId>redshift-jdbc42-no-awssdk</artifactId>
    <version>1.2.41.1065</version>
</dependency>

这将允许您使用Redshift JDBC驱动程序。您可以在这里找到更多信息:https://docs.aws.amazon.com/redshift/latest/mgmt/configure-jdbc-connection.html
您还需要在www.example.com中指定Redshift驱动程序application.properties:

spring.datasource.driver-class-name=com.amazon.redshift.jdbc42.Driver

您可以在这里找到更多信息:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc
进行这些更改后,我能够运行您的应用程序并得到以下响应:

[
    {
        "id": "1",
        "firstname": "John",
        "lastname": "Doe",
        "accountid": "1"
    }
]

希望这能有所帮助!

相关问题