简体   繁体   English

为什么会有映射异常?

[英]Why do i have a mapping exception?

Right now i want to make a small java application, in order to learn the hibernate framework. 现在,我想制作一个小型Java应用程序,以学习休眠框架。 But it gives me the org.hibernate.MappingException: Repeated column in mapping for entity: model.Book column: author (should be mapped with insert="false" update="false") . 但这给了我org.hibernate.MappingException: Repeated column in mapping for entity: model.Book column: author (should be mapped with insert="false" update="false") And if I delete the author column mapping from entities.hbm.xml, then it shows me that sql message "SELEC FROM ..." but after that, it gives me 2 exceptions: 并且,如果我从entitys.hbm.xml中删除了作者列映射,那么它向我显示了SQL消息“ SELEC FROM ...”,但是在那之后,它给了我两个例外:

  • org.hibernate.exception.GenericJDBCException: could not execute query
  • java.sql.SQLException: No database selected.

Can anyone help me? 谁能帮我?

hibernate.cfg.xml file: 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 file: Entitys.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 entities: 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() method: 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;
    }

You have two properties mapped to the column author: 您有两个属性映射到列作者:

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

To solve the second error append the database name to your JDBC connection URL: 要解决第二个错误,请将数据库名称附加到您的JDBC连接URL:

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

Reading through your sources further I stumbled over your getBooks()-method. 进一步阅读您的资料后,我偶然发现了您的getBooks()方法。 You should not create a SessionFactory every time you need a Hibernate Session. 您不应在每次需要休眠会话时都创建一个SessionFactory。 The creation of the SessionFactory is too expensive (measured in time) to perform this every time. SessionFactory的创建过于昂贵(按时间衡量),因此每次都无法执行此操作。 A minimum solution would be a Singleton class you can ask for the SessionFactory: 最小的解决方案是您可以要求SessionFactory的Singleton类:

public class SessionFactoryUtil {

    private static SessionFactory sessionFactory;

    private SessionFactoryUtil() {}

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

    public static SessionFactory getInstance() { return sessionFactory; }

}

Maybe you should define the database/schema name inside the server. 也许您应该在服务器内部定义数据库/方案名称。 Currently you specify only the database server. 当前,您仅指定数据库服务器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM