将java对象映射到数据库
我正在尝试将java对象映射到数据库(我正在使用postgres)。当我运行java文件(HibernateTest.java)时,它显示了很多错误。我认为一切都是正确的。但我对我的eclipse表示怀疑,因为它不支持xml(我的意思是它没有显示颜色,它没有将xml文件作为已知文件)。我是初学者,所以我不能理解他们。请推荐我将java对象映射到数据库,java,database,hibernate,object,mapping,Java,Database,Hibernate,Object,Mapping,我正在尝试将java对象映射到数据库(我正在使用postgres)。当我运行java文件(HibernateTest.java)时,它显示了很多错误。我认为一切都是正确的。但我对我的eclipse表示怀疑,因为它不支持xml(我的意思是它没有显示颜色,它没有将xml文件作为已知文件)。我是初学者,所以我不能理解他们。请推荐我 hibernate.cfg.xml文件 <?xml version='1.0' encoding='utf-8'?> <hibernat
hibernate.cfg.xml
文件
<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost:5432/hibernatedb</property>
<property name="connection.username">postgres</property>
<property name="connection.password">hkm@1160</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQlDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="org.hibernate.hemant.dto.UDestails"/>
</session-factory>
</hibernate-configuration>
我的主java文件hibernatest.java
}
有错误
2016年4月13日9:26:08 PM org.hibernate.Version日志版本
信息:hh000412:Hibernate核心{5.1.0.Final}
2016年4月13日晚上9:26:08 org.hibernate.cfg.Environment
信息:HH000206:找不到hibernate.properties
2016年4月13日晚上9:26:08 org.hibernate.cfg.Environment buildBytecodeProvider
信息:HH000021:字节码提供程序名称:线程“main”中的Javassitexception
线程“main”org.hibernate.internal.util.config.ConfigurationException中出现异常:无法在资源hibernate.cfg.xml的第13行和第63列执行解组。消息:cvc elt.1:找不到元素“hibernate配置”的声明。
位于org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
位于org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
位于org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
位于org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
位于org.hibernate.cfg.Configuration.configure(Configuration.java:259)
位于org.hibernate.cfg.Configuration.configure(Configuration.java:245)
位于org.hemant.hibernate.HibernateTest.main(HibernateTest.java:15)
原因:javax.xml.bind.UnmarshalException
-除此之外:
[org.xml.sax.SAXParseException;行号:13;列号:63;cvc elt.1:找不到元素“hibernate配置”的声明。]
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(未知源)
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(未知源)
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(未知源)
位于org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:126)
... 还有6个
原因:org.xml.sax.saxpasseeption;行号:13;栏目号:63;cvc elt.1:找不到元素“hibernate配置”的声明。
位于com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(未知源)
位于com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(未知源)
位于com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(未知来源)
位于com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(未知来源)
位于com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(未知来源)
位于com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handlestarelement(未知来源)
位于com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(未知来源)
位于com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(未知来源)
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(未知源)
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(未知源)
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.statxeventconnector.handlestarelement(未知源)
位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.statxeventconnector.bridge(未知源)
... 9更多
异常的原因是一个不正确的hibernate.cfg.xml
文件。
请用这个结构
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
...
<mapping class="org.hibernate.hemant.dto.UDestails" />
</session-factory>
</hibernate-configuration>
...
您在hibernate.cfg.xml
中指定了一个不正确的持久类。您使用带注释的映射,因此需要映射类
而不是资源
请不要忘记关闭
会话
和会话工厂
你必须更明确地说明你的错误。对不起,我忘记添加错误了……现在我已经把所有的错误都放进去了@扎卡里亚
package org.hemant.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.hemant.dto.UserDetails;
public class HibernateTest {
/**
* @param args
*/
public static void main(String[] args) {
UserDetails user = new UserDetails();
user.setUserID(1);
user.setUserName("First user");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.beginTransaction().commit();
}
Apr 13, 2016 9:26:08 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
Apr 13, 2016 9:26:08 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Apr 13, 2016 9:26:08 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassistException in thread "main"
Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 13 and column 63 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:259)
at org.hibernate.cfg.Configuration.configure(Configuration.java:245)
at org.hemant.hibernate.HibernateTest.main(HibernateTest.java:15)
Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 63; cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:126)
... 6 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 63; cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.handleStartElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(Unknown Source)
... 9 more
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
...
<mapping class="org.hibernate.hemant.dto.UDestails" />
</session-factory>
</hibernate-configuration>