Hi, I'm coding a simple program that lists and adds book to a table just for practice. Listing works fine, but i cant add book, got a exception and trying to solve it for 3 hours. I'll try to keep this message simple.
Hibernate version: 3.2
I'm using JSF, with a hibernate session control filter. (one session per request)
There are,
a
Book class mapped to book table.
Code:
<hibernate-mapping>
<class name="com.invent.model.Book" table="book" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="increment" />
</id>
<property name="author" type="java.lang.String">
<column name="Author" length="25" />
</property>
<property name="title" type="java.lang.String">
<column name="Title" length="25" />
</property>
<property name="available" type="java.lang.Boolean">
<column name="Available" />
</property>
</class>
</hibernate-mapping>
a
BookDelegate to list / add . (managed bean, request scope)
Code:
public class BookDelegate
{
private long id;
private String author;
private String title;
private boolean available;
.. getters/setters..
public Collection getBooks()
{
Session session = HibSessionFilter.getSession();
return session.createQuery("from Book").list();
}
public void addBook(ActionEvent event)
{
Session session = HibSessionFilter.getSession();
try
{
session.saveOrUpdate(new Book(this.id, this.author, this.title, this.available));
session.flush();
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
a
editBook.jsf to add books.
Code:
<h:form>
<h:panelGrid columns="2" border="1">
<h:outputText value="Author:" />
<h:inputText id="author" value="#{BookDelegate.author}" />
<h:outputText value="Title:" />
<h:inputText id="title" value="#{BookDelegate.title}" />
<h:outputText value="Available:" />
<h:selectBooleanCheckbox id="available" value="#{BookDelegate.available}" />
</h:panelGrid>
<h:commandButton value="Save" action="listBooks" actionListener="#{BookDelegate.addBook}" />
</h:form>
There are 2 records in DB. The next ID will be 3. When BookDelegate.addBook executed,
Exception:
identifier of an instance of com.invent.model.Book was altered from 3 to 3
exception occurs. If I try adding another book I got,
identifier of an instance of com.invent.model.Book was altered from 4 to 4
... (No record inserted to table, max(ID) of table is still 2.)
The generated SQL (show_sql=true):
Hibernate: select max(ID) from book
Name and version of the database you are using:
Mysql 5.1
Any help will be greatly appreaciated.
- GB