休眠-脏检查不适用于@Convert

时间:2018-10-26 10:01:25

标签: hibernate

@Entity
class X {
    @Id
    int id;
    public static final class Converter implements AttributeConverter<Set<Integer>, byte[]> {
        @Override
        public byte[] convertToDatabaseColumn(Set<Integer> attribute) {
            return new byte[2]; // just for demo
        }
        @Override
        public Set<Integer> convertToEntityAttribute(byte[] dbData) {
            return new HashSet<>();// just for demo
        }
    }
    @Convert(converter = X.Converter.class)
    Set<Integer> s = new HashSet<>();
}

我使用@ConvertSet<Integer>转换为db列中的字节。但是我发现脏支票无法使用:

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
X a = new X();
a.s.add(1);
session.save(a);
transaction.commit();


Transaction transaction1 = session.beginTransaction();
session.update(a); // a is unchanged, so this update should do nothing
transaction1.commit();
session.close();

运行这段代码后,convertToDatabaseColumn被多次调用:

call
Hibernate: insert into X (s, id) values (?, ?)
call
Hibernate: update X set s=? where id=?
call
call
Hibernate: update X set s=? where id=?
call
call

发生了什么事?我该如何让休眠状态对Set<Integer>进行脏检查?

0 个答案:

没有答案