'I created in Spring a JPA repository works nice in the CommandLineRunner but when I use in a @Entity the result is null
I am creating a private ItemsRepository itemsRepository but returns null . I am using @Transient and @Autowired .
How I can use a JPA repository inside this class?
package com.example.hibernateonetomany.model;
import com.example.hibernateonetomany.repositories.ItemsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Set;
import javax.persistence.*;
@Entity
@Table(name = "CART")
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cart_id")
private long id;
@Transient
@Autowired
private ItemsRepository itemsRepository;
@OneToMany(mappedBy = "cart")
private Set<Item> items;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Set<Item> getItems() {
Set<Item> list_temp=itemsRepository.findByCart_Id(id);
return list_temp;
}
public void setItems(Set<Item> items) {
this.items = items;
}
} ```
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
