Skip to content
Advertisement

One to One Mapping Spring boot

Hello everyone in advance I have a problem with my one to one connection when I try to log in to the “user” I get this error

what i need is a one to one connection that does not have to be persistent that means a user can have a player but does not have to have it and the other way around

Since in my case the Minecraft server is supposed to fill the player table and the website fills the users so that they can be connected.

2020-07-27 23:06:12,537 DEBUG [http-nio-8080-exec-2] nirvanacw.de.demo.user.UserServiceImpl: --> getUserByEmail email=Test@gmx.de
2020-07-27 23:06:12,550 WARN  [http-nio-8080-exec-2] org.hibernate.engine.jdbc.spi.SqlExceptionHelper: SQL Error: 42122, SQLState: 42S22
2020-07-27 23:06:12,550 ERROR [http-nio-8080-exec-2] org.hibernate.engine.jdbc.spi.SqlExceptionHelper: Feld "player0_.player" nicht gefunden
Column "player0_.player" not found; SQL statement:
select player0_.id as id1_2_1_, player0_.banned as banned2_2_1_, player0_.first_joined as first_jo3_2_1_, player0_.ip_address as ip_addre4_2_1_, player0_.last_joined as last_joi5_2_1_, player0_.player as player8_2_1_, player0_.player_name as player_n6_2_1_, player0_.uuid as uuid7_2_1_, user1_.id as id1_3_0_, user1_.active as active2_3_0_, user1_.created as created3_3_0_, user1_.email as email4_3_0_, user1_.first_name as first_na5_3_0_, user1_.last_name as last_nam6_3_0_, user1_.password as password7_3_0_, user1_.reset_password as reset_pa8_3_0_, user1_.roles as roles9_3_0_, user1_.updated as updated10_3_0_, user1_.username as usernam11_3_0_ from player player0_ left outer join user user1_ on player0_.player=user1_.id where player0_.player=? [42122-200]
2020-07-27 23:06:12,558 ERROR [http-nio-8080-exec-2] org.springframework.security.oauth2.provider.endpoint.TokenEndpoint: Handling error: InternalAuthenticationServiceException, could not prepare statement; SQL [select player0_.id as id1_2_1_, player0_.banned as banned2_2_1_, player0_.first_joined as first_jo3_2_1_, player0_.ip_address as ip_addre4_2_1_, player0_.last_joined as last_joi5_2_1_, player0_.player as player8_2_1_, player0_.player_name as player_n6_2_1_, player0_.uuid as uuid7_2_1_, user1_.id as id1_3_0_, user1_.active as active2_3_0_, user1_.created as created3_3_0_, user1_.email as email4_3_0_, user1_.first_name as first_na5_3_0_, user1_.last_name as last_nam6_3_0_, user1_.password as password7_3_0_, user1_.reset_password as reset_pa8_3_0_, user1_.roles as roles9_3_0_, user1_.updated as updated10_3_0_, user1_.username as usernam11_3_0_ from player player0_ left outer join user user1_ on player0_.player=user1_.id where player0_.player=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
org.springframework.security.authentication.InternalAuthenticationServiceException: could not prepare statement; SQL [select player0_.id as id1_2_1_, player0_.banned as banned2_2_1_, player0_.first_joined as first_jo3_2_1_, player0_.ip_address as ip_addre4_2_1_, player0_.last_joined as last_joi5_2_1_, player0_.player as player8_2_1_, player0_.player_name as player_n6_2_1_, player0_.uuid as uuid7_2_1_, user1_.id as id1_3_0_, user1_.active as active2_3_0_, user1_.created as created3_3_0_, user1_.email as email4_3_0_, user1_.first_name as first_na5_3_0_, user1_.last_name as last_nam6_3_0_, user1_.password as password7_3_0_, user1_.reset_password as reset_pa8_3_0_, user1_.roles as roles9_3_0_, user1_.updated as updated10_3_0_, user1_.username as usernam11_3_0_ from player player0_ left outer join user user1_ on player0_.player=user1_.id where player0_.player=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement

User Entity :

    package nirvanacw.de.demo.user;
    
    import nirvanacw.de.demo.player.Player;
    import org.hibernate.annotations.GenericGenerator;
    
    import javax.persistence.*;
    import javax.validation.constraints.Email;
    import java.time.ZonedDateTime;
    import java.util.Objects;
    
    @Entity
    @Table(name = "user")
    public class User {
    
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
        @GenericGenerator(name = "native", strategy = "native")
        @Column(name = "id", updatable = false, nullable = false)
        private Long id;
    
        @Email
        @Column(name = "email")
        private String email;
    
        @Column(name ="username")
        private String username;
    
        @Column(name ="password")
        private String password;
    
        @Column(name ="first_name")
        private String firstName;
    
        @Column(name ="last_name")
        private String lastName;
    
        @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        private Player player;
    
        @Column(name ="reset_password")
        private boolean resetPassword;
    
        @Column(name ="roles")
        private String roles;
    
        @Column(name ="active")
        private boolean active;
    
        @Column(name = "created")
        private ZonedDateTime created;
    
        @Column(name = "updated")
        private ZonedDateTime updated;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public boolean isResetPassword() {
            return resetPassword;
        }
    
        public void setResetPassword(boolean resetPassword) {
            this.resetPassword = resetPassword;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getRoles() {
            return roles;
        }
    
        public void setRoles(String roles) {
            this.roles = roles;
        }
    
        public boolean getActive() {
            return active;
        }
    
        public void setActive(boolean active) {
            this.active = active;
        }
    
        public Player getPlayer() {
            return player;
        }
    
        public void setPlayer(Player player) {
            this.player = player;
        }
    
        @PrePersist
        public void onPrePersist() {
            this.created = ZonedDateTime.now();
        }
    
        @PreUpdate
        public void onPreUpdate() {
            this.updated = ZonedDateTime.now();
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", email='" + email + ''' +
                    ", username='" + username + ''' +
                    ", firstName='" + firstName + ''' +
                    ", lastName='" + lastName + ''' +
                    ", player=" + player +
                    ", resetPassword=" + resetPassword +
                    ", roles='" + roles + ''' +
                    ", active=" + active +
                    ", created=" + created +
                    ", updated=" + updated +
                    '}';
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            User user = (User) o;
            return resetPassword == user.resetPassword &&
                    active == user.active &&
                    Objects.equals(id, user.id) &&
                    Objects.equals(email, user.email) &&
                    Objects.equals(username, user.username) &&
                    Objects.equals(password, user.password) &&
                    Objects.equals(firstName, user.firstName) &&
                    Objects.equals(lastName, user.lastName) &&
                    Objects.equals(player, user.player) &&
                    Objects.equals(roles, user.roles) &&
                    Objects.equals(created, user.created) &&
                    Objects.equals(updated, user.updated);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(id, email, username, password, firstName, lastName, player, resetPassword, roles, active, created, updated);
        }
    }

Player Entity :

import java.sql.Timestamp;
import java.util.Objects;
import java.util.UUID;

@Entity
@Table(name = "player")
public class Player {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Column(name = "uuid")
    private UUID uuid;

    @OneToOne
    @JoinColumn(name = "player")
    private User user;

    @Length(max = 16)
    @Column(name = "player_name")
    private String userName;

    @Column(name = "banned")
    private boolean banned;

    @Column(name = "first_joined")
    private Timestamp firstJoined;

    @Column(name = "last_joined")
    private Timestamp lastJoined;

    @Column(name = "ip_address")
    private String ipAddress;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public UUID getUuid() {
        return uuid;
    }

    public void setUuid(UUID uuid) {
        this.uuid = uuid;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public boolean isBanned() {
        return banned;
    }

    public void setBanned(boolean banned) {
        this.banned = banned;
    }

    public Timestamp getFirstJoined() {
        return firstJoined;
    }

    public void setFirstJoined(Timestamp firstJoined) {
        this.firstJoined = firstJoined;
    }

    public Timestamp getLastJoined() {
        return lastJoined;
    }

    public void setLastJoined(Timestamp lastJoined) {
        this.lastJoined = lastJoined;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    @Override
    public String toString() {
        return "Player{" +
                "id=" + id +
                ", uuid=" + uuid +
                ", user=" + user +
                ", userName='" + userName + ''' +
                ", banned=" + banned +
                ", firstJoined=" + firstJoined +
                ", lastJoined=" + lastJoined +
                ", ipAddress='" + ipAddress + ''' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Player player = (Player) o;
        return banned == player.banned &&
                Objects.equals(id, player.id) &&
                Objects.equals(uuid, player.uuid) &&
                Objects.equals(user, player.user) &&
                Objects.equals(userName, player.userName) &&
                Objects.equals(firstJoined, player.firstJoined) &&
                Objects.equals(lastJoined, player.lastJoined) &&
                Objects.equals(ipAddress, player.ipAddress);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, uuid, user, userName, banned, firstJoined, lastJoined, ipAddress);
    }
}

Flyway user :

Create TABLE user(
    id bigint NOT NULL AUTO_INCREMENT,
    email character varying(256),
    username character varying(256),
    password character varying(256),
    reset_password boolean,
    first_name character varying(50),
    last_name character varying(50),
    gender character varying(10),
    player_id character varying(17),
    active boolean,
    roles character varying(40),
    created TIMESTAMP,
    updated TIMESTAMP,

    CONSTRAINT PK_account_account_id PRIMARY KEY (id)
);

Player Flyway :

CREATE TABLE player(
    id bigint NOT NULL AUTO_INCREMENT,
    uuid char(36) NOT NULL,
    player_name character varying (16),
    user_id bigint,
    banned boolean,
    first_joined timestamp,
    last_joined timestamp,
    ip_address character varying (15),

    CONSTRAINT PK_player_player_id PRIMARY KEY (id),
    CONSTRAINT FK_player_user_id_user_id FOREIGN KEY (user_id) REFERENCES user(id)

);

Advertisement

Answer

The error is quite clear

Column "player0_.player" not found;

the player table does not have a player column

the error is here:

@OneToOne
@JoinColumn(name = "player")
private User user;

change to:

@OneToOne
@JoinColumn(name = "user_id")
private User user;

It is the column by which the join with user is established.

As a recommendation, there are configuration parameters for the JPA implementation to validate the correct modeling of the entities without the need for queries, find out how to activate it with the implementation you have chosen.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement