无法将一对多和多对一映射到第二种情况?



我正在尝试存储用户下达的订单集合。一个用户可能有多个订单,但一个订单只有一个用户。因此形成了1-M关系。Items 和 ItemOrders 之间也存在相同的关系,ItemOrder 由一个 Item 和要订购的 Items 数量组成。因此,一个 ItemOrder 有一个 Item,但一个 Item 可以有多个 ItemOrders。

在我的测试套件中,我已经设法正确地创建了项目和项目订单。但是,当我扩展测试以创建用户和订单时,我得到一个 SQLSyntaxErrorException for "Order"...这很奇怪,因为它应该是实现两种结果的完全相同的过程......而且我不知道我在这里做错了什么,有什么想法吗?

@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
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 的相关部分:

基本道.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,并在一次提交中完成所有操作,但我仍然重现相同的错误。

"您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本对应的手册以获取正确的语法错误" 休眠 4

问题是我将我的表命名为"订单"它似乎。这是从上面的堆栈跟踪中获得的。

相关内容

最新更新