Hi, I'm running the following code to update the database according to the data I read from CSV file. I've tried to debug, and check the console and it's running through the whole 800 records. I don't get any error, but only the first record is inserted. If I'm using persist instead of merge, I got "Cannot persist detached object" error.
for (String[] data : dataList) {
log.debug("Reading data no " + (i++));
EntityManager em = PersistenceUtil.getAgisDbEntityManager();
EntityTransaction tr = em.getTransaction();
tr.begin();
try {
AddressEntity address = new AddressEntity();
updateAddress(data, address);
em.merge(address);
//em.persist(address);
em.flush();
tr.commit();
} catch (Exception exc) {
log.error(exc.getMessage(), exc);
if (tr.isActive())
tr.rollback();
}
}
And here is my updateAddress method, basically it's updating some of the fields.
private void updateAddress(String[] data, AddressEntity address) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//setting the column data
for (int i = 0; i < data.length; i++) {
final String column = dataColumns.get(i);
if (!column.equals("#IGNORE#")) {
setProperty(address, column, data[i]);
}
}
for (String field : this.defaultColumns.keySet()) {
if (!field.startsWith("#"))
setProperty(address, field, this.defaultColumns.get(field));
}
}
Here is my persistence.xml for your reference.
<persistence-unit name="agisdb-PU">
<class>com.agis.livedb.domain.AddressEntity</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/agisdb"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.user" value="root"/>
<property name="toplink.jdbc.password" value="password"/>
</properties>
</persistence-unit>
Do you think I missed out something? Thanks a lot! Robert