使用Hibernate将jTable中的所有行添加到MS SQL Server DB

时间:2014-08-30 06:54:10

标签: java sql sql-server swing hibernate

我需要将jTable中的所有行插入到MS SQL server DB中。我正在使用hibernate。这是我尝试过的代码。如果我正在尝试打印spnamevreqvcolcnnamesta,则会打印,但我无法将这些值加载到DB因为循环只工作一次,所以它只插入一次。

我需要将spnamevreqvcolcnnamesta的多个值加载到同一个Labitemcode中。如果有人在这个问题上帮助我,将会很有帮助。

try {
    queries();
    Login.session = Login.sessionfactory.openSession();
    Login.session.beginTransaction();
    for (int row = 0; row < jTable3.getRowCount(); row++) {
        TstSpecimenPojo tsp = new TstSpecimenPojo();
        String spname = (String) jTable3.getModel().getValueAt(row, 0);
        String vreq = (String) jTable3.getModel().getValueAt(row, 1);
        String vcol = (String) jTable3.getModel().getValueAt(row, 2);
        String cnname = (String) jTable3.getModel().getValueAt(row, 3);
        int sta = (int) jTable3.getModel().getValueAt(row, 4);
        tsp.setLabitemcode(Labitemcode);
        tsp.setSpecimenid(spname);
        tsp.setVolreq(vreq);
        tsp.setVolcol(vcol);
        tsp.setContid(cnname);
        tsp.setTststatus(sta);
        Login.session.save(tsp);
    }
    Login.session.getTransaction().commit();
} catch (HibernateException e) {
    System.out.println(e.getMessage());
}

printStackTrace()的结果

org.hibernate.exception.DataException: could not insert: [POJO.TstSpecimenPojo]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:77)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
    at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
    at MAIN.ServiceMapping.saveAction(ServiceMapping.java:669)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at trevahms.MainScreen.save(MainScreen.java:442)
    at trevahms.MainScreen.saveButton3Action(MainScreen.java:429)
    at trevahms.MainScreen.access$200(MainScreen.java:36)
    at trevahms.MainScreen$3.actionPerformed(MainScreen.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.sql.DataTruncation: Data truncation
    at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:386)
    at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988)
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421)
    at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:671)
    at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:613)
    at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:572)
    at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:727)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
    ... 60 more

1 个答案:

答案 0 :(得分:1)

标识种子值的整数溢出可能会导致此问题。

此案例有三种快速解决方案:

1 - )您可以使用

更改您的表格
ALTER TABLE table_name
ALTER COLUMN id_column_name bigint

2-)如果Identity Seed很大但某些id值范围不存在你可以重新种子为1或任何其他空种子范围:

DBCC CHECKIDENT ('table_name', RESEED, 1)

3-)在management Studio中,右键单击该表,然后按照脚本表as-> create to-> New Query Editor Window

更改" int"输入" bigint"为您的ID列。在窗口中保留此创建新表命令。然后重命名您的表,最后执行我们刚刚保留的create table命令。然后您可以将数据从旧的(重命名的)表移动到新表中(我建议使用SSIS包,导入数据功能以移动大数据)