java ee - When adding book values to @Entity values turn out null JavaEE -
i have done stupid , have tried , tried 4 hours , can't figure out wrong. when try add book database, mysql increments values set in mysqltable columns "null".
im running eclipse, , glassfish 3.1.1, maven 2.1 , mysql.
i have mixed something, or annotations wrong or stupid. please help!
books entity
package se.andolf.entities; import java.io.serializable; import javax.faces.bean.requestscoped; import javax.faces.bean.sessionscoped; import javax.inject.named; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.table; @entity @sessionscoped @named @table(name = "books") public class books implements serializable { private static final long serialversionuid = 1l; @id //private key @column (name = "book_id") private long bookid; @column (name = "title") private string title; @column (name = "author") private string author; @column (name = "price") private string price; public long getbookid() { return bookid; } public void setbookid(long bookid) { this.bookid = bookid; } public string getauthor() { return author; } public void setauthor(string author) { system.out.println(author); this.author = author; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getprice() { return price; } public void setprice(string price) { this.price = price; } }
bookcontroller
package se.andolf.controller; import java.util.list; import javax.annotation.resource; import javax.faces.bean.sessionscoped; import javax.inject.inject; import javax.inject.named; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import javax.persistence.query; import javax.transaction.heuristicmixedexception; import javax.transaction.heuristicrollbackexception; import javax.transaction.notsupportedexception; import javax.transaction.rollbackexception; import javax.transaction.systemexception; import javax.transaction.usertransaction; import se.andolf.entities.books; @named @sessionscoped public class bookcontroller { @persistencecontext() private entitymanager entitymanager; @resource usertransaction usertransaction; @inject private books book; public string savebook() { try { system.out.println(book.getauthor() + " hej"); usertransaction.begin(); book.setauthor(book.getauthor()); book.settitle(book.gettitle()); book.setprice(book.getprice()); entitymanager.persist(book); usertransaction.commit(); } catch (notsupportedexception | systemexception | securityexception | illegalstateexception | rollbackexception | heuristicmixedexception | heuristicrollbackexception e) { e.printstacktrace(); } return "index"; } public list<books> getallbooks(){ query query = entitymanager.createquery("select s books s"); list<books>allbooks = query.getresultlist(); return allbooks; } }
index.xhtml
<?xml version='1.0' encoding='utf-8' ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:head> <title>boklista</title> </h:head> <h:body> <h:messages></h:messages> <ui:repeat var="i" value="#{bookcontroller.getallbooks()}"> <h:panelgrid columns="4"> <h:outputlabel value="#{i.bookid}" /> <h:outputlabel value="#{i.title}"/> <h:outputlabel value="#{i.author}"/> <h:outputlabel value="#{i.price}"/> </h:panelgrid> </ui:repeat> <h:form> <h:panelgrid columns="2"> <h:outputlabel for="title" value="title:"/> <h:inputtext id="title" value="#{books.title}" required="true"/> <h:outputlabel for="author" value="author:"/> <h:inputtext id="author" value="#{books.author}" required="true"/> <h:outputlabel for="price" value="price: "/> <h:inputtext id="price" value="#{books.price}" required="true"/> <h:commandbutton value="submit" action="#{bookcontroller.savebook()}"/> </h:panelgrid> </h:form> </h:body> </html>
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>facelettest</display-name> <welcome-file-list> <welcome-file>index.xhtml</welcome-file> </welcome-file-list> <servlet> <servlet-name>faces servlet</servlet-name> <servlet-class>javax.faces.webapp.facesservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>faces servlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <context-param> <description>state saving method: 'client' or 'server' (=default). see jsf specification 2.5.2</description> <param-name>javax.faces.project_stage</param-name> <param-value>development</param-value> </context-param> <context-param> <param-name>javax.servlet.jsp.jstl.fmt.localizationcontext</param-name> <param-value>resources.application</param-value> </context-param> <listener> <listener-class>com.sun.faces.config.configurelistener</listener-class> </listener> </web-app>
you can not let cdi manage jpa entities , still expect them persist correctly. if you're going way, should use producers.
Comments
Post a Comment