Spring JPA
JPA problem:
I want to create a
BicycleRider
object and store it i my database; however, I need a BicycleTeam
to associate it to. Do I need to need to create a BicycleTeam
object or do I need to do something else?19 Replies
Hey, @Danish Pirate!
Please remember to
/close
this post once your question has been answered!create both objects, set/initialize them appropriately and persist them
Wouldn't it be a problem if I am adding an a
BicycleRider
to an existing BicycleTeam
? I have to create the object by pulling it from the database and since BicycleTeam
contains of a list of all riders it could take a long time to create the object if there are a ton of BicycleRider
objects who have a relations to it.
I have used that approach, but just curious if there is a better way.
should be fine
reusing existing (persisted) objects is perfectly fine
Would it work if I were to create a new
BicycleTeam
object using data from an existing one, but excluding the list of riders from it?why don't you just use the existing object?
I'm just worried about loading about retrieving a large objects from the database.
This approach doesn't seem very scale-able
Do you use lazy fetching?
Yes
if you add a bicycle to the (lazily loaded) list, does it work without loading it?
You mean like this:
Should I be adding the rider to the riderteam and saving it or adding the riderteam to the rider and saving or does it not matter?
try adding to it at the beginning
and check whether it works
and check that it doesn't load all the objects
Is there a way to override lazy loading?
I want to return a list of all
BicycleRider
objects, but due to lazy loading I get an error. I can fix it by choosing eager loading for BicycleRider
, but I am not sure if I want to do that.Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
What should I do instead? I can't return a list of all
BicycleRider
from the endpoint without getting
You typically want eager loading when you (almost) always need the collection when needing the object
e.g. if you have a role-->permission mapping, you might want to load the permissions eagerly when loading the roles
and the
fetch = FetchType.LAZY
specifies whether to load it lazily or eagerly
Can you show the whole stack trace?Sure, just a sec
btw Spring allows you to log all the executed SQL statements for testing
Pastebin
Hibernate: select bicyclerid0_.id as id10, bicyclerid0_.bicyclete...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.