尝试将数据添加到数据库时的未知列

时间:2019-06-12 14:47:07

标签: java mysql hibernate spring-mvc

我正在使用Spring,Hibernate和JPA进行一个简单的CRUD项目。 它是一个基本的优惠券系统,优惠券对象具有参数,其中之一是优惠券类型, 在绑定到服务器的jsp中输入值时:

<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: Unknown column 'coupon0_.coupon_type' in 'field list'

这是数据库结构的图片,名称相同,我不知道出了什么问题。

enter image description here

此外,这是优惠券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 ammount on ever 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 + "]";
}

}

希望任何人都能发现问题,我们将不胜感激!

1 个答案:

答案 0 :(得分:0)

[已解决]

问题是我在var中使用了两个单词,例如couponType,在数据库中将是coupon_type。

将其更改为仅键入pojo和数据库,现在可以正常使用了!

希望这对遇到此问题的人有所帮助。