非常感谢任何帮助(至少如何追查问题的根本原因),因为我已经和它斗争了好几天,甚至找不到解决方法。
问题本身:我有一些实体,它们都运行良好 - persist(),find()等,除了一种方法,我创建两个不同的实体(订单和项目,一个订单可以有很多项目)。在调用em.persist(..)后,命令被保存,我看到它由DB生成的id,项目被保存到DB(我直接在DB中通过SELECT看到它),但它显示ID = 0。无论我做什么它总是0(例如,当我打开订单时,我仍然看到它的ID = 0),直到我重新启动服务器 - 然后它显示正确的项目ID。 方法代码(记录后我添加了实际值):
public void createOrderFromItems(ArrayList<TehnomirItemDTO> items, User user) {
Order ord = new Order();
User managers = getUserByEmail(Constants.ALL_MANAGERS_GROUP);
ord.setAssignedTo(managers);
Date date = new Date();
ord.setCreatedOn(date);
User customer = user;
ord.setCustomer(customer);
BigDecimal custBalance = new BigDecimal(0);
ArrayList<Balance> balances = getBalanceForUser(customer);
for (Balance b:balances) {
custBalance.add(b.getAmount());
}
logger.debug("before1. order: "+ord.getOrderId()); //here I get 0
em.persist(ord);
logger.debug("before2. order: "+ord.getOrderId()); //still 0
State new_state = getStateByName(SharedConstants.STATE_NEW);
logger.debug("before3. order: "+ord.getOrderId()); //here I get actual ID, generated by DB, e.g. 189
State overpriced = getStateByName(SharedConstants.STATE_LIMIT_EXCEEDED);
ArrayList<Item> itemList = new ArrayList<Item>();
for (TehnomirItemDTO tid:items) {
Item item = new Item(tid);
item.setOrder(ord);
logger.debug("order inside2:"+ord.getOrderId()); //again, actual ID
item.setPriceInt(tid.getPrice_int());
custBalance = custBalance.subtract(item.getPriceInt());
if (custBalance.floatValue()>0) {
item.setStateBean(new_state);
} else item.setStateBean(overpriced);
logger.debug("item before:"+item.getItemId()); //here I get 0
em.persist(item);
item = em.merge(item);
em.setFlushMode(FlushModeType.COMMIT);//added just in case it would work but it didn't
em.flush();//same as previous line
Item tst = getItemByID(1);
logger.debug("item after:"+item.getItemId()+" ord:"+ord.getOrderId()); //again, orderID is correct, itemID is 0
itemList.add(item);
}
ord.setItems(itemList);
State new_state2 = getStateByName(SharedConstants.STATE_NEW);
logger.debug(ord.getItems().get(0).getItemId()+" order: "+ord.getOrderId());//again, orderID is correct, itemID is 0
}
订单类:
@Entity
@Table(name="orders")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY/*, generator="ORDERS_ORDERID_GENERATOR"*/)
@Column(name="ORDER_ID")
private int orderId;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="CREATED_ON")
private Date createdOn;
//bi-directional many-to-one association to Item
@OneToMany(mappedBy="order")
private List<Item> items;
//uni-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="ASSIGNED_TO")
private User assignedTo;
//uni-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="CUSTOMER")
private User customer;
public Order() {
}
public int getOrderId() {
return this.orderId;
}
}
项目类(删除了getter和setter以使其更具可读性): @实体 @table(名称= “项目”。) public class Item实现Serializable { private static final long serialVersionUID = 1L;
@Id
@Column(name="ITEM_ID")
private int itemId;
private String code;
private BigDecimal weight;
public BigDecimal getWeight() {
return weight;
}
public void setWeight(BigDecimal weight) {
this.weight = weight;
}
private String comments;//any additional info user'd like to add
private String description;
@Column(name="EXT_ID")
private int extId;
private String manufacturer;
@Column(name="PRICE_EXT")
private BigDecimal priceExt;
@Column(name="PRICE_INT")
private BigDecimal priceInt;
private String region;
private String term;
//bi-directional many-to-one association to Order
@ManyToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="ORDER_ID")
private Order order;
//bi-directional many-to-one association to State
@ManyToOne
@JoinColumn(name="STATE")
private State state;
}
我对缓存有一些想法,所以我添加到了persistence.xml行
property name="toplink.cache.type.default" value="NONE"
property name="toplink.cache.type.Order" value="NONE"
但它没有任何帮助
答案 0 :(得分:0)
尝试将int
更改为Integer
private Integer orderId;
以及吸气剂和制定者。
答案 1 :(得分:0)
您提到项目由数据库分配了ID值,但错过了订购时的@GeneratedValue(strategy = GenerationType.IDENTITY)注释。这是告诉JPA db控制值的原因,否则它期望应用程序设置它,使其保持默认为0.
答案 2 :(得分:0)
调用em.persist(obj)后,调用em.flush();.它应该工作。
更好的私有方法保存 喜欢
private void save(object obj)
{
em.persist(obj);
em.flush();
}