现在我想制作一个小型的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个例外:为什么我有映射异常?
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>
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;
}
哦,傻了。谢谢,一个问题已解决。但是第二个呢? – DaJackal 2010-09-26 12:49:58
为我的答案添加了第二个问题的解决方案。 – stefanglase 2010-09-26 12:55:48
非常感谢,我现在指定了实体xml文件中的数据库名称,并且它正在工作。现在我有另一个问题:我写这个查询,它是与懒取指?如果不是,我该怎么做? – DaJackal 2010-09-26 13:21:31