i've been trying to solve a problem but got stuck now.
I have 2 entity classes/tables Order and OrderItem. OrderItem has 1 Order and Order has 1..* OrderItems.My EJB works fine, it adds order, gets its' id and adds order's items
Order1 order = addOrder(project, name);
em.flush();
int orderId = order.getOrder();
addOrderItem(orderId, cart);
So everything works fine, the order id is inserted also in order item row as needed.
But now i want to perform JPQL with many mapped entities, Order and OrderItem included. As i know to perform a query i need to map my entities first, and so the problem is in my mappings.
Here is the mappingpublic class Order1 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "order_")
private Integer order;
...
@OneToMany(mappedBy="orders",targetEntity=OrderItem.class,
fetch=FetchType.EAGER, cascade = CascadeType.ALL)
private List<OrderItem> orderItems;
...
}
public class OrderItem implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "order_item")
private Integer orderItem;
@Column(name = "order_" /*, insertable=false, updatable=false*/)
private BigInteger order;
...
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="order_")
private Order1 orders;
...
}
With this kind of mapping i get exception at compiletime
Exception Description: Multiple writable mappings exist for the field [order_item.order_]. Only one may be defined as writable, all others must be specified read-only.
I've googled and found out that i have to make the order column in OrderItem non-writable. As you can see in the code, i've done that and it compiles, but when i place an order in my application, then order id into order_item's order column is not inserted and is NULL. But i can't leave it so, there must be an order reference in order_item.
My JPQL query is createQuery("SELECT o.order FROM OrderItem oi JOIN oi.orders o");
It works when I make order column in order_item table uninsertable, but it can't be left that way. What are the suggestions? What is wrong in my mapping and why can't i have order_ in order_item insertable?
update about @toto answer:
My entities were generated by Netbeans wizard. If i won't define BigInteger order
in OrderItem
then i couldn't insert order id
into my order_item
table.
My EJB methods are following:
public Order1 addOrder(BigInteger project, BigInteger name) {
Order1 order = new Order1();
...
em.persist(order);
return order;
}
private void addOrderItem(int orderId, ProjectCart cart) {
// convert orderId int to BigInteger
BigInteger oId = new BigInteger(String.valueOf(orderId));
OrderItem orderItem = new OrderItem();
orderItem.setOrder(oId);
....
em.persi开发者_StackOverflow中文版st(orderItem);
}
So this means if i leave out from OrderItem
@Column(name = "order_")
private BigInteger order1;
my getters and setters for order1
inside OrderItem
won't work and i can't orderItem.setOrder(oId)
in addOrderItem()
method simply cause my order1
is not defined in OrderItem
. Other thing is that i don't have foreign keys in my datamodel, but i think it's not a concern, because in mappings i have set join columns.
You must not define a column BigInteger order
of name order
inside OrderItem
. It will be done automatically (in the background) by JPA. The compiler complains because you are creating a column that has been created by JPA already.
精彩评论