简体   繁体   English

org.h2.jdbc.JdbcSQLSyntaxErrorException:找不到列“USER0_.PROFILE_ID”; SQL 声明:

[英]org.h2.jdbc.JdbcSQLSyntaxErrorException : Column "USER0_.PROFILE_ID" not found; SQL statement:

I'm currently building an application that has an user logic, and I've been trying to build simple methods and just 1 mock on the database but I can't seem to make ANY requests through the api. But, I am able to access h2 database and the user I created for testing is there:我目前正在构建一个具有用户逻辑的应用程序,并且我一直在尝试构建简单的方法并且只对数据库进行 1 个模拟,但我似乎无法通过 api 发出任何请求。但是,我能够访问 h2 数据库,我为测试创建的用户就在那里:

在此处输入图像描述

this is the error I get when I try to make requests:这是我尝试发出请求时遇到的错误:

org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "USER0_.PROFILE_ID" not found; org.h2.jdbc.JdbcSQLSyntaxErrorException:找不到列“USER0_.PROFILE_ID”; SQL statement: select user0_.user_id as user_id1_1_, user0_.user_email as user_ema2_1_, user0_.user_name as user_nam3_1_, user0_.password as password4_1_, user0_.profile_id as profile_5_1_ from table_user user0_ [42122-214] SQL 语句:select user0_.user_id 作为 user_id1_1_,user0_.user_email 作为 user_ema2_1_,user0_.user_name 作为 user_nam3_1_,user0_.password 作为 password4_1_,user0_.profile_id 作为 profile_5_1_ 来自 table_user user0_ [42122-214]

this is my User model (theres also the getters and setters and constructors but I wont put them here):这是我的用户 model(还有 getter、setter 和构造函数,但我不会把它们放在这里):

@Entity
@Table(name="table_user")
public class User implements Serializable{
    private static final long serialVersionUID = 1L;
    
    @Column(name="user_name")
    private String name;
    
    @Column(name="user_email", unique=true)
    private String email;
    
    @Column(name="password")
    private String password;
    
    @Column(name="user_id")
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    
    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="profile_id")
    private UserProfile userProfile;
    

this is my controller:这是我的 controller:

@RestController
@RequestMapping(value="/users", produces="application/json")
public class UserController {
    
    @Autowired
    private TheUserRepository repo;
    
    @GetMapping
    public ResponseEntity<List<User>> findAll(){
        List<User> users= repo.findAll();
        return ResponseEntity.ok().body(users);
    }
    
    @GetMapping(value="/{id}")
    public ResponseEntity<Optional<User>> findById(@PathVariable Integer id){
        Optional<User> user=repo.findById(id);
        return ResponseEntity.ok().body(user);
    }
    
    @GetMapping(value="emails/{email}")
    public ResponseEntity<User> doesEmailExist(User user){
        String email=user.getEmail();
        doesUserExist(email);
        return ResponseEntity.ok().body(user);
    }
    
    private boolean doesUserExist(String email) {
        if (repo.findByEmail(email) != null) {
            return true;
        }
        else {
            return false;
        }
    }
    
    @PostMapping
    public ResponseEntity<String> insert(@RequestBody User user){
        User entity=repo.findByEmail(user.getEmail());
        if(entity==null) {
            repo.save(entity);
            return ResponseEntity.status(HttpStatus.CREATED).body("USER SUCESSFULLY CREATED");
        }else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("AN USER ASSOCIATED TO THIS EMAIL ALREADY EXISTS");
        }   
    }
    
    @PutMapping(value="/updateName/{id}")
    public ResponseEntity<User> updateName(@PathVariable Integer id, @RequestBody User user){
        User updatedName=user;
        updatedName.setName(user.getName());
        repo.save(updatedName);
        return ResponseEntity.ok().body(updatedName);
    }
    
    @PutMapping(value="/updatePassword/{id}")
    public ResponseEntity<User> updatePassword(@PathVariable Integer id, @RequestBody User user){
        User updatedPassword=user;
        updatedPassword.setName(user.getPassword());
        repo.save(updatedPassword);
        return ResponseEntity.ok().body(updatedPassword);
    }
    
    @DeleteMapping(value="/{id}")
    public ResponseEntity<User> delete(@PathVariable Integer id){
        repo.deleteById(id);
        return ResponseEntity.noContent().build();
    }
}

this is my application.properties:这是我的应用程序。属性:

spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=


spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update

spring.jpa.defer-datasource-initialization=true


spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

spring.application.name=currency-exchange-service
server.port= 8080


spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.hbm2ddl.auto=update

and my data.sql:和我的数据。sql:

DROP TABLE IF EXISTS `TABLE_USER`;

CREATE TABLE  `TABLE_USER`(
    `USER_NAME` varchar(250) NOT NULL,
    `USER_EMAIL` varchar(250) NOT NULL,
    `PASSWORD` varchar(250) NOT NULL,
    `USER_ID` int AUTO_INCREMENT PRIMARY KEY,
    `USER_PROFILE` varchar(250)
);

INSERT INTO `TABLE_USER` (`USER_NAME`, `USER_EMAIL`, `PASSWORD`, `USER_PROFILE`)
VALUES ('vitória', 'vitoria@gmail.com', 'testing', 'testing');

thank you so much in advance for anyone who tries to help!非常感谢任何试图提供帮助的人!

if theres anything I've left out, it could probably be here: https://github.com/vitoriaacarvalho/recommend-me-a-show/tree/main/recommend-me-a-show如果有什么我遗漏的,它可能在这里: https://github.com/vitoriaacarvalho/recommend-me-a-show/tree/main/recommend-me-a-show

First problem:第一个问题:

You don't need the @JoinColumn (which is wrong)你不需要@JoinColumn (这是错误的)

Simply use this:只需使用这个:

@OneToOne(cascade=CascadeType.ALL)
private UserProfile userProfile;

Second problem:第二个问题:

Your data.sql contains DDL (Create etc) and DML (insert etc) but this should only contain DML.您的 data.sql 包含 DDL(创建等)和 DML(插入等),但这应该只包含 DML。

As you have set正如你所设定的

spring.jpa.hibernate.ddl-auto=update

the tables will be generated by Hibernate. You only need to insert data.表将由Hibernate生成。你只需要插入数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.h2.jdbc.JdbcSQLSyntaxErrorException:找不到列; SQL 声明 [SPRINGBOOT] - org.h2.jdbc.JdbcSQLSyntaxErrorException: Column not found; SQL statement [SPRINGBOOT] org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 语句中的语法错误 - org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement 引起:org.h2.jdbc.JdbcSQLSyntaxErrorException:Function “SYSUTCDATETIME”未找到; SQL 声明:[90022-200] - Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Function "SYSUTCDATETIME" not found; SQL statement:[90022-200] Spring SQL:org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 语句中的语法错误“;预期为“标识符”,使用 INSERT INTO 时 - Spring SQL: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "; expected "identifier", when using INSERT INTO 如何修复 org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL 语句中的语法错误需要“标识符” - How to fix org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement expected "identifier" Spring JPA H2 数据库获取 org.h2.jdbc.JdbcSQLSyntaxErrorException 未找到表 - Spring JPA H2 database get org.h2.jdbc.JdbcSQLSyntaxErrorException Table not found H2版本升级后org.h2.jdbc.JdbcSQLSyntaxErrorException - org.h2.jdbc.JdbcSQLSyntaxErrorException after H2 version upgrade 插入时间戳记时,H2 org.h2.jdbc.JdbcSQLSyntaxErrorException - H2 org.h2.jdbc.JdbcSQLSyntaxErrorException when inserting a Timestamp org.h2.jdbc.JdbcSQLSyntaxErrorException h2 数据库 java - org.h2.jdbc.JdbcSQLSyntaxErrorException h2 database java org.h2.jdbc.JdbcSQLException:未找到架构“MYAPP”; SQL语句 - org.h2.jdbc.JdbcSQLException: Schema “MYAPP” not found; SQL statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM