春天& Hibernate java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0

时间:2012-01-04 14:21:04

标签: hibernate spring one-to-many

这是我的模特。与userInfo有多对一关系的keyInfo。

@Entity
@Table(name = "KEY_INFOS")
public class KeyInfo implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "KEY_ID")
private long id;

...

}

@Entity
@Table(name = "USER_INFOS")
public class UserInfo implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "USER_ID")
private long id;

@Column(name = "NAME", nullable = false, length=10)
private String name;

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(name = "USER_INFOS_KEY_INFOS", 
    joinColumns = { @JoinColumn(name = "USER_ID") }, 
    inverseJoinColumns = { @JoinColumn(name = "KEY_ID") })
private Set<KeyInfo> keyInfos = new HashSet<KeyInfo>(0);
....

}

我的数据访问类。它使用HibernateDaoSupport并实现自定义接口。还支持交易:

public class UserInfoDAOImplementation extends HibernateDaoSupport implements UserInfoDAO {

@Transactional
public void insertUserInfo(UserInfo userInfo) {
    getHibernateTemplate().save(userInfo);  
}

@Transactional
public List<UserInfo> getAllUsers() {
    return getHibernateTemplate().find("from " + UserInfo.class.getSimpleName());
}
}

但在这之后,特别是最后一行:

KeyInfoDAO keyInfoDAO = (KeyInfoDAO) context.getBean("keyInfoDAO");
            UserInfoDAO userInfoDAO = (UserInfoDAO) context.getBean("userInfoDAO");

            HashSet<KeyInfo> keyTimeInfos = ((KeyEventCollector) keyEventCollector).getKeyTimeInfos();
            if(nameText.getText() != null && nameText.getText().length() > 0) {
                UserInfo userInfo = new UserInfo(nameText.getText());

                userInfo.setKeyInfos(keyTimeInfos);
                userInfoDAO.insertUserInfo(userInfo);

                List<UserInfo> userInfos = userInfoDAO.getAllUsers();
                for(UserInfo userInfoTmp : userInfos) {
                    double quality = 0;

                    Set<KeyInfo> keyInfos = userInfoTmp.getKeyInfos();
                    System.out.println(keyInfos.size());
                }
            }

我有错误,提到查询字符串

Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at org.hibernate.type.descriptor.java.CharacterTypeDescriptor.wrap(CharacterTypeDescriptor.java:80)
at org.hibernate.type.descriptor.java.CharacterTypeDescriptor.wrap(CharacterTypeDescriptor.java:34)
at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$2.doExtract(VarcharTypeDescriptor.java:61)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:254)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:250)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:230)
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:331)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2283)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1527)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1455)
at org.hibernate.loader.Loader.getRow(Loader.java:1355)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:611)
at org.hibernate.loader.Loader.doQuery(Loader.java:829)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2166)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:479)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:900)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:279)
at org.hibernate.loader.Loader.doList(Loader.java:2542)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:459)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:365)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:921)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:904)
at com.recognizer.data.access.UserInfoDAOImplementation.getAllUsers(UserInfoDAOImplementation.java:21)
at com.recognizer.gui.CollectorFrame$1.actionPerformed(CollectorFrame.java:71)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

2 个答案:

答案 0 :(得分:1)

为澄清以上答案,如果一行中的字段仅包含一个空格(''),则会导致Spring Framework中断。尽管其他空白字符也能正常工作。

解决方案是永远不要在行的字段中使用''作为值。

即使不使用休眠模式,也会发生这种情况。这是Spring Framework和JPA的问题。

答案 1 :(得分:0)

这很奇怪,但错误是由表行列中只存在一个单一空格符号引起的。解决方案不是放置单个空间标志,尽管其他白色空间是正确的。

PS:关于同样的问题是HHH-7504,(正如预期的那样)是一个不会修复