我需要从表中读取一行,更改值并更新此行。 Hibernate Debug-Log表示它可以工作,但是没有。 更新后的数据库表条目与以前相同。
应用程序:
public int holeNeuNummer(String sa) throws WrappedException {
int neueNummer = 0;
Rggsnr rggsnr;
try {
rggsnr = Service.selectFirst("from Rggsnr where sa='" + sa + "'");
System.out.println("OLD: " + rggsnr);
} catch(DatabaseException | EmptyResultException e) {
printError(e);
return neueNummer;
}
rggsnr.getId().setLfdnr(rggsnr.getId().getLfdnr() + 1);
// updaten
try {
Service.update(rggsnr);
System.out.println("NEW: " + rggsnr);
} catch (Throwable e) {
e.printStackTrace();
}
return neueNummer;
}
输出:
OLD: Rggsnr{sa=BH, lfdnr=867}
NEW: Rggsnr{sa=BH, lfdnr=868}
更新的休眠调试日志:
08:07:12,542 DEBUG [org.hibernate.engine.transaction.spi.AbstractTransactionImpl] - begin
08:07:12,542 DEBUG [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] - Obtaining JDBC connection
08:07:12,542 DEBUG [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] - Obtained JDBC connection
08:07:12,542 DEBUG [org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction] - initial autocommit status: false
08:07:12,546 DEBUG [org.hibernate.engine.transaction.spi.AbstractTransactionImpl] - committing
08:07:12,546 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener] - Processing flush-time cascades
08:07:12,547 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener] - Dirty checking collections
08:07:12,550 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener] - Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
08:07:12,550 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener] - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
08:07:12,551 DEBUG [org.hibernate.internal.util.EntityPrinter] - Listing entities:
08:07:12,551 DEBUG [org.hibernate.internal.util.EntityPrinter] - net.oko.batch.persistence.dto.okod.Rggsnr{id=component[frei,jahr,lfdnr,sa]{frei= , jahr=2019, sa=BH, lfdnr=868}}
08:07:12,551 DEBUG [org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction] - committed JDBC Connection
08:07:12,552 DEBUG [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] - Releasing JDBC connection
08:07:12,552 DEBUG [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] - Released JDBC Connection
更新功能:
public static void update(Object obj) throws DatabaseException {
Session hsession = null;
try {
hsession = HibernateUtil.getSessionFactory().openSession();
Transaction htransaction = hsession.beginTransaction();
hsession.update(obj);
htransaction.commit();
} catch (HibernateException ex) {
throw new DatabaseException(ex);
} finally {
if (hsession != null)
hsession.close();
}
}