java.sql.SQLSyntaxErrorException:'字段列表'中的未知列

时间:2016-08-15 01:48:53

标签: java hibernate

我正在尝试使用Hibernate处理OneToMany关系。我正在使用@Temporal注释来告诉hibernate有关数据字段的信息。我不知道为什么我在这里收到此错误。看起来日期格式有问题。请让我知道如何解决它。

客户

package regular;

import java.util.List;

import javax.persistence.CascadeType;
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.OneToMany;

@Entity
public class Customers {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int customer_id;
    private String customerName;
    private String contactName;
    private String address;
    private String city;
    private String postalCode;
    private String country;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    @JoinColumn(name = "customer_id")
    private List<Orders> order;

    public Customers() {
    }

    public List<Orders> getOrder() {
        return order;
    }

    public void setOrder(List<Orders> order) {
        this.order = order;
    }

    public Customers(String customerName, String contactName, String address, String city, String postalCode,
            String country, List<Orders> order) {
        this.customerName = customerName;
        this.contactName = contactName;
        this.address = address;
        this.city = city;
        this.postalCode = postalCode;
        this.country = country;
        this.order = order;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public int getCustomer_id() {
        return customer_id;
    }

    @Override
    public String toString() {
        return "Customers [customer_id=" + customer_id + ", customerName=" + customerName + ", contactName="
                + contactName + ", address=" + address + ", city=" + city + ", postalCode=" + postalCode + ", country="
                + country + ", order=" + order + "]";
    }

}

订单

package regular;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class Orders {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int orderId;

    @Temporal(value = TemporalType.TIMESTAMP)
    private Date orderDate;

    private String productName;
    private int quantity;

    public Orders(String productName, int quantity) {
        this.orderDate = new Date();
        this.productName = productName;
        this.quantity = quantity;
    }

    public Orders() {
    }

    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getOrderId() {
        return orderId;
    }

    @Override
    public String toString() {
        return "Orders [orderId=" + orderId + ", orderDate=" + orderDate + ", productName=" + productName
                + ", quantity=" + quantity + "]";
    }

}

转轮

package regular;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Runner {

    public static void main(String[] args) {

        SessionFactory sessionFactory = new Configuration().configure("/regular/hibernate.cfg.xml")
                .addAnnotatedClass(Customers.class).addAnnotatedClass(Orders.class).buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Customers customer = new Customers();

        customer.setCustomerName("Robert Bosch");
        customer.setAddress("404 California Ave");
        customer.setCity("California");
        customer.setPostalCode("60466");
        customer.setCountry("USA");

        List<Orders> orders = new ArrayList<>();

        orders.add(new Orders("Car", 4));
        orders.add(new Orders("Headphones", 6));

        customer.setOrder(orders);

        session.save(customer);

        session.close();
    }

}

错误

Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'orderDate' in 'field list'
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:686)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:663)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:653)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115)
    at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2041)
    at com.mysql.cj.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1827)
    at com.mysql.cj.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2041)
    at com.mysql.cj.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:1977)
    at com.mysql.cj.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:4963)
    at com.mysql.cj.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1962)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
    ... 41 more

7 个答案:

答案 0 :(得分:2)

我的表名和数据库看起来有问题。我更改了表名,但它确实有效。谢谢大家!

答案 1 :(得分:1)

hibernate 默认将驼峰式字母转换为下划线。因此,您要么更改表中的列以反映这一点,要么更改休眠命名策略。

答案 2 :(得分:0)

我遇到了类似的问题,由于这篇文章,我查看了列名。就我而言,我有错字

@ManyToOne
@JoinColumn(name = "client", referencedColumnName = "id")
private Client client;

应该是哪个

@ManyToOne
@JoinColumn(name = "client_id", referencedColumnName = "id")
private Client client;

答案 3 :(得分:0)

有时在将数据插入表中时,您正在插入其他表中,但是在查询中,您正在写入其他表的名称。

此答案或多或少与问题作者的答案相符。

答案 4 :(得分:0)

我也面临类似的问题。我有一个名为

的字段
@Column(name = "CountryCode") 
 private String CountryCode

显然,它是作为country_code而不是countrycode(这是表中的实际列)。我将其更改为,并且有效:

 @Column(name = "countrycode") 
     private String CountryCode

因此,请确保只以大写字母开头,并且不要在列名注释中包含其他任何字母,因为它会使用_分解并使用第二个大写字母作为分隔符来插入_。

答案 5 :(得分:0)

以下链接可能对某些用户有帮助:

Hibernate field naming issue with Spring Boot (naming strategy)

在application.properties文件中添加以下内容:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

答案 6 :(得分:0)

如果使用新变量更新了 java 模型类并且您没有自动生成数据库表,请确保相应地更新数据库表中的列。