voyent
[SOLVED] Hibernate und IceFaces - PrimaryKey  XML
Forum Index -> General Help
Author Message
cuchulainn

Joined: 04/Sep/2008 00:00:00
Messages: 88
Offline


Hi,

I am writing some servlets with IceFaces and Hibernate. First, I used mapping files and everything worked fine, but now I want to try annotations. The data are displayed in my DataTable, but if I try to insert a new record into the table, I get an exception.

The first record with the primary key 0 is no problem, but the next ones. The jspx-page seems to set the primary key id = 0, but I think it has to be null, when Hibernate creates an insert-statement. So, every new record gets the primary key 0, but there is a record with primary key 0 -> The new record cannot be inserted.

I tried the GenerationTypes AUTO, IDENTITY and SEQUENCE.

Book.java with annotations:

Code:
 import javax.faces.event.ActionEvent;
 import javax.persistence.*;
 
 @Entity
 @Table (name = "book")
 public class Book {
 	private int bookId;
 	private String title;
 	private String author;
 	private int pages;
 	private boolean selected;
 	
 	public Book(int bookId, String title, String author, int pages) {
 		System.out.println("Konstruktor");
 		this.bookId = bookId;
 		this.title = title;
 		this.author = author;
 		this.pages = pages;
 	}
 	
 	public Book() {
 		System.out.println("Standardkonstruktor");
 	}
 	
 	@Id
 	@GeneratedValue(strategy = GenerationType.SEQUENCE)
 	public int getBookId() {
 		return bookId;
 	}
 
 	public void setBookId(int bookId) {
 		this.bookId = bookId;
 	}
 
 	public String getTitle() {
 		return title;
 	}
 	public void setTitle(String title) {
 		this.title = title;
 	}
 	public String getAuthor() {
 		return author;
 	}
 	public void setAuthor(String author) {
 		this.author = author;
 	}
 	public int getPages() {
 		return pages;
 	}
 	public void setPages(int pages) {
 		this.pages = pages;
 	}
 
 	@Transient
 	public boolean isSelected() {
 		return selected;
 	}
 
 	public void setSelected(boolean selected) {
 		this.selected = selected;
 	}
 	
 	public void addBook(ActionEvent actionEvent) {
 		try {
 			BookService.addBook(this);
 			this.setAuthor("");
 			this.setPages(0);
 			this.setTitle("");			
 		} catch(Exception e) { 
 
 		}
 	}	
 }
 


The addBook-method
Code:
 	public static void addBook(Book book) throws Exception {
 		Session session = null;
 
 		try {
 			session = HibernateUtil.getSessionFactory().getCurrentSession();
 			session.beginTransaction();
 			session.save(book);
 			session.flush();
 			session.getTransaction().commit();
 		}
 		catch(Exception e) {
 			throw new Exception(e);
 		}
 		finally {
 			HibernateUtil.closeSession(session);
 		}
 	}
 


addBook.jspx

Code:
 <?xml version="1.0" encoding="ISO-8859-1" ?>
 <jsp:root version="1.2" 
   xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:f="http://java.sun.com/jsf/core"
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:ice="http://www.icesoft.com/icefaces/component">
 <jsp:directive.page contentType="text/html;charset=ISO-8859-1" pageEncoding="ISO-8859-1"/>
 <f:view>
   <ice:outputDeclaration doctypeRoot="HTML" doctypePublic="-//W3C//DTD HTML 4.01 Transitional//EN" doctypeSystem="http://www.w3.org/TR/html4/loose.dtd"/>
   <html>
     <head>
      <title><ice:outputText value="MyOpac - Buch hinzufuegen" /></title>
      <ice:outputStyle href="./xmlhttp/css/xp/xp.css" />
     </head>
     <body>
 		<ice:form partialSubmit="true" >   	
      		<!-- Titel -->
      		<ice:outputLabel for="" value="Titel:"></ice:outputLabel>
 			<ice:inputText value="#{book.title}"></ice:inputText>
 			<br />
 			
 			<!--  Autor -->
 			<ice:outputLabel for="" value="Autor:"></ice:outputLabel>
 			<ice:inputText value="#{book.author}"></ice:inputText>
 			<br />
 			
 			<!-- Seitenanzahl -->
 			<ice:outputLabel for="" value="Seitenanzahl:"></ice:outputLabel>
 			<ice:inputText value="#{book.pages}"></ice:inputText>
 			<br />
 
 			<ice:commandButton action="bookList" value="Zurueck"></ice:commandButton>
 			<ice:messages />
 
 			<ice:commandButton value="Anlegen" action="bookList" actionListener="#{book.addBook}"></ice:commandButton>
      </ice:form>
 
     </body>
   </html>
 </f:view>
 </jsp:root>
 


Does anybody know how to solve this problem?

Dipl.-Inf. (FH) Christopher Olbertz
Hochschule für Technik und Wirtschaft des Saarlandes
http://www.htw-saarland.de
cuchulainn

Joined: 04/Sep/2008 00:00:00
Messages: 88
Offline


Hi,

I found the solution. There was no identity-column in my table. When I insert one, the jsp works.

For all, who have the same problem, here the creation of a table with an identity-column:

create table myTable (id integer auto_increment primary key, field1 varchar(75));

Then it works at least with the generationtypes AUTO and IDENTITY.

Dipl.-Inf. (FH) Christopher Olbertz
Hochschule für Technik und Wirtschaft des Saarlandes
http://www.htw-saarland.de
edykory


Joined: 27/Nov/2006 00:00:00
Messages: 332
Offline


Hello,

Don't wanna offend you, but this ain't in my opinion the best approach.

First of all, you should try to separate concerns: you have an entity - Book, a database service - BookService ... why don't you also create a controller - BookController; things will become much clearer. In this controller you can have a field - Book book ... Now, on the controller's addBook method you could do something like :
Code:
public void addBook(ActionEvent actionEvent) {
  	try {
  		BookService.addBook(book);
  		book = new Book(); 
 		// or book = null, depending on how you design 
 		// your interface / behaviour			
  	} catch(Exception e) { 
   	}
  }


The problem in your code was not about how you define the id mapping, but about trying to use save on beans which already had an id set (after the first save call).

Also, a little bit off-topic ... since you close your sessions by hand in that finally block, use openSession() instead of getCurrentSession(). The latter was designed for context propagation (when it's not the Book service the one taking care of managing the Session, but some higher level - check on the net about OpenSessionInView pattern). Things work for the moment in your case (since getCurrentSession will always create a new Session) ... but they will start breaking soon in case you change your programming pattern or forget to close a Session.

Eduard Korenschi
 
Forum Index -> General Help
Go to:   
Powered by JForum 2.1.7ice © JForum Team