gpt4 book ai didi

java - 无法为第二种情况映射 OneToMany 和 ManyToOne?

转载 作者:行者123 更新时间:2023-12-02 09:27:47 25 4
gpt4 key购买 nike

我正在尝试存储用户发出的订单的集合。一个用户可能有多个订单,但一个订单只有一个用户。这样就形成了1-M关系。 Items 和 ItemOrders 之间也存在相同的关系,ItemOrder 由一个 Item 和一定数量的要订购的 Items 组成。因此,一个 ItemOrder 有一个 Item,但一个 Item 可以有多个 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<>();

}

测试

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

最佳答案

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax error" Hibernate 4

问题是我将我的表命名为“Order”。这是从上面的堆栈跟踪中获得的。

关于java - 无法为第二种情况映射 OneToMany 和 ManyToOne?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58216153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com