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


Groups > comp.lang.java.programmer > #33244

Re: The correct way to create compound keys in JPA

Newsgroups comp.lang.java.programmer
Date 2015-11-29 23:14 -0800
References <882851eb-79ed-4cc3-8ab5-6f6b3078fe58@googlegroups.com>
Message-ID <aed6457a-6fc1-4c0d-83ac-760e9bb2add5@googlegroups.com> (permalink)
Subject Re: The correct way to create compound keys in JPA
From Lew <lewbloch@gmail.com>

Show all headers | View raw


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?

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web