使用JPA ORM的MySQL数据库中的com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

时间:2015-11-12 00:32:23

标签: java mysql spring-mvc jpa jdbc

db的结构有点复杂 - 简而言之:我有一个随机的地方有一些菜单选项,每个菜单选项都包含一些用户可以选择的可选功能。我需要创建一个方法来保存用户所做的每个订单的信息。

这是我的数据库表示的图像:enter image description here

我的控制器代码:

@RequestMapping(value = "/place/{placeId}/menu/{menuId}", method = RequestMethod.POST)
    public String createOrder(@PathVariable("placeId") long placeId,
                              @PathVariable("menuId") long menuId,
                              @RequestParam(value="services", required = false) List<Long> services) {
        PlaceUser user = userService.placeUser();
        dao.newOrder(user, dao.getPlaceById(placeId),dao.getMenuById(menuId), services);
        return "redirect:/place/" + placeId;
    }

保存方法:

@Override
    @Transactional
    public void newOrder(PlaceUser user, Place place, PlaceMenu menu, List<Long> servicesList) {

        Order order = new Order();
        order.setUser(user);
        order.setPlace(place);
        order.setMenu(menu);
        em.persist(order);

        if(servicesList!=null){
            for(long sId:servicesList){
                PlaceMenuOptionalService services = getMenuServicesById(sId);
                order.setServices(getServiceAsList(services,order.getServices()));
                em.merge(order);
                services.setOrders(getOrdersList(order,services.getOrders()));
                em.merge(services);
            }
        }

        user.setOrders(getOrdersList(order,user.getOrders()));
        em.merge(user);
        place.setOrders(getOrdersList(order, place.getOrders()));
        em.merge(place);
        menu.setOrders(getOrdersList(order,menu.getOrders()));
        em.merge(menu);

        em.flush();
    }

发布的代码太多了。您可以在我的GitHub存储库中找到实体代码和任何其他代码

https://github.com/dimitrij94/FindAndGo

另外,如果我允许jpa自己创建外键,我会得到一些奇怪的结果。它会将service_id和orderid绑定到place_menu_service(id),所以我的@JoinTable注释可能有问题

  

2015-11-12 02:10:56.970 ERROR 3164 --- [nio-8080-exec-7]   o.a.c.c.C。[。[。[/]。[dispatcherServlet]:Servlet.service()for   在path []的上下文中的servlet [dispatcherServlet]引发了异常   [请求处理失败;嵌套异常是   javax.persistence.PersistenceException:   org.hibernate.exception.SQLGrammarException:无法执行   语句具有根本原因

     

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你有一个   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本,用于在'Order(菜单,   地点,用户)第1行的值(8,30,13)'   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)     在   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)     在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     在java.lang.reflect.Constructor.newInstance(Constructor.java:422)

    @Entity
    public class Order {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        Long id;

        @ManyToOne
        @JoinColumn(name = "place")
        Place place;

        @ManyToOne
        @JoinColumn(name = "user")
        PlaceUser user;

        @ManyToOne
        @JoinColumn(name = "menu")
        PlaceMenu menu;


        @ManyToMany
        @JoinTable(
                joinColumns =@JoinColumn(name = "order_id", referencedColumnName = "id"),
                inverseJoinColumns = @JoinColumn(name = "service_id", referencedColumnName = "id"),
                name = "service_orders")
        List<PlaceMenuOptionalService> services;
...standart getters and setters


 @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String placeName;

    private String placeDescription;

    private int placeFinalRating;
    private int placeRatingsNum;
    private int placeFollowersNum;
    private String placeSpeciality;

    @OneToMany(mappedBy = "place")
    private List<PlaceRating> placeRatings;

    @OneToMany(mappedBy= "menuPlace")
    private List<PlaceMenu>placeMenu;

    @ManyToMany(mappedBy = "userPlaces")
    private List<PlaceUser> placeUsers;

    @OneToMany(mappedBy = "place")
    private List<PlacePhoto> placePhotos;

    @OneToMany(mappedBy = "place")
    List<PlaceEvent>placeEvents;

    @OneToMany(mappedBy = "place")
    List<PlaceComment>placeComments;

    @ManyToOne
    @JoinColumn(name = "placeOwner")
    PlaceUser placeOwner;

    @OneToOne(mappedBy = "place")
    PlaceAddress address;

    @OneToMany(mappedBy = "place")
    List<Order>orders;
...standart getters and setters

@Entity
@Table
public class PlaceUser {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String userName;
    private String userEmail;
    private String userPass;
    private boolean enabled;

    private String sex;
    private int age;
    private String name;
    private String sname;

    @OneToOne(mappedBy = "user")
    private UserAddress address;

    @OneToOne(mappedBy = "user")
    private VerificationToken tokens;

    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
    private List<Authorities> authorities;

    @OneToMany(mappedBy = "user")
    private List<PlaceMenuRating> user;

    @OneToMany(mappedBy = "placeOwner")
    private List<Place> ownerPlaces;

    @OneToMany(mappedBy = "user")
    private List<PlaceRating> userPlacesRating;

    @ManyToMany(mappedBy = "users")
    private List<PlaceEvent> userEvents;

    @OneToMany(mappedBy = "user")
    private List<Order>orders;

@Entity
public class PlaceMenu implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String menuName;
    private String menuDescription;
    private int menuPrice;
    private int menuFinalRating;

    @OneToMany(mappedBy = "menu")
    private List<PlaceMenuRating> menuRatings;

    @ManyToOne
    @JoinColumn(name="menuPlace")
    private Place menuPlace;

    @OneToMany(mappedBy = "menu")
    private List<PlaceMenuPhoto> photo;

    @OneToMany(mappedBy = "menu")
    private  List<PlaceMenuOptionalService>services;

    @OneToMany(mappedBy = "menu")
    private List<Order>orders;

    private int durationH;
    private int durationM;

    public PlaceMenu() {
    }

    public PlaceMenu(DefaultMultipartHttpServletRequest dmhsRequest) {
        this.menuName=dmhsRequest.getParameter("menuName");
        this.menuDescription=dmhsRequest.getParameter("menuDescription");
        this.menuPrice=Integer.valueOf(dmhsRequest.getParameter("menuPrice"));
    }

    public PlaceMenu(MenuDTO menuDTO) {
        this.menuName=menuDTO.getName();
        this.menuDescription=menuDTO.getDescription();
        this.menuPrice=menuDTO.getPrice();
        this.durationH=menuDTO.getHours();
        this.durationM=menuDTO.getMinutes();
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public int getMenuFinalRating() {
        return menuFinalRating;
    }

    public void setMenuFinalRating(int menuFinalRating) {
        this.menuFinalRating = menuFinalRating;
    }

    public Long getId() {
        return id;
    }

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

    public Place getMenuPlace() {
        return menuPlace;
    }

    public void setMenuPlace(Place place) {
        this.menuPlace = place;
    }

    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String serviceName) {
        this.menuName = serviceName;
    }

    public String getMenuDescription() {
        return menuDescription;
    }

    public void setMenuDescription(String serviceDescription) {
        this.menuDescription = serviceDescription;
    }

    public int getMenuPrice() {
        return menuPrice;
    }

    public void setMenuPrice(int menuPrice) {
        this.menuPrice = menuPrice;
    }

    public List<PlaceMenuRating> getMenuRatings() {
        return menuRatings;
    }

    public void setMenuRatings(List<PlaceMenuRating> menuRatings) {
        this.menuRatings = menuRatings;
    }

    public List<PlaceMenuPhoto> getPhoto() {
        return photo;
    }

    public void setPhoto(List<PlaceMenuPhoto> photo) {
        this.photo = photo;
    }

    public List<PlaceMenuOptionalService> getServices() {
        return services;
    }

    public void setServices(List<PlaceMenuOptionalService> services) {
        this.services = services;
    }

    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    public int getDurationH() {
        return durationH;
    }

    public void setDurationH(int durationH) {
        this.durationH = durationH;
    }

    public int getDurationM() {
        return durationM;
    }

    public void setDurationM(int durationM) {
        this.durationM = durationM;
    }


@Entity
@Table(name = "place_menu_services")
public class PlaceMenuOptionalService {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;
    String description;
    int price;

    @ManyToOne
    @JoinColumn(name = "menu")
    PlaceMenu menu;

    @ManyToMany(mappedBy = "services")
    List<Order> orders;

0 个答案:

没有答案