File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Null Value in @JoinColumn Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Null Value in @JoinColumn" Watch "Null Value in @JoinColumn" New topic
Author

Null Value in @JoinColumn

Jas Singh
Greenhorn

Joined: Jul 14, 2006
Posts: 10
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 ]

SCJP 1.4<br />IBM Certified eCom Professional
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
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);


-Mike
Pro JPA 2: Mastering the Java Persistence API
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Value in @JoinColumn
 
Similar Threads
am I doing this correct way?
finally decided to generate keys this way
problem saving with foreign key error
One to many insert with iBatis
Check box and Indexed properties