Java Can';第二个病例一个多个一个?
我正在尝试存储用户的订单集合。一个用户可能有多个订单,但一个订单只有一个用户。因此形成了1-M关系。Items和ItemOrders之间也存在相同的关系,ItemOrder由一个Item和要订购的Item数量组成。因此,一个ItemOrder有一个项目,但一个项目可以有多个ItemOrder 在我的测试套件中,我成功地正确地创建了Items和ItemOrders。然而,当我扩展测试以同时创建用户和订单时,我得到了“订单”的SQLSyntaxErrorException。。。这很奇怪,因为它应该是完全相同的程序来实现这两个结果。。。我不知道我做错了什么,有什么想法吗Java Can';第二个病例一个多个一个?,java,hibernate,Java,Hibernate,我正在尝试存储用户的订单集合。一个用户可能有多个订单,但一个订单只有一个用户。因此形成了1-M关系。Items和ItemOrders之间也存在相同的关系,ItemOrder由一个Item和要订购的Item数量组成。因此,一个ItemOrder有一个项目,但一个项目可以有多个ItemOrder 在我的测试套件中,我成功地正确地创建了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<>();
}
EntityInt包含基本DAO主要用于执行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();
}
}
我还测试了删除BasicDao并在一次提交中完成所有操作,但仍然会重复相同的错误。
问题是,我将表命名为“Order”。这是从上面的堆栈跟踪中获得的。我用一个小小的spring启动应用程序重现了您的实体和测试:一切正常!您的帖子需要补充:例如,整个stacktrace。此外,您没有解释什么是EntityInt,基本DAO是如何实现的,等等。感谢您为我检查这一部分。我会调查一下BasicDao,看看是否有错误。我已经附加了更多与测试相关的部分以及堆栈跟踪中的原因。
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();
}
}
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