I'm creating a web application in Spring where I've some transactions and categories. The transactions have a ManyToOne relation to Category. Now I want to set the category by id and not by Category (as object).
The reason why I couldn't use the EntityManager is because I needed this in a EntityListener. In my answer I found a way to autowire this EntityListener which fixed my problem.
Is this possible in Spring?
I found this on StackOverflow Hibernate - Add member entity instance by ID, but I can't use the EntityManager where I need it.
This is my transaction entity:
@Entity
@ToString
@Slf4j
@EntityListeners(TransactionListener.class)
@Getter
@Setter
public class Transaction {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column
    private long account;
    @Column
    @JsonFormat(pattern = "dd-MM-yyyy")
    private LocalDate date;
    @Column
    private float amount;
    @Column(columnDefinition = "text")
    private String description;
    @Column(unique = true)
    private String hash;
    @ManyToOne
    @JoinColumn(name = "category_id")
    @JsonIgnoreProperties("transactions")
    private Category category;
}
and this is my category entity
@Entity
@ToString
@Getter
@Setter
@EntityListeners(CategoryListener.class)
public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column
    private String name;
    @Column
    private String color;
    @Column
    private String[] matchers;
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @OneToMany(mappedBy = "category")
    @JsonIgnoreProperties("category")
    private List<Transaction> transactions;
}
