-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: em.persist(Order) does not work
PostPosted: Thu Dec 20, 2007 12:50 pm 
Newbie

Joined: Thu Dec 20, 2007 9:26 am
Posts: 3
Hi,

I try to implement an order in a webshop.
Therefore i have an relation Orders, an relation orderpositions and an
relation article.
The table orderpositions has the columns: artnr(Foreign key. PK in article),
ordnr(Foreign key. PK in Orders), posnr(Primary key) and quantity.
The ordnr is an autogenerated value(in Orders).

When I try to persist an Order (entitymanager.persist(order)), hibernate should
persist the positions automatically at the same transaction(cascade={Persist}).

The Problem is, when hibernate is trying to persist the positions it has
a null value at ordnr.
But I think he should automatically take the new generated Value from Oders.

The Errormsg:

Caused by: java.sql.BatchUpdateException: Batch-Eintrag 0 insert into postgres.orderpositions (quantity, artnr, ordnr, posnr) values (1, 51, NULL, 15) was cancelled.

Mapping:

orderpositions:

@ManyToOne
@JoinColumn(name="ordnr")
private Order order;

@ManyToOne
@JoinColumn(name="artnr")
private Article article;

Orders:

@OneToMany(mappedBy="order",cascade={ALL})
private List<orderpositions> orderpositionsCollection = new ArrayList<orderpositions>();

Can anyone help me ??

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 8:28 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Without seeing your code it is not obvious what is going wrong but remember that while in the database a foreign key is a two way association in your Java model it is not. This means simply adding an OrderPosition object to the list associated with an order is not enough. You also have to set the Order on the OrderPosition. This is typically referred to as needing 'scaffolding' code to maintain two way relations ships.

So try this in your Order class unless you do it already:

Code:
void addOrderPosition(OrderPosition op) {
  orderpositionsCollection.add(op);
  op.setOrder(this);
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 2:59 am 
Newbie

Joined: Thu Dec 20, 2007 9:26 am
Posts: 3
Hi malm66,

Thank you so much, it works fine!!

Bye


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.