实体在尝试提交事务时导致Mysql语法错误

时间:2019-05-15 13:46:29

标签: java mysql jpa syntax

我正在尝试将实体的数据库放入数据库中,但是当我尝试将其持久存储在数据库中时,其中一个实体触发了Mysql语法错误

我已将其范围缩小到我的Order实体,即与之混淆的实体,请帮助。

   @Entity
public class Order implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne(cascade = CascadeType.PERSIST)
    private User user;

    @OneToMany(cascade = CascadeType.ALL)
    private final List<Vehicle> vehicle = new ArrayList();

    public Order() {
    }

    public Order(int id, User user) {
        this.id = id;
        this.user = user;
    }

    public Order(User user) {
        this.user = user;
    }
    //....more getters and setters

触发语法错误的Order实体。


   public static void main(String[] args) {

   EntityManager em = PuSelector.getEntityManagerFactory("pu").createEntityManager();


        Vehicle v = new Vehicle("dd 12 312", "Volvo", "gas", "shift", 1998, "SUV", 5, 5, 1500);
        Vehicle v2 = new Vehicle("aa 16 376", "Volvo", "diesel", "auto", 1990, "SUV", 5, 5, 1000);

        em.getTransaction().begin();

    Role userRole = new Role("user");
    Role adminRole = new Role("admin");

    User user = new User("user", "test");
    user.addRole(userRole);

    User admin = new User("admin", "test");
    admin.addRole(adminRole);

    User both = new User("user_admin", "test");
    both.addRole(userRole);
    both.addRole(adminRole); 

    em.persist(userRole);
    em.persist(adminRole);
    em.persist(user);
    em.persist(admin);
    em.persist(both);

    User p = new User("Magnus", "123123");
    p.addRole(adminRole);

        Order o = new Order(p);
        o.addVehicle(v);
        o.addVehicle(v2);

        em.persist(v2);
        em.persist(v);
        em.persist(o);

        em.getTransaction().commit();

@Entity
@Table(name = "users")
public class User implements Serializable
{

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "user_name", length = 25)
    private String userName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "user_pass")
    private String userPass;
    @JoinTable(name = "user_roles", joinColumns =
    {
        @JoinColumn(name = "user_name", referencedColumnName = "user_name")
    }, inverseJoinColumns =
    {
        @JoinColumn(name = "role_name", referencedColumnName = "role_name")
    })
    @ManyToMany
    private List<Role> roleList = new ArrayList();

这是用户,如果需要的话。

也是Stracktrace

[EL Info]: 2019-05-15 15:32:04.809--ServerSession(1903051549)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
Wed May 15 15:32:05 CEST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Wed May 15 15:32:05 CEST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
[EL Info]: connection: 2019-05-15 15:32:05.26--ServerSession(1903051549)--file:/C:/Users/Magnus West Madsen/Documents/NetBeansProjects/CA3/target/classes/_DO_NOT_RENAME_ME_url=jdbc:mysql://localhost:3306/seed?zeroDateTimeBehavior=convertToNull_user=root login successful
[EL Warning]: 2019-05-15 15:32:05.325--ServerSession(1903051549)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: 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 'ORDER (ID INTEGER AUTO_INCREMENT NOT NULL, USER_user_name VARCHAR(25), PRIMARY K' at line 1
Error Code: 1064
Call: CREATE TABLE ORDER (ID INTEGER AUTO_INCREMENT NOT NULL, USER_user_name VARCHAR(25), PRIMARY KEY (ID))
Query: DataModifyQuery(sql="CREATE TABLE ORDER (ID INTEGER AUTO_INCREMENT NOT NULL, USER_user_name VARCHAR(25), PRIMARY KEY (ID))")
[EL Warning]: 2019-05-15 15:32:05.355--ServerSession(1903051549)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: 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 'ORDER ADD CONSTRAINT FK_ORDER_USER_user_name FOREIGN KEY (USER_user_name) REFERE' at line 1
Error Code: 1064
Call: ALTER TABLE ORDER ADD CONSTRAINT FK_ORDER_USER_user_name FOREIGN KEY (USER_user_name) REFERENCES users (user_name)
Query: DataModifyQuery(sql="ALTER TABLE ORDER ADD CONSTRAINT FK_ORDER_USER_user_name FOREIGN KEY (USER_user_name) REFERENCES users (user_name)")
[EL Warning]: 2019-05-15 15:32:05.851--UnitOfWork(1976870927)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: 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 'ORDER (USER_user_name) VALUES ('Magnus')' at line 1
Error Code: 1064
Call: INSERT INTO ORDER (USER_user_name) VALUES (?)
    bind => [1 parameter bound]
Query: InsertObjectQuery(Entitys.Order[ id=0 ])
Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: 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 'ORDER (USER_user_name) VALUES ('Magnus')' at line 1
Error Code: 1064
Call: INSERT INTO ORDER (USER_user_name) VALUES (?)
    bind => [1 parameter bound]
Query: InsertObjectQuery(Entitys.Order[ id=0 ])
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)
    at utils.SetupTestUsers.main(SetupTestUsers.java:56)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: 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 'ORDER (USER_user_name) VALUES ('Magnus')' at line 1
Error Code: 1064

0 个答案:

没有答案