![](/img/trans.png)
[英]Why do I have to deal with Throwable if I invoke getCause() on an Exception
[英]Why do i have a mapping exception?
現在,我想制作一個小型Java應用程序,以學習休眠框架。 但這給了我org.hibernate.MappingException: Repeated column in mapping for entity: model.Book column: author (should be mapped with insert="false" update="false")
。 並且,如果我從entitys.hbm.xml中刪除了作者列映射,那么它向我顯示了SQL消息“ SELEC FROM ...”,但是在那之后,它給了我兩個例外:
org.hibernate.exception.GenericJDBCException: could not execute query
java.sql.SQLException: No database selected.
誰能幫我?
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>
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實體:
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()方法。 您不應在每次需要休眠會話時都創建一個SessionFactory。 SessionFactory的創建過於昂貴(按時間衡量),因此每次都無法執行此操作。 最小的解決方案是您可以要求SessionFactory的Singleton類:
public class SessionFactoryUtil {
private static SessionFactory sessionFactory;
private SessionFactoryUtil() {}
static {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public static SessionFactory getInstance() { return sessionFactory; }
}
也許您應該在服務器內部定義數據庫/方案名稱。 當前,您僅指定數據庫服務器。
<property name = "hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/DATABASENAME</property>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.