Accessing Spring Beans inside AttributeConverter class

后端 未结 3 1408
逝去的感伤
逝去的感伤 2020-12-10 02:25

I\'m developing a Spring Data JPA application, and I\'ve created an AttributeConverter class in order to save an ArrayList of objects as JSON in a

3条回答
  •  北海茫月
    2020-12-10 03:02

    With JPA 2.2, Spring 5.1( SPR-16305) and Hibernate 5.3.0 (HHH-12135) you no longer need to use the mutable static property hack and can use dependency injection just like you would on a regular spring managed bean (note that annotations are no longer necessary):

    public class MyAttributeConverter implements AttributeConverter {
    
        private final MySpringBean bean;
    
        public MyAttributeConverter(MySpringBean bean) {
            this.bean = bean;
        }
    
        public Y convertToDatabaseColumn(X attribute) {
          ...
        }
    
        public X convertToEntityAttribute(Y dbData) {
          ...
        }
    }
    

提交回复
热议问题