Hibernate Basic示例不起作用

时间:2015-08-22 19:58:00

标签: java hibernate jdbc

我试图实现一个基本的Hibernate示例,但我无法让它工作。

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

public class SimpleTest {

    public static void main(String[] args) {

        SessionFactory sessionFactory = new Configuration().
                configure().buildSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();

        Lecturer lecturer1 = new Lecturer();
        lecturer1.setFirstName("Fatma");
        lecturer1.setLastName("Meawad");

        session.save(lecturer1);
        tx.commit();

        System.out.println
                ("The lecturer " + lecturer1.getFirstName()+ " "
                        + lecturer1.getLastName()+" is successfully added to your database");

    }
}

每当我尝试运行它时,我得到:

  

线程中的异常" main" org.hibernate.HibernateException:无法   make JDBC Connection [jdbc:mysql // 127.0.0.1:3306 / sampledb] at   org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:77)     在   org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)     在   org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)     在   org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)     在   org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)     在   org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)     在   org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)     在   org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)     在   org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)     在   org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)     在   org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)     在   org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)     在   org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)     在SimpleTest.main(SimpleTest.java:11)​​at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:483)at   com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我今天刚开始使用Hibernate,但经过一整天的尝试后,我无法获得一个基本的例子(我尝试了其他例子)。我忘记了什么?

编辑:我的hibernate.cfg.xml

    <?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>

        <!-- ________________ To be Edited _________________ -->

        <property name="connection.url">jdbc:mysql//127.0.0.1:3306/sampledb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">password</property>

        <!-- _____________ End of To be Edited ______________ -->


        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>


        <!-- _________ Defining the Mapping Files ___________ -->

        <mapping resource="Lecturer.hbm.xml" />

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

1 个答案:

答案 0 :(得分:0)

    <property name="hibernate.hbm2ddl.auto">update</property>

在你的hibernate.cfg.xml中添加它,然后再试一次