Spring JPA

JPA problem:
@Entity @Setter @Getter @NoArgsConstructor
public class BicycleRider {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private int id;
// timeInSeconds refers to the amount of time it took finish the three stages of the race represented in seconds.
private double timeInSeconds;
private int mountainPoints;
private int sprintPoints;
@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn(name = "bicycleteam_id")
private BicycleTeam bicycleTeam;
}
@Entity @Setter @Getter @NoArgsConstructor
public class BicycleRider {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private int id;
// timeInSeconds refers to the amount of time it took finish the three stages of the race represented in seconds.
private double timeInSeconds;
private int mountainPoints;
private int sprintPoints;
@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn(name = "bicycleteam_id")
private BicycleTeam bicycleTeam;
}
I want to create a BicycleRiderobject and store it i my database; however, I need a BicycleTeam to associate it to. Do I need to need to create a BicycleTeamobject or do I need to do something else?
19 Replies
JavaBot
JavaBot3y ago
Hey, @Danish Pirate! Please remember to /close this post once your question has been answered!
dan1st
dan1st3y ago
create both objects, set/initialize them appropriately and persist them
danishpirate
danishpirateOP3y ago
Wouldn't it be a problem if I am adding an a BicycleRiderto an existing BicycleTeam? I have to create the object by pulling it from the database and since BicycleTeamcontains of a list of all riders it could take a long time to create the object if there are a ton of BicycleRiderobjects who have a relations to it. I have used that approach, but just curious if there is a better way.
@Override
public void saveRider(BicycleRider bicycleRider, Long bicycleTeamID) {
BicycleTeam bicycleTeam = bicycleTeamRepository.getReferenceById(bicycleTeamID);
System.out.println(bicycleTeam.getBicycleRiderList());
bicycleRider.setBicycleTeam(bicycleTeam);
bicycleRiderRepository.save(bicycleRider);
}
@Override
public void saveRider(BicycleRider bicycleRider, Long bicycleTeamID) {
BicycleTeam bicycleTeam = bicycleTeamRepository.getReferenceById(bicycleTeamID);
System.out.println(bicycleTeam.getBicycleRiderList());
bicycleRider.setBicycleTeam(bicycleTeam);
bicycleRiderRepository.save(bicycleRider);
}
dan1st
dan1st3y ago
should be fine reusing existing (persisted) objects is perfectly fine
danishpirate
danishpirateOP3y ago
Would it work if I were to create a new BicycleTeamobject using data from an existing one, but excluding the list of riders from it?
dan1st
dan1st3y ago
why don't you just use the existing object?
danishpirate
danishpirateOP3y ago
I'm just worried about loading about retrieving a large objects from the database. This approach doesn't seem very scale-able
dan1st
dan1st3y ago
Do you use lazy fetching?
danishpirate
danishpirateOP3y ago
Yes
dan1st
dan1st3y ago
if you add a bicycle to the (lazily loaded) list, does it work without loading it?
danishpirate
danishpirateOP3y ago
You mean like this:
public void saveRider(BicycleRider bicycleRider, Long bicycleTeamID) {
BicycleTeam bicycleTeam = bicycleTeamRepository.getReferenceById(bicycleTeamID);
bicycleTeam.getBicycleRiderList().add(new BicycleRider());
bicycleRider.setBicycleTeam(bicycleTeam);
bicycleRiderRepository.save(bicycleRider);
}
public void saveRider(BicycleRider bicycleRider, Long bicycleTeamID) {
BicycleTeam bicycleTeam = bicycleTeamRepository.getReferenceById(bicycleTeamID);
bicycleTeam.getBicycleRiderList().add(new BicycleRider());
bicycleRider.setBicycleTeam(bicycleTeam);
bicycleRiderRepository.save(bicycleRider);
}
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?
dan1st
dan1st3y ago
try adding to it at the beginning and check whether it works and check that it doesn't load all the objects
danishpirate
danishpirateOP3y ago
Is there a way to override lazy loading?
@Override
public List<BicycleRider> findAll() {
return bicycleRiderRepository.findAll();
}
@Override
public List<BicycleRider> findAll() {
return bicycleRiderRepository.findAll();
}
I want to return a list of all BicycleRiderobjects, 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
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
danishpirate
danishpirateOP3y ago
What should I do instead? I can't return a list of all BicycleRiderfrom the endpoint without getting
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->pirate.tourdefrance24timersopgave.model.BicycleRider["bicycleTeam"]->pirate.tourdefrance24timersopgave.model.BicycleTeam$HibernateProxy$1DntwsM8["hibernateLazyInitializer"])] with root cause
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->pirate.tourdefrance24timersopgave.model.BicycleRider["bicycleTeam"]->pirate.tourdefrance24timersopgave.model.BicycleTeam$HibernateProxy$1DntwsM8["hibernateLazyInitializer"])] with root cause
dan1st
dan1st3y ago
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?
danishpirate
danishpirateOP3y ago
Sure, just a sec
dan1st
dan1st3y ago
btw Spring allows you to log all the executed SQL statements for testing
danishpirate
danishpirateOP3y ago
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.

Did you find this page helpful?