1

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;
}
JvBarney
  • 41
  • 8
  • May I ask why you can´t use the EntityManager.getReference() method? – C. Weber Aug 15 '19 at 10:16
  • It's not an static method so I need the EntityManager to be loaded with @Autowired or something, but this class where I need it is not a service. Should I make it a service and just use this EntityManger? It seems a bit weird to only use the EntityManger to create a reference... – JvBarney Aug 15 '19 at 10:19
  • Is the class a Spring bean (@Component or sub-annotation) ? – eHayik Aug 15 '19 at 10:32
  • It wasn't, but I changed so the EntityManger can be autowired now... It saves my problem for now. The class where I use it in is an entity listener and I couldn't manage to get it autowired, but I found this which works: https://stackoverflow.com/questions/12155632/injecting-a-spring-dependency-into-a-jpa-entitylistener – JvBarney Aug 15 '19 at 10:36
  • You must are more details to your question. Why can you not use it where you need it ? – eHayik Aug 15 '19 at 10:36
  • In that case you must take this in consideration "In general, the lifecycle method of a portable application should not invoke EntityManager or Query operations, access other entity instances, or modify relationships within the same persistence context. A lifecycle callback method may modify the non-relationship state of the entity on which it is invoked." You can find more details here https://stackoverflow.com/questions/56916520/bean-validation-with-extra-information/57041770#57041770 – eHayik Aug 15 '19 at 10:39

1 Answers1

0

I could not autowire the EntityManager because I needed this in a entity listener and these are not autowired. I found a way to get this autowired on Injecting a Spring dependency into a JPA EntityListener.

Thanks for everyone who helped me! :)

JvBarney
  • 41
  • 8