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!
spring hibernate jpa
add a comment |
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!
spring hibernate jpa
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
add a comment |
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!
spring hibernate jpa
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
spring hibernate jpa
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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