如何使用我的第一个Hibernate项目修复错误?

时间:2012-03-03 11:18:43

标签: java hibernate

我已经尝试过制作我的第一个Hibernate项目。我将以下库添加到我的项目中:

ANTLR-2.7.7 公地集合-3.2.1 dom4j的-1.6.1 hibernate-commons-annotations-4.0.1.Final hibernate-core-4.1.0.Final hibernate-jpa-2.0-api-1.0.1.Final Javassist进行-3.15.0-GA JBoss的日志记录,3.1.0.CR2 jboss-transaction-api_1.1_spec-1.0.0.Final MySQL的连接器的Java-5.1.18槽

hibernate.cfg.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

    <mapping class="com.nda.hibernate.Person"/>
</session-factory>
</hibernate-configuration>

Person.class:

package com.nda.hibernate;

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Person {

    private String name;

    @Id
    private int number;

    public Person() {}

    public Person(String name, int number) {

        this.name=name;
        this.number=number;
    }

    public void setName(String name) {
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public void setNumber(int number) {
        this.number=number;
    }

    public int getNumber() {
        return number;
    }
}

我安装了Denwer。使用参数:login - root,密码为空。服务器是localhost,并且有数据库&#34; test&#34; with table&#34; person&#34;(CHAR(50)name,INT(10)number - primary key)

Main.class:

    SessionFactory sessions=new AnnotationConfiguration().configure().buildSessionFactory();
    Session session=sessions.openSession();

它不起作用。我有一些错误:

03.03.2012 14:11:07 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
03.03.2012 14:11:07 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.0.Final}
03.03.2012 14:11:07 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
03.03.2012 14:11:07 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
03.03.2012 14:11:07 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
03.03.2012 14:11:07 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
03.03.2012 14:11:07 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
03.03.2012 14:11:07 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
03.03.2012 14:11:07 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
03.03.2012 14:11:07 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
03.03.2012 14:11:07 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
03.03.2012 14:11:07 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/test]
03.03.2012 14:11:07 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
03.03.2012 14:11:08 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
03.03.2012 14:11:08 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
03.03.2012 14:11:08 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory

我该如何解决?谢谢。

4 个答案:

答案 0 :(得分:26)

我也有同样的问题。 经过一些谷歌搜索,我解决了这个问题。

您所要做的就是将命名空间“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”更改为“http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd” 如Hibernate 3.6 Migration Guide中所述。

答案 1 :(得分:12)

这些对我来说都不像是错误 - 它们只是日志行。唯一的“问题”是这里的警告:

  

警告:HHH000223:已识别的过时的hibernate名称空间http://hibernate.sourceforge.net/。请改用名称空间http://www.hibernate.org/dtd/。请参阅Hibernate 3.6迁移指南!

这只是一个警告 - 值得修复,但听起来它并非非常紧急。

其他一切都只是在INFO级别......所以不要担心它们。继续前进,看看您的代码是否符合您对数据库访问的期望。

答案 2 :(得分:6)

这很容易 在您的文件xml hibernate配置中,您必须更改行

http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd

http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd

答案 3 :(得分:0)

Person.class 应该实现 serializable ,但似乎你遇到的主要问题是Hibernate 3.6迁移指南!

见。