【问题标题】:how to connect to MySql with Hbernate using Eclipse如何使用 Eclipse 通过 Hibernate 连接到 MySql
【发布时间】:2011-12-05 02:38:58
【问题描述】:

我是休眠和日食的新手。通过观看 youtube 视频,我成功地使用了 Derby。现在我想用 MySql 来做。

我已经成功连接 MySQL 到 eclips

接下来我写了简单的持久类人

导入 javax.persistence.Entity; 导入 javax.persistence.Id;

@实体 公共类人{

private String name;
private int id;


public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Id
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

}


接下来我为此编写了一个测试类

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;


public class PersonTest {

    /**
     * @param args
     */
    public static void main(String[] args) {

        AnnotationConfiguration cfg = new AnnotationConfiguration();
        cfg.addAnnotatedClass(Person.class);
        cfg.configure("hibernate.cfg.xml");

        new SchemaExport(cfg).create(true, true);

        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();

        Person person = new Person();

        person.setName("Alex");
        person.setId(1);

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

    }

}

我的 Hibernate 配置文件

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

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/ruwaKuppi</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>
        <property name="hibernate.default_schema">ruwaKuppi</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="person.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

我的映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="Person" table="PERSON">
   <id name="id" type="int" column="ID" >
   <generator class="assigned"/>
  </id>

  <property name="name">
   <column name="NAME" />
  </property>

 </class>
</hibernate-mapping>

现在我收到此错误.... ![在此处输入图片描述][2]

13:13:17,795 信息版本:15 - Hibernate Annotations 3.4.0.GA 13:13:17,818 信息环境:560 - 休眠 3.3.2.GA 13:13:17,821 信息环境:593 - hibernate.properties 未找到 13:13:17,825 信息环境:771 - 字节码提供程序名称:javassist 13:13:17,830 INFO Environment:652 - 使用 JDK 1.4 java.sql.Timestamp 处理 13:13:17,927 信息版本:14 - Hibernate Commons Annotations 3.1.0.GA 13:13:17,949 信息配置:1474 - 从资源配置:hibernate.cfg.xml 13:13:17,950 信息配置:1451 - 配置资源:hibernate.cfg.xml 13:13:18,059 信息配置:600 - 从资源读取映射:person.hbm.xml 13:13:18,147 信息配置:1589 - 配置的 SessionFactory:空 13:13:18,173 信息方言:175 - 使用方言:org.hibernate.dialect.MySQLDialect 13:13:18,307 信息 HbmBinder:322 - 映射类:Person -> PERSON 13:13:18,326 INFO AnnotationBinder:419 - 来自注释类的绑定实体:Person 13:13:18,364 信息映射:161 - 重复导入:Person->Person 13:13:18,367 INFO EntityBinder:422 - 在表 Person 上绑定实体 Person 线程“主”org.hibernate.DuplicateMappingException 中的异常:重复的类/实体映射人 在 org.hibernate.cfg.Mappings.addClass(Mappings.java:141) 在 org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:789) 在 org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546) 在 org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291) 在 org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:803) 在 org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:128) 在 org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:91) 在 PersonTest.main(PersonTest.java:18)

请任何人帮助我摆脱这个烂摊子...非常感谢您的关注..

p.s :我知道最后的代码不清楚....我认为这是上面的重要行

13:13:18,367 INFO EntityBinder:422 - 在表 Person 上绑定实体 Person 线程“主”org.hibernate.DuplicateMappingException 中的异常:重复的类/实体映射人 在 org.hibernate.cfg.Mappings.addClass(Mappings.java:141)

【问题讨论】:

  • 亲爱的 JB NIzet
    我删除了它,我得到了新的错误,(我之前在插入 person.hbm.xml 文件之前遇到了这个错误)现在我把它找回来了 14:33:23,608 INFO SessionFactoryObjectFactory:105 - 未将工厂绑定到 JNDI,未配置 JNDI 名称 14:33:23,612 INFO SchemaUpdate:155 - 在线程“main”中运行 hbm2ddl 模式更新异常 org.hibernate.HibernateException:No 'CurrentSessionContext 已配置!在 org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:620) 在 PersonTest.main(PersonTest.java:21) 我现在该怎么办..提前谢谢
  • thread 添加到您的 XML 配置文件中,并阅读 docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/… 以了解其含义。

标签: java mysql eclipse hibernate orm


【解决方案1】:

人物类文件

@Entity public class Person {

private String name;
@Id
private int id;



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

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

}

测试类 公共类 PersonTest {

/**
 * @param args
 */
public static void main(String[] args) {
 Person person = new Person();

    person.setName("Alex");
    person.setId(1);

    SessionFactory factory =  Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    session.beginTransaction();
    session.save(person);
    session.getTransaction().commit();

}

}

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>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/ruwaKuppi</property>
    <property name="connection.username">root</property>
    <property name="connection.password">1234</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

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

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.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>

    <!-- Names the annotated entity class -->
    <mapping class="Put your package name and class name here"/>

</session-factory>

</hibernate-configuration>

无需添加映射文件 确保您的休眠 jar 包含在 jdbc jar 中。 包括来自 lib-bytecode-javassist 的 jar, jpa 和必需的。 检查 hibernate.cfg.xml 文件,它应该包含在 src 文件夹中

【讨论】:

    【解决方案2】:

    从 Person 类的 id 的 getter 中移除注解 '@id'

    【讨论】:

      【解决方案3】:

      您告诉 Hibernate 从 hibernate.cfg.xml 文件加载映射:

      cfg.configure("hibernate.cfg.xml");
      

      但您也告诉它添加 Person 类作为映射槽注释实体:

      cfg.addAnnotatedClass(Person.class);
      

      决定是使用注释还是使用 XML 来映射 Person 实体。如果您选择 XML,则删除 cfg.addAnnotatedClass(Person.class); 行。如果您选择注释,则从 XML 文件中删除 &lt;class name="Person" table="PERSON"&gt;(当然还有它的所有子元素)。

      【讨论】:

      • 那我应该从 hibernate.cfg.xml 中删除 吗??
      • 亲爱的 JB NIzet
        我删除了它,我得到了新的错误,(我之前在插入 person.hbm.xml 文件之前遇到了这个错误)现在我把它找回来了 14:33:23,608 INFO SessionFactoryObjectFactory:105 - 未将工厂绑定到 JNDI,未配置 JNDI 名称 14:33:23,612 INFO SchemaUpdate:155 - 在线程“main”中运行 hbm2ddl 模式更新异常 org.hibernate.HibernateException:No 'CurrentSessionContext 已配置!在 org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:‌ ​620) 在 PersonTest.main(PersonTest.java:21) 我现在该怎么办..提前谢谢
      猜你喜欢
      • 1970-01-01
      • 2011-03-18
      • 2020-02-07
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 2018-12-12
      • 1970-01-01
      相关资源
      最近更新 更多