java spring database

Asked

Viewed 66 times

2

Good evening, I’m having a doubt in my studies, I’m doing a project to train knowledge and I came across a problem.

I have a User class:

@Entity 
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String password;

@OneToMany
private List<Service> services = new ArrayList<>();
//get and set

And a Service:

@Entity
public class Service {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String description;
private String userService;
private String password;

My question is, how to search a user’s Services List?

In my view I will have to create an attribute in services and set the id of a user? But this way would not be a "heavy" bank? where I will have to walk all the bank looking for the service that has that "USER_ID"

2 answers

1

My question is, how to search a user’s Services List?

Only: user.getServices(). When making this call will be returned a List<Service>.

In my view I will have to create an attribute in services and set the id of a user? (...) where I will have to walk all the bank looking for the service that has that "USER_ID"

This is not necessary as Hibernate/JPA knows how to obtain the data in the database (by means of the method find(), for example) from the entity class annotations.

However, it may be that only note the attribute services with @OneToMany in the entity class User not enough for Hibernate/JPA to work. You can learn more in questions already answered here at Stackoverflow:

  • Thanks for the reply, but when trying to use the user.getServices() I get exceptions.

0

ERROR when using user.getServices() :

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor47.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 48 more

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: rememberPassword.model.User.services, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:597)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:216)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:576)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:147)
at org.hibernate.collection.internal.PersistentBag.get(PersistentBag.java:477)
at rememberPassword.controller.MainScreenController.autentication(MainScreenController.java:94)
... 58 more

Method used to view the list of services and that generates the error above:

        Optional<User> usuario = userReposi.findById(1L);

    System.out.println("Descricao serv: " + usuario.get().getServices().get(0).getDescription());
  • Please edit your question and add this information to it, not as an answer. However, this exception occurs because the Hibernate session has already been closed. Kindly post also the complete method and class from where you are calling the Repository

Browser other questions tagged

You are not signed in. Login or sign up in order to post.