Issue
When I trying to log in I have this problem:
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
here is entities User and Orders:
@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;
}
}
When I change FetchType.EAGER
on FetchType.LAZY
in User Entity on method getOrderds() I can succesfuly log in, but when I trying to get my orders I obtain an error:
failed to lazily initialize a collection of role: "ua.sombra.webstore.domain.User.orders, could not initialize proxy - no Session"
I can not solve this problem. What i need to do?
Solution
Your problem is that order
is reserved word in SQL and you cannot use it as a name for your table. Rename it to orders
or smth..
Answered By - veljkost