JPA 合并似乎不起作用
我正在运行以下代码,根据从 CSV 文件读取的数据更新数据库。 我尝试调试并检查控制台,它正在运行整个 800 条记录。 我没有收到任何错误,但只插入了第一条记录。 如果我使用持久而不是合并,则会收到“无法持久分离对象”错误。
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();
}
}
这是我的 updateAddress 方法,基本上它是更新一些字段。
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));
}
}
这是我的 persistence.xml 供您参考。
<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>
你认为我错过了什么吗? 多谢! 罗伯特
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该使用 persist(),而不是合并()
you should use persist(), not merge()
好的,问题出在我的地址实体对象上,我必须将以下内容添加到 id 字段中。
@GenerateValue(strategy=GenerationType.identity)
谢谢 dfa!
罗伯特
Ok, the problem is with my Address Entity object, I have to add the following to the id field.
@GeneratedValue(strategy=GenerationType.identity)
Thanks dfa!
Robert