Не удается сопоставить OneToMany и ManyToOne для второго случая? - PullRequest
0 голосов
/ 03 октября 2019

Я пытаюсь сохранить коллекцию заказов, сделанных пользователем. У одного пользователя может быть много заказов, но у одного заказа есть только один пользователь. Таким образом, 1-М отношения сформированы. Такая же связь существует и между Items и ItemOrders, ItemOrder состоит из Item и количества товаров для заказа. Итак, у ItemOrder есть один Item, но у одного Item может быть много ItemOrders.

В моем тестовом наборе мне удалось правильно создать Items и ItemOrders. Однако, когда я расширяю тест, чтобы также создавать пользователей и заказы, я получаю исключение SQLSyntaxErrorException для «Заказа» ... Это странно, поскольку для достижения обоих результатов должна быть точно такая же процедура ... И я не могу понять, чтоЯ делаю что-то не так, есть идеи?

@Entity
public class ItemEntity implements EntityInt {
    @Id
    @GeneratedValue(generator = "incrementor")
    @Column(name = "item_id", unique = true)
    public int id;

    @OneToMany(mappedBy="item")
    public Set<OrderItemEntity> orderItems = new HashSet<>();

}

@Entity
public class OrderItemEntity implements EntityInt {

    @Id
    @GeneratedValue(generator = "incrementor")
    @Column(name = "order_item_id", unique = true)
    public int id;

    @Column(name = "amount", nullable = false)
    public int amount;

    @ManyToOne
    @JoinColumn(name="item_id", nullable = false)
    private ItemEntity item;

    public OrderItemEntity(ItemEntity item, int amount) {
        this.setItem(item);
        this.amount = amount;
    }

    public void setItem(ItemEntity item) {
        if (this.item != null)
            this.item.orderItems.remove(this);
        this.item = item;
        this.item.orderItems.add(this);
    }
}

@Entity
public class OrderEntity implements EntityInt {

    @Id
    @GeneratedValue(generator = "incrementor")
    @Column(name = "order_id", unique = true)
    public int id;

    @ManyToOne
    @JoinColumn(name="user_id", nullable = false)
    public UserEntity user;

    public UserEntity getUser() {
        return user;
    }

    public void setUser(UserEntity user) {
        if (this.user != null)
            this.user.orders.remove(this);
        this.user = user;
        this.user.orders.add(this);
    }
}

@Entity
public class UserEntity implements EntityInt {

        @Id
        @GeneratedValue(generator = "incrementor")
        @Column(name = "user_id", unique = true)
        public int id;

        @OneToMany(mappedBy="user")
        public Set<OrderEntity> orders = new HashSet<>();

}

TEST

@Test
    public void testItemOrderEntity() throws Exception {
         EntityManagerFactory factory = BasicDao.getEntityManagerFactory();
    EntityManager em = factory.createEntityManager();

    em.getTransaction().begin();

    String itemName = Math.random() + "";
    ItemEntity item = new ItemEntity(itemName, 0, 0);
    em.persist(item);

    OrderItemEntity orderItem = new OrderItemEntity(item, 5);
    em.persist(orderItem);

    String uname = "" + Math.random();
    UserEntity user = new UserEntity(uname, uname);
    em.persist(user);

    // Error
    OrderEntity order = new OrderEntity(user);
    em.persist(order);
    em.getTransaction().commit();
    }

EntityInt содержит методы, используемые в основном BasicDao для выполнения операций CRUD. Он имеет такие вещи, как getId (), getVersion (), createVerifyIsUnqieuQuery () и так далее.

BasicDao является основным классом доступа к репозиторию и расширяется itemsDao, UsersDao и так далее. Это соответствующие части BasicDao, которые используются в тестовом примере:

BasicDao.java

private static final String PERSISTENCE_UNIT_NAME = "org.hibernate.lab1_web_shop.jpa";

    public static EntityManagerFactory getEntityManagerFactory() {
        return  Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    }

    public static EntityInt insert(EntityInt entity) throws Exception {
        EntityManagerFactory factory = getEntityManagerFactory();
        EntityManager em = factory.createEntityManager();
        try {
            em.getTransaction().begin();
            Query isUniqueQuery = entity.createVerifyIsUniqueQuery(em);
            if (isUniqueQuery != null) {
                List<EntityInt> resultList = isUniqueQuery.getResultList();
                if (resultList.size() == 0) {
                    entity.beforeInsert(em);
                    em.persist(entity);
                } else {
                    entity = null;
                }
            } else {
                // There is no uniqueness filter so we insert it as is
                entity.beforeInsert(em);
                em.persist(entity);
            }
            em.getTransaction().commit();
            // Returns the persistent entity along with any database modified attributes
            return entity;
        } catch (Exception e) {
            em.getTransaction().rollback();
            throw new Exception(e);
        } finally {
            em.close();
        }
    }

Обратите внимание, что beforeInsert () на самом деле не используетсялица, используемые в тесте.

Это трассировка стека:

Caused by: javax.persistence.RollbackException: Error while committing the transaction
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
Caused by: java.sql.SQLSyntaxErrorException: 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_id, version, order_id) values (9, 0, 10)' at line 1

Я также тестировал удаление BasicDao и просто делал все за один коммит, и все еще воспроизводил ту же ошибку.

1 Ответ

1 голос
/ 03 октября 2019

"У вас есть ошибка в вашем синтаксисе SQL; проверьте правильность синтаксической ошибки в руководстве, соответствующем вашей версии сервера MySQL" Hibernate 4

Проблема заключалась в том, что я назвал свойТаблица «Порядок» оказывается. Который был получен из трассировки стека выше.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...