0

我是新来的冬眠。将 4.2.2 版与 mysql 4 一起使用。我遇到了同样的错误。我不知道发生了什么帮助我。

hibernate.cfg.xml 文件

<hibernate-configuration>
<session-factory>

        <property name="hibernate.connction.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connction.url">jdbc:mysql://localhost:3306/HibernateDB</property>
        <property name="hibernate.connction.user_name">mysql</property>
        <property name="hibernate.connction.password">mysql</property>


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

        <property name="cache.provider_class">org.hibernate.cache.NocacheProvider</property>

        <property name="show_sql">true</property>

        <property name="hibernate.hbm2ddl">create</property>
        <property name="hbm2ddl">craete</property>

        <mapping class="org.shammu.UserDetails" />

</session-factory>
</hibernate-configuration>

用户详细信息类

package org.shammu;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserDetails {

@Id
private int userId;
private String userName;
public int getUserId() {
    return userId;
}
public void setUserId(int userId) {
    this.userId = userId;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
}

我的测试课是:

package org.shammu;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class TestClass {

/**
 * @param args
 */
public static void main(String[] args) {
    UserDetails user = new UserDetails();
    user.setUserId(1);
    user.setUserName("Shammu");
    @SuppressWarnings("deprecation")
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    session.save(user);
    session.getTransaction().commit();

}

}

控制台错误消息是:

Jun 28, 2013 1:02:27 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Jun 28, 2013 1:02:27 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.2.Final}
Jun 28, 2013 1:02:27 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 28, 2013 1:02:27 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 28, 2013 1:02:27 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jun 28, 2013 1:02:27 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jun 28, 2013 1:02:27 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jun 28, 2013 1:02:27 AM    org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Jun 28, 2013 1:02:27 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Jun 28, 2013 1:02:27 AM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Jun 28, 2013 1:02:27 AM  org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Jun 28, 2013 1:02:27 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" java.lang.UnsupportedOperationException: The application must supply JDBC connections
at  org.hibernate.service.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:62)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)
at org.shammu.TestClass.main(TestClass.java:19)

我已经在类路径中包含了 mysql-connector jar 请任何人帮助我..

4

1 回答 1

1

你的配置文件需要一点点改变:

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

看起来hibernate无法连接到数据库。还要确保您提供的连接 URL 与数据库凭据正确。

有关正确的属性名称集,请参阅此:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html

于 2013-06-27T20:15:33.483 回答