Query error – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use

huangapple go评论78阅读模式
英文:

Query error - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use

问题

以下是翻译好的内容:

Package controller: UserProduct:

package hieuboy.controller;

import ...

@Controller
@Transactional
@RequestMapping(value = "user/product")
public class UserProductController extends HieuBoyShopController {

    @Autowired
    ServletContext application;

    @Autowired
    JavaMailSender mailSender;

    // Tìm kiếm sản phẩm
    @SuppressWarnings("unchecked")
    @RequestMapping("search-product.htm")
    public String searchProduct(ModelMap model, HttpServletRequest request, @RequestParam("") String search) {
        Session session = sessionFactory.getCurrentSession();
        String hqlCount = "SELECT COUNT(p) FROM Product p WHERE p.status = 1 and p.nameProduct LIKE '%" + search + "%'";
        ...
        return "user/product-list";
    }

    // Show danh sách sản phẩm
    @SuppressWarnings("unchecked")
    @RequestMapping("list")
    public String list(ModelMap model, HttpServletRequest request) {
        Session session = sessionFactory.getCurrentSession();
        String hqlCount = "SELECT COUNT(p) FROM Product p";
        ...
        return "user/product-list";
    }

    // Show danh sách sản phẩm theo danh mục
    @SuppressWarnings("unchecked")
    @RequestMapping("list-by-category/{id}.htm")
    public String listCategory(ModelMap model, @PathVariable("id") Integer id, HttpServletRequest request) {
        Session session = sessionFactory.getCurrentSession();
        String hqlCount = "SELECT COUNT(p) FROM Product p WHERE p.status = 1 and p.category.id=:cid1";
        ...
        return "user/product-list";
    }

    // Show danh sách sản phẩm theo hãng sản xuất
    @SuppressWarnings("unchecked")
    @RequestMapping("list-by-producer/{id}.htm")
    public String listProducer(ModelMap model, @PathVariable("id") Integer id, HttpServletRequest request) {
        Session session = sessionFactory.getCurrentSession();
        String hqlCount = "SELECT COUNT(p) FROM Product p WHERE p.status = 1 and p.producer.id = :pid1";
        ...
        return "user/product-list";
    }

    // Xem chi tiết
    @RequestMapping("detail/{id}.htm")
    public String detailProduct(ModelMap model, HttpServletResponse response, @PathVariable("id") Integer id, 
            @ModelAttribute("product") Product product, @CookieValue(value = "views", defaultValue = "") String views) {
        ...
        return "user/product-detail";
    }

    // ...

    // Gửi mail link sản phẩm cho bạn bè
    @RequestMapping("sendToFriend")
    public String sendToFriend(ModelMap model, HttpServletRequest request, @RequestParam String id,
            @RequestParam String from, @RequestParam String to, @RequestParam String subject,
            @RequestParam String body) {
        ...
        return "redirect:/user/product/detail/" + id + ".htm";
    }
}

UserHome:

@SuppressWarnings("unchecked")
@ModelAttribute("saleOffProducts")
public List<Product> get9SaleOffProducts() {
    Session session = sessionFactory.getCurrentSession();
    String hql = "FROM Product p WHERE p.status = 1 AND p.discount > 0 ORDER BY p.discount DESC";
    ...
    return query.list();
}

Model:

package hieuboy.model;

import ...

@Entity
@Table(name = "Products", catalog = "HieuBoyShop")
public class Product implements java.io.Serializable {

    private static final long serialVersionUID = 4208468054445602750L;
    private Integer id;
    private Category category;
    private Producer producer;
    private String nameProduct;
    private String photo;
    private Integer quantity;
    private Date productDate;
    private String unitBrief;
    private Double unitPrice;
    private Double discount;
    private String description;
    private Integer views;
    private Boolean available;
    private Boolean special;
    private Boolean latest;
    private Boolean status;
    private Collection<OrderDetail> orderDetail;

    public Product() {
    }

    // ...
}

MySQL:

CREATE TABLE `products` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `NameProduct` varchar(50) NOT NULL,
  `Photo` varchar(255) DEFAULT NULL,
  `Quantity` int NOT NULL,
  `ProductDate` datetime NOT NULL,
  `UnitBrief` varchar(50) NOT NULL,
  `UnitPrice` float NOT NULL,
  `Discount` float DEFAULT NULL,
  `Description` varchar(1000) DEFAULT NULL,
  `Views` int DEFAULT NULL,
  `Available` bit(1) DEFAULT NULL,
  `Special` bit(1) DEFAULT NULL,
  `Latest` bit(1) DEFAULT NULL,
  `Status` bit(1) NOT NULL,
  `CategoryID` int NOT NULL,
  `ProducerID` int NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `FK_Products_Categories` (`CategoryID`),
  KEY `FK_Products_Producers` (`ProducerID`),
  CONSTRAINT `FK_Products_Categories` FOREIGN KEY (`CategoryID`) REFERENCES `categories` (`ID`),
  CONSTRAINT `FK_Products_Producers` FOREIGN KEY (`ProducerID`) REFERENCES `producers` (`ID`)
);

希望这能对你有所帮助。

英文:

The project uses MS SQL but I want to switch to MySQL and I have a query problem:

>...threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException:
could not extract ResultSet] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:

>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 '9 product0_.ID as ID1_11_, product0_.Available as Availabl2_11_, product0_.Categ' at line 1

Package controller:
UserProduct:

package hieuboy.controller;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import hieuboy.admin.controller.HieuBoyShopController;
import hieuboy.model.Product;
import hieuboy.other.PagerModel;
@Controller
@Transactional
@RequestMapping(value = &quot;user/product&quot;)
public class UserProductController extends HieuBoyShopController {
@Autowired
ServletContext application;
@Autowired
JavaMailSender mailSender;
// T&#236;m kiếm sản phẩm
@SuppressWarnings(&quot;unchecked&quot;)
@RequestMapping(&quot;search-product.htm&quot;)
public String searchProduct(ModelMap model, HttpServletRequest request, @RequestParam(&quot;&quot;) String search) {
Session session = sessionFactory.getCurrentSession();
// Đếm sản phẩm để ph&#226;n trang v&#224; t&#236;m kiếm
String hqlCount = &quot;SELECT COUNT(p) FROM Product p WHERE p.status = 1 and p.nameProduct LIKE &#39;%&quot; + search + &quot;%&#39;&quot;;
Long rowCount = (Long) session.createQuery(hqlCount).uniqueResult();
System.out.println(rowCount);
PagerModel pager = new PagerModel();
pager = PagerModel.getPager(&quot;acpager&quot;, 9, rowCount.intValue(), request);
pager.setRowCount(rowCount.intValue());
pager.setPageSize(9);
pager.navigate(request);
model.addAttribute(&quot;search&quot;, search);
model.addAttribute(&quot;flat&quot;, &quot;search-product&quot;);
// T&#236;m kiếm sản phẩm
String hql = &quot;FROM Product p WHERE p.status = 1 and p.nameProduct LIKE &#39;%&quot; + search + &quot;%&#39;&quot;;
Query query = session.createQuery(hql);
query.setFirstResult(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
List&lt;Product&gt; list = query.list();
model.addAttribute(&quot;list&quot;, list);
return &quot;user/product-list&quot;;
}
// Show danh s&#225;ch sản phẩm
@SuppressWarnings(&quot;unchecked&quot;)
@RequestMapping(&quot;list&quot;)
public String list(ModelMap model, HttpServletRequest request) {
Session session = sessionFactory.getCurrentSession();
// Đếm sản phẩm v&#224; ph&#226;n trang
String hqlCount = &quot;SELECT COUNT(p) FROM Product p&quot;;
Long rowCount = (Long) session.createQuery(hqlCount).uniqueResult();
PagerModel pager = new PagerModel();
pager = PagerModel.getPager(&quot;acpager&quot;, 9, rowCount.intValue(), request);
pager.setRowCount(rowCount.intValue());
pager.navigate(request);
model.addAttribute(&quot;flat&quot;, &quot;list&quot;);
// Lấy danh s&#225;ch sản phẩm c&#243; views giảm dần
String hql = &quot;FROM Product p WHERE p.status = 1 ORDER BY p.views DESC&quot;;
Query query = session.createQuery(hql);
query.setFirstResult(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
List&lt;Product&gt; list = query.list();
model.addAttribute(&quot;list&quot;, list);
return &quot;user/product-list&quot;;
}
// Show danh s&#225;ch sản phẩm theo danh mục
@SuppressWarnings(&quot;unchecked&quot;)
@RequestMapping(&quot;list-by-category/{id}.htm&quot;)
public String listCategory(ModelMap model, @PathVariable(&quot;id&quot;) Integer id, HttpServletRequest request) {
Session session = sessionFactory.getCurrentSession();
// Ph&#226;n trang sản phẩm theo danh mục
String hqlCount = &quot;SELECT COUNT(p) FROM Product p WHERE p.status = 1 and p.category.id=:cid1&quot;;
Long rowCount = (Long) session.createQuery(hqlCount).setParameter(&quot;cid1&quot;, id).uniqueResult();
PagerModel pager = new PagerModel();
pager = PagerModel.getPager(&quot;acpager&quot;, 9, rowCount.intValue(), request);
pager.setRowCount(rowCount.intValue());
pager.navigate(request);
model.addAttribute(&quot;flat&quot;, &quot;list-by-category/&quot; + id);
// Lấy danh s&#225;ch sản phẩm theo danh mục
String hql = &quot;FROM Product p WHERE p.status = 1 and p.category.id=:cid&quot;;
Query query = session.createQuery(hql);
query.setParameter(&quot;cid&quot;, id);
query.setFirstResult(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
List&lt;Product&gt; list = query.list();
model.addAttribute(&quot;list&quot;, list);
return &quot;user/product-list&quot;;
}
// Show danh s&#225;ch sản phẩm theo h&#227;ng sản xuất
@SuppressWarnings(&quot;unchecked&quot;)
@RequestMapping(&quot;list-by-producer/{id}.htm&quot;)
public String listProducer(ModelMap model, @PathVariable(&quot;id&quot;) Integer id, HttpServletRequest request) {
Session session = sessionFactory.getCurrentSession();
// Ph&#226;n trang sản phẩm theo h&#227;ng
String hqlCount = &quot;SELECT COUNT(p) FROM Product p WHERE p.status = 1 and p.producer.id = :pid1&quot;;
Long rowCount = (Long) session.createQuery(hqlCount).setParameter(&quot;pid1&quot;, id).uniqueResult();
PagerModel pager = new PagerModel();
pager = PagerModel.getPager(&quot;acpager&quot;, 9, rowCount.intValue(), request);
pager.setRowCount(rowCount.intValue());
pager.navigate(request);
model.addAttribute(&quot;flat&quot;, &quot;list-by-producer/&quot; + id);
// Show danh s&#225;ch sản phẩm theo h&#227;ng
String hql = &quot;FROM Product p WHERE p.status = 1 and p.producer.id = :pid&quot;;
Query query = session.createQuery(hql);
query.setParameter(&quot;pid&quot;, id);
query.setFirstResult(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
List&lt;Product&gt; list = query.list();
model.addAttribute(&quot;list&quot;, list);
return &quot;user/product-list&quot;;
}
// Xem chi tiết
@RequestMapping(&quot;detail/{id}.htm&quot;)
public String detailProduct(ModelMap model, HttpServletResponse response, @PathVariable(&quot;id&quot;) Integer id,
@ModelAttribute(&quot;product&quot;) Product product, @CookieValue(value = &quot;views&quot;, defaultValue = &quot;&quot;) String views) {
product.setId(id);
Session session = sessionFactory.getCurrentSession();
session.refresh(product);
// Tăng số lần xem
product.setViews(product.getViews() + 1);
session.update(product);
// Ghi nhận mặt h&#224;ng đ&#227; xem v&#224;o cookie
if (!views.contains(id.toString())) {
views += &quot;,&quot; + id;
}
String hql = &quot;FROM Product &quot; + &quot; WHERE id IN(2018&quot; + views + &quot;)&quot;;
Query query = session.createQuery(hql);
model.addAttribute(&quot;views&quot;, query.list());
Cookie cookie = new Cookie(&quot;views&quot;, views);
cookie.setMaxAge(60 * 60 * 24 * 30);
response.addCookie(cookie);
return &quot;user/product-detail&quot;;
}
@SuppressWarnings(&quot;unchecked&quot;)
@RequestMapping(&quot;type/{value}.htm&quot;)
public String listSpecial(ModelMap model, @PathVariable(&quot;value&quot;) String value, HttpServletRequest request) {
Session session = sessionFactory.getCurrentSession();
Long rowCount = (long) 0;
Query query = null;
// Danh s&#225;ch h&#224;ng b&#225;n chạy
if (value.equalsIgnoreCase(&quot;best&quot;)) {
// Đếm sản phẩm tr&#234;n 40 đơn h&#224;ng chi tiết
String hqlCount = &quot;SELECT COUNT(p) FROM Product p WHERE p.status = 1 and SIZE(p.orderDetail) &gt;40&quot;;
rowCount = (Long) session.createQuery(hqlCount).uniqueResult();
System.out.println(rowCount);
PagerModel pager = new PagerModel();
pager = PagerModel.getPager(&quot;acpager&quot;, 9, rowCount.intValue(), request);
pager.setRowCount(rowCount.intValue());
pager.navigate(request);
// Lấy danh s&#225;ch sản phẩm tr&#234;n 40 đơn h&#224;ng chi tiết
String hql = &quot;FROM Product p &quot; + &quot; WHERE p.status = 1 and SIZE(p.orderDetail) &gt; 40 &quot;
+ &quot; ORDER BY SIZE(p.orderDetail) DESC &quot;;
query = session.createQuery(hql);
query.setFirstResult(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
model.addAttribute(&quot;flat&quot;, &quot;type/best&quot;);
}
// Danh s&#225;ch h&#224;ng mới nhất
else if (value.equalsIgnoreCase(&quot;latest&quot;)) {
// Đếm sản phẩm mới nhất để ph&#226;n trang
String hqlCount = &quot;SELECT COUNT(p) FROM Product p WHERE p.status =1 and p.latest=1&quot;;
rowCount = (Long) session.createQuery(hqlCount).uniqueResult();
if (rowCount &gt; 24) {
rowCount = (long) 24;
}
PagerModel pager = new PagerModel();
pager = PagerModel.getPager(&quot;acpager&quot;, 9, rowCount.intValue(), request);
pager.navigate(request);
// Lấy danh s&#225;ch sản phẩm mới nhất
String hql = &quot;FROM Product p WHERE p.status = 1 and p.latest = 1&quot;;
query = session.createQuery(hql);
query.setFirstResult(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
model.addAttribute(&quot;flat&quot;, &quot;type/latest&quot;);
}
// Danh s&#225;ch h&#224;ng c&#243; lượt xem cao nhất
else if (value.equalsIgnoreCase(&quot;views&quot;)) {
// Đếm sản phẩm với lượt view cao hơn 0
String hqlCount = &quot;SELECT COUNT(p) FROM Product p WHERE p.status = 1 and p.views &gt; 0&quot;;
rowCount = (Long) session.createQuery(hqlCount).uniqueResult();
if (rowCount &gt; 24) {
rowCount = (long) 24;
}
PagerModel pager = new PagerModel();
pager = PagerModel.getPager(&quot;acpager&quot;, 9, rowCount.intValue(), request);
pager.setRowCount(rowCount.intValue());
pager.navigate(request);
// Lấy danh s&#225;ch sản phẩm theo views
String hql = &quot;FROM Product p WHERE p.status = 1 and p.views &gt; 0 ORDER BY p.views DESC&quot;;
query = session.createQuery(hql);
query.setFirstResult(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
model.addAttribute(&quot;flat&quot;, &quot;type/views&quot;);
}
// Danh s&#225;ch h&#224;ng c&#243; kiểu đăc biệt
else if (value.equalsIgnoreCase(&quot;special&quot;)) {
// Đếm danh s&#225;ch sản phẩm v&#224; ph&#226;n trang theo spcecial
String hqlCount = &quot;SELECT COUNT(p) FROM Product p WHERE p.status=1 and p.special = 1&quot;;
rowCount = (Long) session.createQuery(hqlCount).uniqueResult();
if (rowCount &gt; 24) {
rowCount = (long) 24;
}
PagerModel pager = new PagerModel();
pager = PagerModel.getPager(&quot;acpager&quot;, 9, rowCount.intValue(), request);
pager.setRowCount(rowCount.intValue());
pager.navigate(request);
// Lấy danh s&#225;ch sản phẩm theo kiểu đặc biệt
String hql = &quot;FROM Product p WHERE p.status = 1 and p.special=1&quot;;
query = session.createQuery(hql);
query.setFirstResult(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
model.addAttribute(&quot;flat&quot;, &quot;type/special&quot;);
}
// Danh s&#225;ch h&#224;ng giảm gi&#225;
else if (value.equalsIgnoreCase(&quot;saleoff&quot;)) {
// Đếm sản phẩm theo loại giảm gi&#225; v&#224; ph&#226;n trang
String hqlCount = &quot;SELECT COUNT(p) FROM Product p WHERE p.status = 1 and p.discount &gt; 0 &quot;;
rowCount = (Long) session.createQuery(hqlCount).uniqueResult();
if (rowCount &gt; 24) {
rowCount = (long) 24;
}
PagerModel pager = new PagerModel();
pager = PagerModel.getPager(&quot;acpager&quot;, 9, rowCount.intValue(), request);
pager.setRowCount(rowCount.intValue());
pager.navigate(request);
// Lấy danh s&#225;ch sản phẩm theo giảm gi&#225;
String hql = &quot;FROM Product p WHERE p.status = 1 and p.discount &gt; 0 ORDER BY p.discount DESC &quot;;
query = session.createQuery(hql);
query.setFirstResult(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
model.addAttribute(&quot;flat&quot;, &quot;type/saleoff&quot;);
}
// Danh s&#225;ch h&#224;ng được y&#234;u th&#237;ch
else if (value.equalsIgnoreCase(&quot;favorite&quot;)) {
Map&lt;Integer, Integer&gt; map = new HashMap&lt;Integer, Integer&gt;();
String path = application.getRealPath(&quot;like.txt&quot;);
try {
@SuppressWarnings(&quot;resource&quot;)
Scanner scanner = new Scanner(new File(path));
String values = scanner.nextLine();
values = values.substring(1, value.length() - 1);
System.out.println(&quot;values:&quot; + values);
String[] keyValuePairs = values.split(&quot;,&quot;);
for (String pair : keyValuePairs) {
String[] entry = pair.split(&quot;=&quot;);
map.put(Integer.parseInt(entry[0].trim()), Integer.parseInt(entry[1].trim()));
rowCount++;
}
} catch (Exception e) {
e.printStackTrace();
}
Set&lt;Integer&gt; key = map.keySet();
String listId = key.toString();
listId = listId.substring(1, listId.length() - 1);
//
PagerModel pager = new PagerModel();
pager = PagerModel.getPager(&quot;acpager&quot;, 9, rowCount.intValue(), request);
pager.setRowCount(rowCount.intValue());
pager.navigate(request);
//
String hql = &quot;FROM Product p WHERE p.status = 1 and p.id IN(&quot; + listId + &quot;)&quot;;
query = session.createQuery(hql);
query.setFetchSize(pager.getStartRow());
query.setMaxResults(pager.getPageSize());
model.addAttribute(&quot;flat&quot;, &quot;type/favorite&quot;);
}
List&lt;Product&gt; list = query.list();
model.addAttribute(&quot;list&quot;, list);
return &quot;user/product-list&quot;;
}
// Gửi mail link sản phẩm cho bạn b&#232;
@RequestMapping(&quot;sendToFriend&quot;)
public String sendToFriend(ModelMap model, HttpServletRequest request, @RequestParam String id,
@RequestParam String from, @RequestParam String to, @RequestParam String subject,
@RequestParam String body) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
String url = request.getRequestURL().toString().replace(&quot;sendToFriend&quot;, &quot;detail/&quot; + id);
System.out.println(url);
String text = body + &quot;&lt;br/&gt; H&#227;y click v&#224;o đ&#226;y &lt;a href=&#39;&quot; + url + &quot;&#39;&gt;Xem sản phẩm &lt;/a&gt;&quot;;
helper.setText(text, true);
mailSender.send(message);
model.addAttribute(&quot;message&quot;, &quot;success&quot;);
} catch (Exception e) {
model.addAttribute(&quot;message&quot;, &quot;error&quot;);
}
return &quot;redirect:/user/product/detail/&quot; + id + &quot;.htm&quot;;
}
}

UserHome:

@SuppressWarnings(&quot;unchecked&quot;)
@ModelAttribute(&quot;saleOffProducts&quot;)
public List&lt;Product&gt; get9SaleOffProducts() {
Session session = sessionFactory.getCurrentSession();
String hql = &quot;FROM Product p WHERE p.status = 1 AND p.discount &gt; 0 ORDER BY p.discount DESC&quot;;
Query query = session.createQuery(hql);
query.setMaxResults(9);
return query.list();
}

Model:

package hieuboy.model;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name = &quot;Products&quot;,catalog = &quot;HieuBoyShop&quot;)
public class Product implements java.io.Serializable {
private static final long serialVersionUID = 4208468054445602750L;
private Integer id;
private Category category;
private Producer producer;
private String nameProduct;
private String photo;
private Integer quantity;
private Date productDate;
private String unitBrief;
private Double unitPrice;
private Double discount;
private String description;
private Integer views;
private Boolean available;
private Boolean special;
private Boolean latest;
private Boolean status;
private Collection&lt;OrderDetail&gt; orderDetail;
public Product() {
}
public Product(Integer id, Category category, Producer producer, String nameProduct, Integer quantity,
Date productDate, String unitBrief, Double unitPrice) {
this.id = id;
this.category = category;
this.producer = producer;
this.nameProduct = nameProduct;
this.quantity = quantity;
this.productDate = productDate;
this.unitBrief = unitBrief;
this.unitPrice = unitPrice;
}
public Product(Integer id, Category category, Producer producer, String nameProduct, String photo, Integer quantity,
Date productDate, String unitBrief, Double unitPrice, Double discount, String description, Integer views,
Boolean available, Boolean special, Boolean latest, Boolean status, Collection&lt;OrderDetail&gt; orderDetail) {
this.id = id;
this.category = category;
this.producer = producer;
this.nameProduct = nameProduct;
this.photo = photo;
this.quantity = quantity;
this.productDate = productDate;
this.unitBrief = unitBrief;
this.unitPrice = unitPrice;
this.discount = discount;
this.description = description;
this.views = views;
this.available = available;
this.special = special;
this.latest = latest;
this.status = status;
this.orderDetail = orderDetail;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = &quot;ID&quot;, unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = &quot;CategoryID&quot;, nullable = false)
public Category getCategory() {
return this.category;
}
public void setCategory(Category category) {
this.category = category;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = &quot;ProducerID&quot;, nullable = false)
public Producer getProducer() {
return this.producer;
}
public void setProducer(Producer producer) {
this.producer = producer;
}
@Column(name = &quot;NameProduct&quot;, nullable = false)
public String getNameProduct() {
return this.nameProduct;
}
public void setNameProduct(String nameProduct) {
this.nameProduct = nameProduct;
}
@Column(name = &quot;Photo&quot;)
public String getPhoto() {
return this.photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
@Column(name = &quot;Quantity&quot;, nullable = false)
public Integer getQuantity() {
return this.quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = &quot;dd/MM/yyyy&quot;)
@Column(name = &quot;ProductDate&quot;, nullable = false, length = 23)
public Date getProductDate() {
return this.productDate;
}
public void setProductDate(Date productDate) {
this.productDate = productDate;
}
@Column(name = &quot;UnitBrief&quot;, nullable = false)
public String getUnitBrief() {
return this.unitBrief;
}
public void setUnitBrief(String unitBrief) {
this.unitBrief = unitBrief;
}
@Column(name = &quot;UnitPrice&quot;, nullable = false, precision = 53, scale = 0)
public Double getUnitPrice() {
return this.unitPrice;
}
public void setUnitPrice(Double unitPrice) {
this.unitPrice = unitPrice;
}
@Column(name = &quot;Discount&quot;, precision = 53, scale = 0)
public Double getDiscount() {
return this.discount;
}
public void setDiscount(Double discount) {
this.discount = discount;
}
@Column(name = &quot;Description&quot;)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = &quot;Views&quot;)
public Integer getViews() {
return this.views;
}
public void setViews(Integer views) {
this.views = views;
}
@Column(name = &quot;Available&quot;)
public Boolean getAvailable() {
return this.available;
}
public void setAvailable(Boolean available) {
this.available = available;
}
@Column(name = &quot;Special&quot;)
public Boolean getSpecial() {
return this.special;
}
public void setSpecial(Boolean special) {
this.special = special;
}
@Column(name = &quot;Latest&quot;)
public Boolean getLatest() {
return this.latest;
}
public void setLatest(Boolean latest) {
this.latest = latest;
}
@Column(name = &quot;Status&quot;, nullable = false)
public Boolean getStatus() {
return this.status;
}
public void setStatus(Boolean status) {
this.status = status;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = &quot;product&quot;)
public Collection&lt;OrderDetail&gt; getOrderDetail() {
return orderDetail;
}
public void setOrderDetail(Collection&lt;OrderDetail&gt; orderDetail) {
this.orderDetail = orderDetail;
}
}

Mysql:

CREATE TABLE `products` (
`ID` int NOT NULL AUTO_INCREMENT,
`NameProduct` varchar(50) NOT NULL,
`Photo` varchar(255) DEFAULT NULL,
`Quantity` int NOT NULL,
`ProductDate` datetime NOT NULL,
`UnitBrief` varchar(50) NOT NULL,
`UnitPrice` float NOT NULL,
`Discount` float DEFAULT NULL,
`Description` varchar(1000) DEFAULT NULL,
`Views` int DEFAULT NULL,
`Available` bit(1) DEFAULT NULL,
`Special` bit(1) DEFAULT NULL,
`Latest` bit(1) DEFAULT NULL,
`Status` bit(1) NOT NULL,
`CategoryID` int NOT NULL,
`ProducerID` int NOT NULL,
PRIMARY KEY (`ID`),
KEY `FK_Products_Categories` (`CategoryID`),
KEY `FK_Products_Producers` (`ProducerID`),
CONSTRAINT `FK_Products_Categories` FOREIGN KEY (`CategoryID`) REFERENCES `categories` (`ID`),
CONSTRAINT `FK_Products_Producers` FOREIGN KEY (`ProducerID`) REFERENCES `producers` (`ID`)
)

We hope to help, thank you so much.

答案1

得分: 0

UserHome中删除query.setMaxResults(9);,显然查询构造有误 - 数字9出现在错误的位置。

英文:

Remove query.setMaxResults(9); from UserHome, apparently the query is constructed wrong - that number 9 ends up in wrong place.

huangapple
  • 本文由 发表于 2020年9月30日 18:56:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64136121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定