【发布时间】:2019-06-12 14:47:07
【问题描述】:
我正在使用 Spring、Hibernate 和 JPA 开发一个简单的 CRUD 项目。
这是一个基本的优惠券系统,优惠券对象有参数,其中之一是优惠券类型。在绑定到服务器的 jsp 中输入 vales 时:
<h1> Create Coupon </h1>
<form:form action="company/create" method="POST" modelAttribute="theCoupon">
<input name="title"/>
<input name="startDate"/>
<input name="endDate"/>
<input name="amount"/>
<input name="message"/>
<input name="price"/>
<input name="image"/>
<select name="couponType">
<option>SPORTS</option>
<option>GAMING</option>
</select>
<input type="submit" value="submit">
</form:form>
这是控制器:
@PostMapping("/add")
public String newCoupon(Model theModel) {
List<CouponType> couponType = new ArrayList<CouponType>( Arrays.asList(CouponType.values()));
System.out.println(couponType);
theModel.addAttribute("couponType", couponType);
theModel.addAttribute("theCoupon", new Coupon());
return "add";
}
@RequestMapping("/create")
public String add(@ModelAttribute("theCoupon") Coupon theCoupon) {
theCoupon.setId(0);
System.out.println(theCoupon);
couponService.save(theCoupon);
return "savedCoupon";
}
我收到此错误:
java.sql.SQLSyntaxErrorException:“字段列表”中的未知列“coupon0_.coupon_type”
这是数据库结构的截图,名称相同我不知道是什么问题。
另外,这里是优惠券pojo:
@Entity
@Table(name = "coupon")
public class Coupon {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "title")
private String title;
@Column(name = "startDate")
private String startDate;
@Column(name = "endDate")
private String endDate;
@Column(name = "amount")
private int amount; // decrease amount on every customer purchase
@Column(name = "couponType")
private String couponType;
@Column(name = "message")
private String message;
@Column(name = "price")
private double price;
@Column(name = "image")
private String image;
public Coupon(long id, String title, String startDate, String endDate, int amount, String couponType,
String message, double price, String image) {
super();
this.id = id;
this.title = title;
this.startDate = startDate;
this.endDate = endDate;
this.amount = amount;
this.couponType = couponType;
this.message = message;
this.price = price;
this.image = image;
}
public Coupon() {
super();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getCouponType() {
return couponType;
}
public void setCouponType(String couponType) {
this.couponType = couponType;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@Override
public String toString() {
return "Coupon [id=" + id + ", title=" + title + ", startDate=" + startDate + ", endDate=" + endDate
+ ", amount=" + amount + ", couponType=" + couponType + ", message=" + message + ", price=" + price
+ ", image=" + image + "]";
}
}
希望你们中的任何人都能发现问题,任何帮助将不胜感激!
【问题讨论】:
-
就这么简单
coupon0_不是你的表名 -
@RiggeFolly, coupon0_ 应该是 hibernate 为查询生成的名称。我建议停止服务器,清理,重新编译和重新部署。
-
谢谢大家,我清理并更改了一堆东西,所以我不记得优惠券0 问题的解决方案是什么,但我确实解决了优惠券类型问题,发布了一个为我修复它的答案,谢谢帮助!
标签: java mysql hibernate spring-mvc