JPA Referenced Entities: Is bad practice to reference only its Key instead of all its object









up vote
0
down vote

favorite












Suppose the cenário where customers puts orders on our system. So, for simplicity, we have 2 entities :



Customer
- CustomerID
- Name

Order
- Customer
- OrderID
- OrderValue


We have a REST Service that is used to save/update a Customer and also, a REST Service that is used to POST orders.



In JPA, actually, the Order entity has a ManyToOne relationship to Customer entity. So, assuming that I'm not using DTO, I need to call my OrderService passing, at least, something like this JSON:




"customer":
"customer_id": 10
,
"order_id": 40,
"order_value": 200.1



And, when I return the order to the RestClient, all Customer is being returned, since it's being loaded during the process.



So, thinking on complex systems where the are a lot of Entities that are 'nested', is it acceptable to (or at least not a bad practice) to create entities that references only the key instead of the object itself? Something like:



Order
- CustomerID
- OrderID
- OrderValue


I'm new to JPA/Hibernate and I come from a world where all entities were loaded manually from the Database. So, when dealing with an Order, we used to load only the Customer.CustomerId instead of all Customer data. Here, in this example, I think that I have two options: Load all Customer (using FetchType.EAGER or load nothing (using FetchType.LAZY), so, there is no way (at least that I know) to return to the RESTClient an Order object that contais only it's CustomerID (at least, without manually setting order.customer.customer_id manually, thing that sounds a little bit hacky).



Thanks a lot!










share|improve this question























  • Are you using Spring?
    – Alan Hay
    Nov 8 at 13:18










  • Yes!. Sorry, I forgot to mention that.
    – regisxp
    Nov 8 at 13:19






  • 1




    Add the relevant tags to your question e.g. Spring, Spring MVC? Spring Data? so we have a better idea what the options are
    – Alan Hay
    Nov 8 at 13:22










  • See also stackoverflow.com/a/50096610/1356423 for some options on how you can customize the JSON representation of an Entity without the overhead of a full DTO layer.
    – Alan Hay
    Nov 8 at 13:23















up vote
0
down vote

favorite












Suppose the cenário where customers puts orders on our system. So, for simplicity, we have 2 entities :



Customer
- CustomerID
- Name

Order
- Customer
- OrderID
- OrderValue


We have a REST Service that is used to save/update a Customer and also, a REST Service that is used to POST orders.



In JPA, actually, the Order entity has a ManyToOne relationship to Customer entity. So, assuming that I'm not using DTO, I need to call my OrderService passing, at least, something like this JSON:




"customer":
"customer_id": 10
,
"order_id": 40,
"order_value": 200.1



And, when I return the order to the RestClient, all Customer is being returned, since it's being loaded during the process.



So, thinking on complex systems where the are a lot of Entities that are 'nested', is it acceptable to (or at least not a bad practice) to create entities that references only the key instead of the object itself? Something like:



Order
- CustomerID
- OrderID
- OrderValue


I'm new to JPA/Hibernate and I come from a world where all entities were loaded manually from the Database. So, when dealing with an Order, we used to load only the Customer.CustomerId instead of all Customer data. Here, in this example, I think that I have two options: Load all Customer (using FetchType.EAGER or load nothing (using FetchType.LAZY), so, there is no way (at least that I know) to return to the RESTClient an Order object that contais only it's CustomerID (at least, without manually setting order.customer.customer_id manually, thing that sounds a little bit hacky).



Thanks a lot!










share|improve this question























  • Are you using Spring?
    – Alan Hay
    Nov 8 at 13:18










  • Yes!. Sorry, I forgot to mention that.
    – regisxp
    Nov 8 at 13:19






  • 1




    Add the relevant tags to your question e.g. Spring, Spring MVC? Spring Data? so we have a better idea what the options are
    – Alan Hay
    Nov 8 at 13:22










  • See also stackoverflow.com/a/50096610/1356423 for some options on how you can customize the JSON representation of an Entity without the overhead of a full DTO layer.
    – Alan Hay
    Nov 8 at 13:23













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Suppose the cenário where customers puts orders on our system. So, for simplicity, we have 2 entities :



Customer
- CustomerID
- Name

Order
- Customer
- OrderID
- OrderValue


We have a REST Service that is used to save/update a Customer and also, a REST Service that is used to POST orders.



In JPA, actually, the Order entity has a ManyToOne relationship to Customer entity. So, assuming that I'm not using DTO, I need to call my OrderService passing, at least, something like this JSON:




"customer":
"customer_id": 10
,
"order_id": 40,
"order_value": 200.1



And, when I return the order to the RestClient, all Customer is being returned, since it's being loaded during the process.



So, thinking on complex systems where the are a lot of Entities that are 'nested', is it acceptable to (or at least not a bad practice) to create entities that references only the key instead of the object itself? Something like:



Order
- CustomerID
- OrderID
- OrderValue


I'm new to JPA/Hibernate and I come from a world where all entities were loaded manually from the Database. So, when dealing with an Order, we used to load only the Customer.CustomerId instead of all Customer data. Here, in this example, I think that I have two options: Load all Customer (using FetchType.EAGER or load nothing (using FetchType.LAZY), so, there is no way (at least that I know) to return to the RESTClient an Order object that contais only it's CustomerID (at least, without manually setting order.customer.customer_id manually, thing that sounds a little bit hacky).



Thanks a lot!










share|improve this question















Suppose the cenário where customers puts orders on our system. So, for simplicity, we have 2 entities :



Customer
- CustomerID
- Name

Order
- Customer
- OrderID
- OrderValue


We have a REST Service that is used to save/update a Customer and also, a REST Service that is used to POST orders.



In JPA, actually, the Order entity has a ManyToOne relationship to Customer entity. So, assuming that I'm not using DTO, I need to call my OrderService passing, at least, something like this JSON:




"customer":
"customer_id": 10
,
"order_id": 40,
"order_value": 200.1



And, when I return the order to the RestClient, all Customer is being returned, since it's being loaded during the process.



So, thinking on complex systems where the are a lot of Entities that are 'nested', is it acceptable to (or at least not a bad practice) to create entities that references only the key instead of the object itself? Something like:



Order
- CustomerID
- OrderID
- OrderValue


I'm new to JPA/Hibernate and I come from a world where all entities were loaded manually from the Database. So, when dealing with an Order, we used to load only the Customer.CustomerId instead of all Customer data. Here, in this example, I think that I have two options: Load all Customer (using FetchType.EAGER or load nothing (using FetchType.LAZY), so, there is no way (at least that I know) to return to the RESTClient an Order object that contais only it's CustomerID (at least, without manually setting order.customer.customer_id manually, thing that sounds a little bit hacky).



Thanks a lot!







spring hibernate jpa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 13:23









Alain Cruz

1,390718




1,390718










asked Nov 8 at 13:01









regisxp

5202821




5202821











  • Are you using Spring?
    – Alan Hay
    Nov 8 at 13:18










  • Yes!. Sorry, I forgot to mention that.
    – regisxp
    Nov 8 at 13:19






  • 1




    Add the relevant tags to your question e.g. Spring, Spring MVC? Spring Data? so we have a better idea what the options are
    – Alan Hay
    Nov 8 at 13:22










  • See also stackoverflow.com/a/50096610/1356423 for some options on how you can customize the JSON representation of an Entity without the overhead of a full DTO layer.
    – Alan Hay
    Nov 8 at 13:23

















  • Are you using Spring?
    – Alan Hay
    Nov 8 at 13:18










  • Yes!. Sorry, I forgot to mention that.
    – regisxp
    Nov 8 at 13:19






  • 1




    Add the relevant tags to your question e.g. Spring, Spring MVC? Spring Data? so we have a better idea what the options are
    – Alan Hay
    Nov 8 at 13:22










  • See also stackoverflow.com/a/50096610/1356423 for some options on how you can customize the JSON representation of an Entity without the overhead of a full DTO layer.
    – Alan Hay
    Nov 8 at 13:23
















Are you using Spring?
– Alan Hay
Nov 8 at 13:18




Are you using Spring?
– Alan Hay
Nov 8 at 13:18












Yes!. Sorry, I forgot to mention that.
– regisxp
Nov 8 at 13:19




Yes!. Sorry, I forgot to mention that.
– regisxp
Nov 8 at 13:19




1




1




Add the relevant tags to your question e.g. Spring, Spring MVC? Spring Data? so we have a better idea what the options are
– Alan Hay
Nov 8 at 13:22




Add the relevant tags to your question e.g. Spring, Spring MVC? Spring Data? so we have a better idea what the options are
– Alan Hay
Nov 8 at 13:22












See also stackoverflow.com/a/50096610/1356423 for some options on how you can customize the JSON representation of an Entity without the overhead of a full DTO layer.
– Alan Hay
Nov 8 at 13:23





See also stackoverflow.com/a/50096610/1356423 for some options on how you can customize the JSON representation of an Entity without the overhead of a full DTO layer.
– Alan Hay
Nov 8 at 13:23


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208300%2fjpa-referenced-entities-is-bad-practice-to-reference-only-its-key-instead-of-al%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208300%2fjpa-referenced-entities-is-bad-practice-to-reference-only-its-key-instead-of-al%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)