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;
}
6
  • May I ask why you can´t use the EntityManager.getReference() method? Commented Aug 15, 2019 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... Commented Aug 15, 2019 at 10:19
  • Is the class a Spring bean (@Component or sub-annotation) ? Commented Aug 15, 2019 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: stackoverflow.com/questions/12155632/… Commented Aug 15, 2019 at 10:36
  • You must are more details to your question. Why can you not use it where you need it ? Commented Aug 15, 2019 at 10:36

1 Answer 1

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! :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.