hibernate创建表出错,大神快来帮帮啊

养生程序猿 发布于 2013/11/28 11:35
阅读 307
收藏 0
SLF4J: This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1493)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1035)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1017)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1011)
at com.zhuoshang.model.gggl.GgTypeEntity.main(GgTypeEntity.java:122)
Caused by: org.xml.sax.SAXParseException: Attribute "value" must be declared for element type "property".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(XMLDTDValidator.java:1275)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(XMLDTDValidator.java:1940)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:785)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:377)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2747)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1490)
... 8 more


我的配置文件

<?xml version='1.0' encoding='UTF-8'?>
 <!DOCTYPE hibernate-configuration PUBLIC
           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
   <property name="datasource">
    org.apache.commons.dbcp.BasicDataSource
   </property>
   <property name="connection.url">
    jdbc:sqlserver://192.168.0.107:1433;databaseName=baiyi
   </property>
   <property name="connection.username">sa</property>
   <property name="connection.password">qwe123</property>
   <property name="connection.driver_class"> com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
   <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
   <property name="format_sql">true</property>
   <property name="show_sql">true</property>
   <property name="current_session_context_class">thread</property>
   
   <mapping class="com.zhuoshang.model.gggl.GgTypeEntity" />
  </session-factory>
 
 </hibernate-configuration>

加载中
1
QAllen
QAllen

看这句  Caused by:  org.xml.sax.SAXParseException: Attribute "value" must be declared for element type "property".    

<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/> 是不是写错了

应该这样:

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  

养生程序猿
养生程序猿
我改成这样的了,但是我用main方法还是启动不了
养生程序猿
养生程序猿
我用的是SQLServer
1
打一把
打一把

提示很明白:

Exception in thread "main" org.hibernate.MappingException: invalid configuration 
Caused by: org.xml.sax.SAXParseException: Attribute "value" must be declared for element type "property".

意思是说,你的Hibernate配置文件有问题,映射的时候,property元素必须要有value属性。你检查下你的配置文件呗。


打一把
打一把
回复 @springsky : 用main创建跟Hibernate没关系吧?你看看你方法是不是写的有问题?mysql跟sqlserver是一样的吗?
养生程序猿
养生程序猿
回复 @Sprieo : 我用的是main方法创建的,创建mysql表 就可以
打一把
打一把
回复 @springsky : 自动建表语句不是在这个地方配置的 。你参考下这个位置:http://blog.csdn.net/lixuyuan/article/details/8057119
养生程序猿
养生程序猿
恩,我把配置文件改了,但是启动的时候不报错也不创建表。。。
返回顶部
顶部