Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32872 > unrolled thread

The correct way to create compound keys in JPA

Started byzerbolopolis@gmail.com
First post2015-10-13 13:12 -0700
Last post2015-11-29 23:14 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  The correct way to create compound keys in JPA zerbolopolis@gmail.com - 2015-10-13 13:12 -0700
    Re: The correct way to create compound keys in JPA Lew <lewbloch@gmail.com> - 2015-11-29 23:14 -0800

#32872 — The correct way to create compound keys in JPA

Fromzerbolopolis@gmail.com
Date2015-10-13 13:12 -0700
SubjectThe correct way to create compound keys in JPA
Message-ID<882851eb-79ed-4cc3-8ab5-6f6b3078fe58@googlegroups.com>
Hi, I'm using JPA to try and persist some entities. However I'm struggling to udnerstand compound keys and how they're created. For example I understood that the correct way to create a compound key would be to do something like this:

OrderBookKey orderKey = new OrderBookKey();	            orderKey.setBidOrderId(bidOrderId);	            orderKey.setAskOrderId(askOrderId);
OrderBook book = em.find(OrderBook.class,orderKey);

However book returns null. So I create it like this:
	         
OrderBook book = new OrderBook(amount, em.find(BidOrder.class,bidOrderId),
	            		em.find(AskOrder.class, askOrderId));

But then what is the point of the compound key class? Is it just for find operations after the entity has been persisted?

Is this correct or is the find returning null an indication that something is wrong with the setup of my entity/key wrapper?

Thanks,
Zerb

[toc] | [next] | [standalone]


#33244

FromLew <lewbloch@gmail.com>
Date2015-11-29 23:14 -0800
Message-ID<aed6457a-6fc1-4c0d-83ac-760e9bb2add5@googlegroups.com>
In reply to#32872
On Tuesday, October 13, 2015 at 1:12:30 PM UTC-7, zerbol...@gmail.com wrote:
> Hi, I'm using JPA to try and persist some entities. However I'm struggling to udnerstand compound keys and how they're created. For example I understood that the correct way to create a compound key would be to do something like this:
> 
> OrderBookKey orderKey = new OrderBookKey();	            orderKey.setBidOrderId(bidOrderId);	            orderKey.setAskOrderId(askOrderId);
> OrderBook book = em.find(OrderBook.class,orderKey);
> 
> However book returns null. So I create it like this:
> 	         
> OrderBook book = new OrderBook(amount, em.find(BidOrder.class,bidOrderId),
> 	            		em.find(AskOrder.class, askOrderId));
> 
> But then what is the point of the compound key class? Is it just for find operations after the entity has been persisted?
> 
> Is this correct or is the find returning null an indication that something is wrong with the setup of my entity/key wrapper?

You really shouldn't be messing with "keys" in JPA at all. What defines a 'Book' in your *object* model? Is it the title? Title plus author? Library of Congress catalog number? Use that as the 'equals()'/'hashCode()'/'compareTo()' "key". JPA is for an *object* model, not a table model.

An 'OrderBook' shouldn't be looking at fake "ID" fields for its relations, it should be defining (I'm guessing) '@ManyToMany' connections between 'BidOrder' and 'AskOrder' instances - not IDs, but instances.

What distinguishes a 'BidOrder' from another one, or an 'AskOrder' from another one of those in the object model?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web