无法将数据插入HSQL DB(内存中)。请帮助我理解错误,来自控制台的文字:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
insert
into
author
(ID, NAME, COUNTRY)
values
(default, ?, ?)
Hibernate:
insert
into
book
(ID, NAME, GENRE, AUTHORID)
values
(default, ?, ?, ?)
Hibernate:
insert
into
book
(ID, NAME, GENRE, AUTHORID)
values
(default, ?, ?, ?)
org.hibernate.HibernateException: identifier of an instance of com.maven.vaadin.bookshelf.Author was altered from 1 to null
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:85)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.maven.vaadin.bookshelf.DBManager.save(DBManager.java:50)
at com.maven.vaadin.bookshelf.MyVaadinUI.init(MyVaadinUI.java:43)
at com.vaadin.ui.UI.doInit(UI.java:614)
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:223)
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:73)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Hibernate:
select
author0_.ID as ID0_,
author0_.NAME as NAME0_,
author0_.COUNTRY as COUNTRY0_
from
author author0_
List size: 0
连接到数据库的Hibernate配置和指向映射文件的链接
<hibernate-configuration>
<session-factory>
<!-- Database connection settings, Connect to HSQL, IN Memory -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:mem:test</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property><property name="format_sql">true</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!--create the database schema on startup if required -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/maven/vaadin/bookshelf/Author.hbm.xml"/>
<mapping resource="com/maven/vaadin/bookshelf/Book.hbm.xml"/>
</session-factory>
Hibernate映射文件。第一:
<hibernate-mapping package="com.maven.vaadin.bookshelf">
<class name="com.maven.vaadin.bookshelf.Author" table="author">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="64" not-null="true" unique="true" />
</property>
<property name="country" type="java.lang.String">
<column name="COUNTRY" length="32" not-null="true" />
</property>
<set name="authorbook" table="book" cascade="all" inverse="true">
<key column="AUTHORID" not-null="true" />
<one-to-many class="com.maven.vaadin.bookshelf.Book" />
</set>
</class>
第二
<hibernate-mapping package="com.maven.vaadin.bookshelf">
<class name="com.maven.vaadin.bookshelf.Book" table="book">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="32" not-null="true" />
</property>
<property name="genre" type="java.lang.String">
<column name="GENRE" length="32" not-null="true" />
</property>
<many-to-one name="author" class="com.maven.vaadin.bookshelf.Author" not-null="true">
<column name="AUTHORID" />
</many-to-one>
</class>
Java类:
public class Author implements Serializable{
private Long id;
private String name;
private String country;
private Set<Book> authorbook = new HashSet<Book>();
public Author(String name, String country, Set<Book> authorBook){
super();
this.name = name;
this.country = country;
this.authorbook = authorbook;
}
public Author(){}
public void setId(Long Id){this.id = id;}
public Long getId(){return id;}
public void setName(String name){this.name = name;}
public String getName(){return name;}
public void setCountry(String country){this.country = country;}
public String getCountry(){return country;}
public void setAuthorbook(Set<Book> authorBook){this.authorbook = authorBook;}
public Set<Book> getAuthorbook(){return authorbook;}
public void addBook(Book book){
book.setAuthor(this);
this.authorbook.add(book);
}}
public class Book implements Serializable{
private Long id;
private String name;
private String genre;
private Author author;
public Book(String name, String genre){
super();
this.name = name;
this.genre = genre;
}
public Book(){}
public void setId(Long id){this.id = id;}
public Long getId(){return id;}
public void setName(String name){this.name = name;}
public String getName(){return name;}
public void setGenre(String genre){this.genre = genre;}
public String getGenre(){return genre;}
public void setAuthor(Author author){this.author = author;}
public Author getAuthor(){return author;}}
public class DBManager {
public static void main(String[] args){
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
DBManager app = new DBManager();
app.save("Author","Country");
app.list();
}
public void save(String authorName, String authorCountry){
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Long id = null;
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Author author = new Author();
author.setName(authorName);
author.setCountry(authorCountry);
Book bk1 = new Book();
Book bk2 = new Book();
bk1.setName("Book1");;
bk1.setGenre("Genre1");
bk2.setName("Book2");;
bk2.setGenre("Genre2");
author.addBook(bk1);
author.addBook(bk2);
session.save(author);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void list() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
@SuppressWarnings("unchecked")
List<Author> list = session.createQuery("FROM Author").list();
System.out.println("List size: " + (list).size());
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Author author = (Author) iterator.next();
}
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}}
答案 0 :(得分:2)
属性ID的setter方法存在问题。您已通过 ID 而非 ID ( 我是大写 ) - 为此hibernate抛出异常 您应该将setter方法参数名称从Id更改为id,它将起作用。
public void setId(Long id) {
this.id = id;
}
即使您从this.id = id更改为this.id = Id,它也可以正常工作。
public void setId(Long Id) {
this.id = Id;
}