admin管理员组

文章数量:1386782

I have an attribute converter that I recently added and I want to run on all existing data.

Query query = entityManager.createNativeQuery("Select * from tableA", TableA.class);
List list = query.getResultList()
for(Object obj : list) {
   entityManager.persist(obj);
}

// This code is never called
public class Converter implements AttributeConverter{
   @Override
   convertToEntityAttribute(String colummn){
      //alter the string
   }
}

I noticed that this will not call my attribute converter that is defined in tableA entity on a column. However, if I modify it in anyway, it calls the attribute converter correctly on persist.

I think this is because it detects it wasn't modified and to save a call to the db it doesn't actually persist it. Anybody have any ideas to mark it as persisted without altering the data? Or maybe altering the data and then setting it back?

I have an attribute converter that I recently added and I want to run on all existing data.

Query query = entityManager.createNativeQuery("Select * from tableA", TableA.class);
List list = query.getResultList()
for(Object obj : list) {
   entityManager.persist(obj);
}

// This code is never called
public class Converter implements AttributeConverter{
   @Override
   convertToEntityAttribute(String colummn){
      //alter the string
   }
}

I noticed that this will not call my attribute converter that is defined in tableA entity on a column. However, if I modify it in anyway, it calls the attribute converter correctly on persist.

I think this is because it detects it wasn't modified and to save a call to the db it doesn't actually persist it. Anybody have any ideas to mark it as persisted without altering the data? Or maybe altering the data and then setting it back?

Share Improve this question edited Mar 18 at 10:04 Mark Rotteveel 110k229 gold badges156 silver badges224 bronze badges asked Mar 17 at 18:37 coloradomancoloradoman 4272 gold badges7 silver badges19 bronze badges 20
  • If the entity is unchanged, why (re-)persist it? – Turing85 Commented Mar 17 at 18:39
  • @Turing85 I have an newly added attribute converter added onto a column of the entity. It changes how the data is saved to the db column using the convertToDatabaseColumn. – coloradoman Commented Mar 17 at 18:40
  • @Turing85 so the data is changing, but its changing after persist is being called from what it seems. – coloradoman Commented Mar 17 at 18:41
  • @Turing85 it is never calling my attribute converter if I don't change the data before I call persist. – coloradoman Commented Mar 17 at 18:42
  • @Turing85 I updated the example to show more code – coloradoman Commented Mar 17 at 19:06
 |  Show 15 more comments

2 Answers 2

Reset to default 0

In order to make the converter work at all you need @Convert annotations on the converter and if you don't want it to be used on all Strings you need @Convert on the attributes as well.

Since you want to encrypt and decrypt you need to implement both methods in the same converter.

As you described this doesn't help with the process of migrating your database.
This is because the assumption for converters is that when you load an attribute through a converter and store it again, you effectively didn't change it.

This is what persist relies on. You loaded the entity. You didn't change it. So it doesn't need to be flushed to the database.

This is not the purpose of converters.

Instead write a little program/routine that does the migration.

  • load the data in chunks.
  • encrypt it.
  • write it back to the database.

In order to keep track of what is already migrated and what isn't an additional column is helpful. You write the encrypted data to the additional column. And to load chunks you load the first n elements where the new column is null.

You can do this in JPA, but it's probably easier and faster to do it with JdbcTemplate.

The solution I found was to add a new column that is a modifiedDate. I then changed this date before persist so that it detects there is a change and then correctly uses the attribute converter.

本文标签: javaCan I force EntityManager to persistStack Overflow