• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Null Value in @JoinColumn

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ranchers,
I am new to JPA and currently working on a new project where we are using EJB3, JPA, Toplink, Glassfish, SQL Server 2005.

My use case is like this:
1. Order Table
2. OrderItems Table

1.Order:
ORDER_ID - PK, IDENTITY (Auto Generated key)
ORDER_TYPE
CREATION_DATE
SOME OTHER COLUMNS
2. OrderItems:
ID -PK, IDENTITY(Auto Generated)
ORDER_ID -FK, Ref: ORDER.ORDER_ID
ITEM_NAME
QTY
PEGGABLE
Other Columns.


Code Snippet (Please dont go on syntax etc as I am typing fom Home, where I dn't have access to Eclipse):
@Entity
public class Order
{
@Id
@Generated(generation Type=IDENTITY, cascade=CascadeType.PERSIST)
public int ORDER_ID;

@OneToMany (mappedBy="order")
public List<OrderItem> orderItems;

rest of the code for Order class.
}

@Entity
public class OrderItems
{
@Id
@Generated(generation Type=IDENTITY)
int ID;

@ManyToOne
@JoinColun(name="ORDER_ID", refrenceColumn="ORDER_ID")
Order order;
}

@Stateless
public class Client
{
public void addOrder()
{
Order order=new Order();
order.ORDER_TYPE=SOME_TYPE;
order.CREATION_DATE=new Date();

OrderItem item = new OrderItem();
item.ITEM_NAME="JPA Book";
item.QTY=1;
item.PEGGABLE=2;

List list = new ArrayList();
list.add(item);
order.orderItems=list;

em.persist(order);
}
}


TopLink Log:
INSERT INTO ORDER (ORDER_TYPE, CREATION_DATE, XYZ)
bind [SOME_TYPE, 28/07/2008 10:08PM, xyz]

INSERT INTO OrderItems (ORDER_ID, ITEM_NAME, QTY, PEGGABLE)
bind [null, JPA Book, 1, 2];


So the trouble is that ORDER_ID in OrderItems is coming as null value where I was expecting it to be the ORDER_ID of Order table.
Could you please suggest what could be missing in my code or any suggestion to get the ORDER_ID value in OrderItems table.

Thanks in advance.
[ July 28, 2008: Message edited by: Jas Singh ]
 
author
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jas,

Whenever you have a bidirectional relationship you need to set both directions. In other words, before your call to persist(order) you should add a line something like: item.setOrder(order);
reply
    Bookmark Topic Watch Topic
  • New Topic