eclipse—如何使用外键持久化对象来避免javaee/jpa错误

c9qzyr3d  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(300)

当前在javaee中创建@joincolumn时遇到问题,其中新列是一个对象,这会导致运行时错误。对象主要由字符串和整数组成。我不确定,但我相信外键是持久的,它创建的列比我为它专门创建的一列多。有什么办法解决这个问题吗?下面是错误。

[EL Warning]: 2020-09-04 01:14:18.148--ServerSession(843512726)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "ORDER" at line 1, column 14.
Error Code: 30000
Call: CREATE TABLE ORDER (ID BIGINT NOT NULL, ORDERDESCRIPTION VARCHAR(2000), TIMECREATED TIME, CAR_ID BIGINT, PRIMARY KEY (ID))
Query: DataModifyQuery(sql="CREATE TABLE ORDER (ID BIGINT NOT NULL, ORDERDESCRIPTION VARCHAR(2000), TIMECREATED TIME, CAR_ID BIGINT, PRIMARY KEY (ID))")
[EL Warning]: 2020-09-04 01:14:18.163--ServerSession(843512726)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "ORDER" at line 1, column 13.
Error Code: 30000
Call: ALTER TABLE ORDER ADD CONSTRAINT FK_ORDER_CAR_ID FOREIGN KEY (CAR_ID) REFERENCES CAR (ID)
Query: DataModifyQuery(sql="ALTER TABLE ORDER ADD CONSTRAINT FK_ORDER_CAR_ID FOREIGN KEY (CAR_ID) REFERENCES CAR (ID)")

代码如下:

@Entity
@NamedQueries({
    @NamedQuery(name = "findAllCustomers", query = "SELECT c FROM Customer c"),
    @NamedQuery(name = "findCustomerByName", query = "SELECT c FROM Customer c WHERE c.firstName = :firstName AND c.lastName = :lastName"),
})
public class Customer implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;
    private String officePhone;
    private String mobile;
    private String email;

    @JoinColumn
    @OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
    private List<Order> orders = new ArrayList<Order>();

    public Long getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getOfficePhone() {
        return officePhone;
    }

    public void setOfficePhone(String officePhone) {
        this.officePhone = officePhone;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

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

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

    @Override
    public String toString() {
        return "ass2.Customer[ id=" + id + " firstName=" + firstName + " lastName=" + lastName + " officePhone=" + officePhone + " mobile=" + mobile + " email=" + email + " orders=" + orders + " ]";
    }

}
9jyewag0

9jyewag01#

Order 是保留的sql关键字,您需要为 Order 类,例如:

@Table(name = "client_order")
public class Order { ...

相关问题