This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Value in @JoinColumn

 
Jas Singh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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 ]
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • 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);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic