Closed cicciob95 closed 3 years ago
Can you help us understand the context in which you're in? Are you using lazily loaded references? Finally, a bit of sample code is always helpful.
It sounds a bit as if your question touches on the basics of JPA and attached/detached entities. In such a case, you can find answers to your questions on Stack Overflow.
Yes, references are loaded lazily. The context concerns the provision of a service where the same user can enter into several contracts. In this context it is possible to associate an existing contract with another registered user. Therefore I have implemented the following classes, their respective controllers and repositories
@Entity
@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "codFiscale")
public class Utente {
@Id
private String codFiscale;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "titolare")
private List<ContrattoFornitura> contratti;
//other fields + set and get methods
----
@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ContrattoFornitura {
@Id @GeneratedValue
private long id;
@ManyToOne @JoinColumn(name="titolare")
@JsonProperty("titolare")
private Utente titolare;
//other fields + set and get methods
After a more in-depth analysis, I realized that in reality by retrieving all the contracts currently present, all the fields of the external reference are returned only for the first contract stipulated by a user, while for the subsequent contracts only the identifier is returned. I believe this behavior is due to some specific setting. I apologize for posting this issue but I have not been able to find answers elsewhere. Thanks
You have several choices of approaching the issue on your side (un-proxying the entity instance, mapping the object onto a DTO, switching to eager loading, Open Session in View, …). Stackoverflow and the JPA documentation are better places to discuss your issue as we use GitHub issues primarily to track bugs and feature requests.
@mp911de Thanks
Hello, I implemented a backend with Spring that exports functionality and data through REST APIs to a front end developed with angular. After I replace the reference to an external entity in a certain entity, if I send a GET to retrieve the updated entity, the REST server returns only the foreign key value instead of all the fields of the external reference. By replacing the original reference, the REST server returns the entire object. Through debugging I noticed that the find method of the JPA repository correctly returns all the fields of the external entity, but builds the response only with the key as you can see from the sniffed packet with wireshark.