一对一Mapspring boot

jtw3ybtb  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(406)

大家好,我有一个问题,我的一对一连接,当我试图登录到“用户”我得到这个错误
我需要的是一个一对一的连接,它不必是持久的,这意味着用户可以拥有一个播放器,但不必拥有它,反之亦然
因为在我的例子中,minecraft服务器应该填充player表,而网站填充用户以便他们可以连接。

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

用户实体:

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

玩家实体:

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用户:

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

玩家飞行路线:

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)

);
bjp0bcyl

bjp0bcyl1#

这个错误很明显

Column "player0_.player" not found;

player表没有player列
错误如下:

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

更改为:

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

它是与用户建立连接的列。
建议使用jpa实现的配置参数来验证实体的正确建模,而不需要查询,了解如何使用您选择的实现激活它。

相关问题