Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Help with PrimaryKey usage for legacy tables

Hello,

LineItem has a composite primary key, and so requires a PK class, which can be specified using the @IdClass annotation on the entity. If you are using the JPA 1.0 spec, it only allows basic fields to be apart of the primary key so in addition to the ManyToOne mapping, the LineItem will need a String orderNo attribute that is marked as updateable=false insertable=false (or you will get a multiple writeable mapping exception if both are writable). This string will need to be marked as ID, and the attribute added to the Primary Key class for LineItem.

If you want to try out the future JPA 2.0 specification (EclpseLink 2.0 implements a version of the draft), it allows the ManyToOne to be marked as an ID - so you don't need to add string in LineItem for the Order_No column. You will still need a primary key class for LineItem though.

Also, the mappings you have provided are not correct - the OneToMany mapping is defaulting to using a join table because it is missing the mappedby="order", and the ManyToOne LineItem->Order isn't specifying the column to use and so will default to order_order_no.
Best Regards,
Chris

cmathrusse wrote:
I'm using EclipseLink 1.1.1 and I'm trying to map a parent-child relationship
which exists in legacy tables. I read through the documentation but I'm
still a bit confused by it.
An order's primary key is a String (order_no) that is defined as unique.
LineItems have an association to the order. The lineItem table has the
order_no field and a line_no. It is a combination of order_no and line_no
that make the PrimaryKey for the LineItem. The LineItem line_no is not
unique within the table but is only unique with the addition of the
order_no.

I've tried a few different ways of defining the @Id but nothing seems to be
quite right and the table generation is not correct. I expect the Order to
have an order_no column and the LineItem table to have a composite key of
order_no and line_no.
My objects:

Order {
	@Id
	@Column(name = "ORDER_NO", length = 12)
	private String orderNo;

	@OneToMany(fetch = FetchType.EAGER)
	private java.util.List<LineItem> lineItems;

...
}

LineItem {
	@ManyToOne
	private Order order;

	@Id
	@Column(name = "LINE_NO", nullable = false)
	private Integer lineNo;
}

Thanks for the help....


Back to the top