简体   繁体   中英

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:

在此处输入图像描述

this is the error I get when I try to make requests:

org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "USER0_.PROFILE_ID" not found; 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]

this is my User model (theres also the getters and setters and constructors but I wont put them here):

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

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

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

First problem:

You don't need the @JoinColumn (which is wrong)

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.

As you have set

spring.jpa.hibernate.ddl-auto=update

the tables will be generated by Hibernate. You only need to insert data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM