真的很感謝任何幫助(至少如何跟蹤問題的根本原因),因爲我已經與此戰鬥了好幾天,甚至找不到解決方法。這個問題本身:我有一些實體,它們都工作得很好 - persist(),find()等等,除了一個創建兩個不同實體的方法(Order和Items,一個Order可以有很多Items) 。在調用em.persist(..)命令被保存後,我看到它的數據庫生成的ID,項目被保存到數據庫(我通過直接在數據庫中看到它),但它顯示ID = 0。無論我做什麼,它總是0(例如,當我打開訂單,我仍然看到它的ID = 0),直到我重新啓動服務器 - 然後它顯示項目的正確ID。該方法的 代碼(後登錄我加入的實際值我得到):TopLink EntityManager不能正確保存對象
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
}
Order類:
@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方法,使其更具可讀性): @Entity @Table(name =「items」) public class Item implements 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"
,但它並沒有幫助
由於私有方法,但它並不能幫助。無論如何,訂單(和其他實體)具有int作爲id,它的工作原理。現在我添加了方法,它可以從項目中找到最大ID,並在持久化之前將遞增值設置爲itemID,但它工作正常,但當我看到它時,眼睛流血很難看:) – user2676881