JSF JPA完整示例
我正在使用netbeans asn开发一个jsf应用程序,到目前为止,我已经设计了一个输入表单post.xhtml,并从数据库生成了实体类,从实体类生成了jpa控制器。我现在如何调用jsf页面中的方法来将数据保存到表中 jsf页面仍然没有标记JSF JPA完整示例,jpa,jsf-2,Jpa,Jsf 2,我正在使用netbeans asn开发一个jsf应用程序,到目前为止,我已经设计了一个输入表单post.xhtml,并从数据库生成了实体类,从实体类生成了jpa控制器。我现在如何调用jsf页面中的方法来将数据保存到表中 jsf页面仍然没有标记 <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o
<?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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<body>
<ui:composition template="./WEB-INF/template/newTemplate.xhtml" >
<ui:define name="content">
<h:inputText value="#{newPostEntityJpaController.}"></h:inputText>
</ui:define>
</ui:composition>
</body>
</html>
entity class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Entities;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
*
* @author Admin
*/
@Entity
public class NewPostEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// fields
private Number budget;
private String details;
//
public void setDetails(String details) {
this.details = details;
}
public String getDetails() {
return details;
}
public void setBudget(Number budget) {
this.budget = budget;
}
public Number getBudget() {
return budget;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof NewPostEntity)) {
return false;
}
NewPostEntity other = (NewPostEntity) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Entities.NewEntity[ id=" + id + " ]";
}
}
controller
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Controllers;
import Controllers.exceptions.NonexistentEntityException;
import Controllers.exceptions.RollbackFailureException;
import Entities.NewPostEntity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
/**
*
* @author Admin
*/
public class NewPostEntityJpaController implements Serializable {
public NewPostEntityJpaController(UserTransaction utx, EntityManagerFactory emf) {
this.utx = utx;
this.emf = emf;
}
private UserTransaction utx = null;
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(NewPostEntity newPostEntity) throws RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
em.persist(newPostEntity);
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(NewPostEntity newPostEntity) throws NonexistentEntityException, RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
newPostEntity = em.merge(newPostEntity);
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Long id = newPostEntity.getId();
if (findNewPostEntity(id) == null) {
throw new NonexistentEntityException("The newPostEntity with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
NewPostEntity newPostEntity;
try {
newPostEntity = em.getReference(NewPostEntity.class, id);
newPostEntity.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The newPostEntity with id " + id + " no longer exists.", enfe);
}
em.remove(newPostEntity);
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public List<NewPostEntity> findNewPostEntityEntities() {
return findNewPostEntityEntities(true, -1, -1);
}
public List<NewPostEntity> findNewPostEntityEntities(int maxResults, int firstResult) {
return findNewPostEntityEntities(false, maxResults, firstResult);
}
private List<NewPostEntity> findNewPostEntityEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(NewPostEntity.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public NewPostEntity findNewPostEntity(Long id) {
EntityManager em = getEntityManager();
try {
return em.find(NewPostEntity.class, id);
} finally {
em.close();
}
}
public int getNewPostEntityCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<NewPostEntity> rt = cq.from(NewPostEntity.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
实体类
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
一揽子实体;
导入java.io.Serializable;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
/**
*
*@author-Admin
*/
@实体
公共类NewPostEntity实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
//田地
私人数字预算;
私有字符串详细信息;
//
公共无效集合详细信息(字符串详细信息){
this.details=详细信息;
}
公共字符串getDetails(){
退货详情;
}
公共void setBudget(数字预算){
这个.预算=预算;
}
公共编号getBudget(){
回报预算;
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
@凌驾
公共int hashCode(){
int hash=0;
hash+=(id!=null?id.hashCode():0);
返回散列;
}
@凌驾
公共布尔等于(对象){
//TODO:警告-如果未设置id字段,此方法将不起作用
if(!(NewPostEntity的对象实例)){
返回false;
}
NewPostEntity other=(NewPostEntity)对象;
如果((this.id==null&&other.id!=null)| |(this.id!=null&&!this.id.equals(other.id))){
返回false;
}
返回true;
}
@凌驾
公共字符串toString(){
返回“Entities.NewEntity[id=“+id+”]”;
}
}
控制器
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装控制器;
导入Controllers.exceptions.NonexistentEntityException;
导入Controllers.exceptions.RollbackFailureException;
导入实体。NewPostEntity;
导入java.io.Serializable;
导入java.util.List;
导入javax.persistence.EntityManager;
导入javax.persistence.EntityManagerFactory;
导入javax.persistence.Query;
导入javax.persistence.EntityNotFoundException;
导入javax.persistence.criteria.CriteriaQuery;
导入javax.persistence.criteria.Root;
导入javax.transaction.UserTransaction;
/**
*
*@author-Admin
*/
公共类NewPostEntityJPA控制器实现可序列化{
public NewPostEntityJPA控制器(UserTransaction utx、EntityManagerFactory emf){
this.utx=utx;
this.emf=emf;
}
私有用户事务utx=null;
私有EntityManagerFactory emf=null;
公共实体管理器getEntityManager(){
返回emf.createEntityManager();
}
public void create(NewPostEntity NewPostEntity)抛出RollbackFailureException,异常{
EntityManager em=null;
试一试{
utx.begin();
em=getEntityManager();
em.persist(newPostEntity);
提交();
}捕获(例外情况除外){
试一试{
utx.rollback();
}捕获(异常重新){
抛出新的RollbackFailureException(“尝试回滚事务时出错。”,re);
}
掷骰子;
}最后{
如果(em!=null){
em.close();
}
}
}
public void edit(NewPostEntity NewPostEntity)引发不存在的EntityException、RollbackFailureException、Exception{
EntityManager em=null;
试一试{
utx.begin();
em=getEntityManager();
newPostEntity=em.merge(newPostEntity);
提交();
}捕获(例外情况除外){
试一试{
utx.rollback();
}捕获(异常重新){
抛出新的RollbackFailureException(“尝试回滚事务时出错。”,re);
}
字符串msg=ex.getLocalizedMessage();
如果(msg==null | | msg.length()==0){
Long id=newPostEntity.getId();
if(findNewPostEntity(id)==null){
抛出新的NoneExistentEntityException(“id为“+id+”的newPostEntity不再存在”);
}
}
掷骰子;
}最后{
如果(em!=null){
em.close();
}
}
}
public void destroy(长id)抛出不存在的EntityException、RollbackFailureException、Exception{
EntityManager em=null;
试一试{
utx.begin();
em=getEntityManager();
新邮政实体新邮政实体;
试一试{
newPostEntity=em.getReference(newPostEntity.class,id);
newPostEntity.getId();
}捕获(EntityNotFoundException enfe){
抛出新的NoneExistentEntityException(“id为“+id+”的newPostEntity不再存在。”,enfe);
}
em.remove(newPostEntity);
提交();
}捕获(例外情况除外){
试一试{
utx.rollback();
}捕获(异常重新){
抛出新的RollbackFailureException(“尝试回滚事务时出错。”,re);
}
掷骰子;
}最后{
如果(em!=null){
em.close();
}
}
}
公共列表findNewPostEntityEntities(){
返回findNewPostEntityEntities(true,-1,-1);
}
公共Lis
public void savePost(){
if (newPostEntity != null)
create(newPostEntity);
}
<h:form>
<h:inputText value="#{newPostEntityJpaController.newPostEntity.details}"></h:inputText>
<h:commandButton action="#{newPostEntityJpaController.saveMessage}">
</h:form>