当前位置: 首页 > 知识库问答 >
问题:

您的SQL语法中有一个错误;请查看与您的MySQL服务器版本相对应的手册,以获得要使用的正确语法

咸昀
2023-03-14

当我试图登录时,我遇到了这个问题:

WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1064, SQLState: 42000
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order orders0_ where orders0_.user_id=10' at line 1
ERROR: org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - An internal error occurred while trying to authenticate the user.
org.springframework.security.authentication.InternalAuthenticationServiceException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order orders0_ where orders0_.user_id=10' at line 1

以下是实体用户和订单:

@Entity
@Table(name = "user")
public class User {

    private int id;
    private String firstname;
    private String lastname;
    private String email;
    private String telephone;
    private String sex;
    private String password;
    private String confirmPassword;
    private String login;

    private Set<Role> roles = new HashSet<Role>(0);
    private Set<Product> products = new HashSet<Product>(0);
    private Set<Order> orders = new HashSet<Order>(0);

    public User(){}

    ...

    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        if(orders!=null){
            this.orders = orders;
        }
    }
}

@Entity
@Table(name="order")
public class Order {

    private int id;
    private OrderStatus status;
    private DeliveryType deliveryType;
    private Date date;
    private PaymentType paymentType;
    private String receiver;
    private String phone;
    private String email;
    private BigDecimal deliveryPrice;
    private String address;
    private String cardNumber;
    private String cardTermOf;
    private int cardThreeNumbers;

    public Order(){}

    private User user;  

    private Set<ProductsInOrder> productsInOrders = new HashSet<ProductsInOrder>();

    ...

    @ManyToOne
    @JoinColumn(name = "user_id")
    public User getUser() {
        return user;
    }

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

}

当我在方法getOrderds()的用户实体中更改fetchtype.eager上的fetchtype.lazy时,我可以成功登录,但是当我试图获取订单时,我得到了一个错误:

未能懒惰地初始化角色集合:“Ua.sombra.webstore.domain.user.Orders,无法初始化代理-没有会话”

我不能解决这个问题。我需要做什么?

共有1个答案

宣冥夜
2023-03-14

您的问题是order是SQL中的保留字,您不能将其用作表的名称。将其重命名为orders或SMTH。

 类似资料: