java 使用 Spring Hibernate 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7835389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems using Spring Hibernate
提问by why_vincent
I've been using Spring JDBC with great success but I am having alot of trouble with this project. I'll post the code links here(it's just a small and silly project to test if I can get it up and running so that I can use Hibernate in the future):
我一直在使用 Spring JDBC 并取得了巨大的成功,但我在这个项目中遇到了很多麻烦。我将在这里发布代码链接(这只是一个小而愚蠢的项目,用于测试我是否可以启动并运行它以便我将来可以使用 Hibernate):
xml-file: http://codepaste.net/uw19zc
xml 文件:http: //codepaste.net/uw19zc
main-file: http://codepaste.net/iks1cp
主文件:http: //codepaste.net/iks1cp
I get tons of errors such as
我收到大量错误,例如
[Fatal Error] bean2.out.xml:1:1: Premature end of file.
13:21:39,471 FATAL [main] Main - getAssociatedStylesheets failed
and I haven't created a.out.xml file.
我还没有创建 a.out.xml 文件。
回答by Xavi López
This error is due to incorrect parsing of the xml file. Using Eclipse to validate it gives the error:
此错误是由于对 xml 文件的错误解析造成的。使用 Eclipse 来验证它给出了错误:
cvc-complex-type.2.3: Element 'beans' cannot have character [children], because the type's content type is element-only.
cvc-complex-type.2.3:元素 'beans' 不能有字符 [children],因为该类型的内容类型是 element-only。
There appears to be some strange character in between one or many of those <bean>
declarations. Have you copied this text from somewhere else?
在这些<bean>
声明中的一个或多个之间似乎有一些奇怪的字符。您是否从其他地方复制了此文本?
Remove all spaces and newline characters between <bean>
definitions and put them back with your editor.
删除<bean>
定义之间的所有空格和换行符,并将它们放回您的编辑器中。
UPDATECopying & pasting into notepad++ the text in the codepaste you provided, and setting the charset to UTF-8 showed these characters in the blank lines: xA0. This is the standard Unicode translation for
. This is likely to be the cause of this problem.
更新将您提供的代码粘贴中的文本复制并粘贴到记事本 ++ 中,并将字符集设置为 UTF-8 会在空行中显示这些字符:xA0. 这是
. 这很可能是导致此问题的原因。
This validates ok for me:
这对我来说没问题:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/HibernateDB" />
<property name="username" value="HibernateDB" />
<property name="password" value="java" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="annotatedClasses">
<list>
<value>hdao.HibernateObject</value>
</list>
</property>
</bean><bean id="springHibernateOperator" class="hdao.SpringHibernateOperatorImplementation">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
回答by higuaro
If you are in linux, use cat -v file-name.xml
to detect special "invisible" characters like 'M-BM-' in your xml file
如果您使用的是 linux,请用于cat -v file-name.xml
检测 xml 文件中的特殊“不可见”字符,例如“M-BM-”
回答by ankit
Sometimes there are hidden characters in dependencies or some bean definitions, which you copy from some tutorial's website. best way to find out those hidden characters do a `
有时在依赖项或某些 bean 定义中存在隐藏字符,您可以从某些教程的网站复制这些字符。找出那些隐藏字符的最佳方法是`
ctrl + shift + F
ctrl + shift + F
this will format your document and you can see that hidden character in between some tag `
这将格式化您的文档,您可以看到某些标签之间的隐藏字符`