JPA不使用Converter类更新列 [英] JPA not updating column with Converter class

查看:98
本文介绍了JPA不使用Converter类更新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Converter类将复杂的类存储为mySQL中的JSON文本.当我添加一个新实体时,Converter类将按预期工作.但是,当我更新实体时,复杂类中的数据不会在数据库中更新,而是会在内存中更新.其他属性(例如,纬度和经度)也会更新.我在 convertToDatabaseColumn 方法中放置的断点,并没有在更新时触发.

I'm using a Converter class to store a complex class as JSON text in mySQL. When I add a new entity, the Converter class works as intended. However, when I update the entity, the data in the complex class is not updated in the database but it's updated in memory. Other attributes such as Lat and Long are updated. The breakpoint I placed at the convertToDatabaseColumn method and it did not trigger on update.

对象类

public class Project {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String city;
    private String state;
    private String country;
    @Enumerated(EnumType.STRING)
    private StatusType status;
    private String street;
    private double latitude;
    private double longitude;
    @Convert(converter=ProjectPropertyConverter.class)
    private ProjectProperty property;
}

public class ProjectProperty {

    private String description;
    private List<String> projectImgs;
    private Boolean hasImages;
}

属性转换器类

@Converter (autoApply=true)
public class ProjectPropertyConverter implements AttributeConverter<ProjectProperty, String> {

    @Override
    public String convertToDatabaseColumn(ProjectProperty prop) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            String jsonString = mapper.writeValueAsString(prop);
            return jsonString;
        } catch (Exception e) {
            System.out.print(e.toString());
            return null;
        }

    }

    @Override
    public ProjectProperty convertToEntityAttribute(String jsonValue) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            ProjectProperty p = mapper.readValue(jsonValue, ProjectProperty.class);

            if(p.getProjectImgs().isEmpty())
            {
                p.setHasImages(Boolean.FALSE);
            }
            else
            {
                p.setHasImages(Boolean.TRUE);
            }          
            return p;
        } catch (Exception e) {
            System.out.print(e.toString());
            return null;
        }
    }
}

更新数据库的方法

public void modifyEntity(Object entity, String query, HashMap params) {
    try {
        tx.begin();
        em.flush();
        tx.commit();

    } catch (Exception e) {
        e.toString();
    }
}

推荐答案

我来这里寻找相同的答案.原来的问题是JPA不知道您的对象是脏的.通过在此复杂对象上实现equals()/hashcode()方法可以解决此问题.在您的示例中,为ProjectProperty实现equals和哈希码

I came here looking for same answers. Turns out the problem is JPA doesn't know that your object is dirty. This was solved by implementing equals()/hashcode() methods on this complex objects. In your example, implement equals and hashcode for ProjectProperty

一旦完成,JPA便可以通过这些方法识别基础对象是否脏了,并进行转换和持久化.

Once that is done, JPA is able to identify via these methods that the underlying object is dirty and converts and persists.

这篇关于JPA不使用Converter类更新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆