java - 为什么我有映射异常?

标签 java mysql hibernate orm

现在我想做一个小的java应用程序,以学习hibernate框架。但它给了我 org.hibernate.MappingException: Repeated column in mapping for entity: model.Book column: author (should be mapped with insert="false"update="false")。如果我从 entities.hbm.xml 中删除作者列映射,那么它会显示 sql 消息“SELEC FROM ...”,但在那之后,它给了我 2 个异常(exception):

  • org.hibernate.exception.GenericJDBCException: 无法执行查询
  • java.sql.SQLException: 没有选择数据库。

谁能帮帮我?

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.driver_class">com.mysql.jdbc.Driver</property>
    <property name = "hibernate.connection.url">jdbc:mysql://127.0.0.1:3306</property>
    <property name = "hibernate.connection.username">root</property>
    <property name = "hibernate.connection.password"></property>
    <property name = "hibernate.connection.pool_size">10</property>
    <property name = "dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name = "hibernate.hbm2ddl.auto">update</property>
    <property name = "show_sql">true</property>

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

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

entities.hbm.xml 文件:

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

<hibernate-mapping package = "model">

   <class name = "Genre" table = "virtual bookcase.genres">
      <id name = "id" column = "idGenres" type = "long">
        <generator class = "increment"/>
      </id>   
      <property name = "name" column = "name" type = "string"/>
      <set name = "books" table = "books" cascade = "all-delete-orphan">
        <key column = "idGenres" not-null = "true" />
        <one-to-many class = "Book"/>
      </set>
   </class>

   <class name = "Book" table = "virtual bookcase.books">
        <id name = "id" column = "idBooks" type = "long">
         <generator class = "increment"/>
        </id>
        <property name = "title" column = "title" type = "string"/>
        <property name = "author" column = "author" type = "string"/>
        <property name = "publisher" column = "author" type = "string"/>
        <property name = "pages" column = "pages" type = "short"/>
        <property name = "borrowed" column = "borrowed" type = "byte"/>
        <property name = "borrowedTo" column = "borrowedTo" type = "string"/>
   </class>

</hibernate-mapping>

Java 实体:

public class Genre
{
    private long id;
    private String name;
    private Set<Book> books;

    public long getId()
    {
        return id;
    }

    public String getName()
    {
        return name;
    }

    public void setId(long id)
    {
        this.id = id;
    }

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

    public Set<Book> getBooks()
    {
        return books;
    }

    public void setBooks(Set<Book> books)
    {
        this.books = books;
    }

    @Override
    public String toString()
    {
        return name;
    }
}

getBooks() 方法:

public Set<Book> getBooks()
    {
        Set<Book> books = null;

        connect();

        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();

        Query q = s.createQuery("FROM Book");
        books = new TreeSet<Book>(q.list());

        for (Book b : books)
            System.out.println(b);

        s.close();
        sf.close();

        disconnect();

        return books;
    }

最佳答案

您有两个属性映射到列作者:

<property name="author" column="author" type="string"/>
<property name="publisher" column="author" type="string"/>

要解决第二个错误,请将数据库名称附加到您的 JDBC 连接 URL:

<property name="hibernate.connection.url">
    jdbc:mysql://127.0.0.1:3306/dbname
</property>

进一步阅读您的资料后,我无意中发现了您的 getBooks() 方法。您不应该在每次需要 Hibernate Session 时都创建 SessionFactory。 SessionFactory 的创建成本太高(以时间衡量),无法每次都执行此操作。最低限度的解决方案是一个 Singleton 类,您可以请求 SessionFactory:

public class SessionFactoryUtil {

    private static SessionFactory sessionFactory;

    private SessionFactoryUtil() {}

    static {
       sessionFactory = new Configuration().configure().buildSessionFactory();
    }

    public static SessionFactory getInstance() { return sessionFactory; }

}

关于java - 为什么我有映射异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3797823/

相关文章:

java - RabbitMQ 同一队列的多个消费者

mysql - 使用 select 时出错 Mysql 插入查询(适用于 Postgres/H2)

mysql - Tomcat、Spring、Hibernate 应用程序 - 用户报告 session 混淆

php - 使用ajax jquery运行php脚本

java - 捕获MySQL的信息字符串输出-用Java编码时加载数据INFILE

mysql - 我无法更新我的实体表(Spring Boot 2 hibernate 5)

java - 结合 delete-orphan 和 where 条件

Java 服务器代码将数据传递给正在运行的 java 程序

java - WebSocket session 之间共享的所有成员是否应该同步?

java.io.IOException : Read-only file system 异常