使用支持 bean 和 jsf 的 Java EE 持久化实体 [重复]

Posted

技术标签:

【中文标题】使用支持 bean 和 jsf 的 Java EE 持久化实体 [重复]【英文标题】:Java EE Persisting Entitys using backing beans and jsf [duplicate] 【发布时间】:2016-10-15 21:21:47 【问题描述】:

我创建了一个实体类来保存用户的数据。 JSF 的支持 bean 包含创建和存储实体实例的 persist 方法。我从 JSF 页面调用此方法。我不想让这篇文章变得冗长和混乱,所以我只会发布这两个类和 jsf 页面。是时候进入 jsf 页面时,我收到一条错误消息: javax.persistence.TransactionRequiredException 我尝试以几种方式更改代码,但仍然无法正常工作。这里有什么问题?

import javax.ejb.TransactionAttribute;
import static javax.ejb.TransactionAttributeType.REQUIRES_NEW;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.SystemException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;

/**
 *
 * @author David Jennings
 */
@ManagedBean(name = "formbean")
@RequestScoped
public class DataBean 

    /**
     * Creates a new instance of DataBean
     */
    public DataBean() 

    

    DataBean(String firstName,
            String middleName,
            String lastName,
            String birthDay,
            String birthMonth,
            String birthYear,
            String unit,
            String street,
            String city,
            String state,
            String zipCode) 

        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
        this.birthDay = birthDay;
        this.birthMonth = birthMonth;
        this.birthYear = birthYear;
        this.unit = unit;
        this.street = street;
        this.city = city;
        this.state = state;
        this.zipCode = zipCode;
    
    @PersistenceContext
    EntityManager em ;



    private String result;
    private String firstName;
    private String middleName;
    private String lastName;
    private String birthDay;
    private String birthMonth;
    private String birthYear;
    private String unit;
    private String street;
    private String city;
    private String state;
    private String zipCode;

    @TransactionAttribute(REQUIRES_NEW)
    public void persist() throws SystemException 

        em.persist(new DataBean(this.firstName, this.middleName, this.lastName, this.birthDay, this.birthMonth, this.birthYear, this.unit, this.street, this.city, this.state, this.zipCode));



    

    /**
     * @return the firstName
     */
    public String getFirstName() 

        return firstName;
    

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) 
        this.firstName = firstName;
    

    /**
     * @return the middleName
     */
    public String getMiddleName() 
        return middleName;
    

    /**
     * @param middleName the middleName to set
     */
    public void setMiddleName(String middleName) 
        this.middleName = middleName;
    

    /**
     * @return the lastName
     */
    public String getLastName() 
        return lastName;
    

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) 
        this.lastName = lastName;
    

    /**
     * @return the birthDay
     */
    public String getBirthDay() 
        return birthDay;
    

    /**
     * @param birthDay the birthDay to set
     */
    public void setBirthDay(String birthDay) 
        this.birthDay = birthDay;
    

    /**
     * @return the birthMonth
     */
    public String getBirthMonth() 
        return birthMonth;
    

    /**
     * @param birthMonth the birthMonth to set
     */
    public void setBirthMonth(String birthMonth) 
        this.birthMonth = birthMonth;
    

    /**
     * @return the birthYear
     */
    public String getBirthYear() 
        return birthYear;
    

    /**
     * @param birthYear the birthYear to set
     */
    public void setBirthYear(String birthYear) 
        this.birthYear = birthYear;
    

    /**
     * @return the unit
     */
    public String getUnit() 
        return unit;
    

    /**
     * @param unit the unit to set
     */
    public void setUnit(String unit) 
        this.unit = unit;
    

    /**
     * @return the street
     */
    public String getStreet() 
        return street;
    

    /**
     * @param street the street to set
     */
    public void setStreet(String street) 
        this.street = street;
    

    /**
     * @return the city
     */
    public String getCity() 
        return city;
    

    /**
     * @param city the city to set
     */
    public void setCity(String city) 
        this.city = city;
    

    /**
     * @return the state
     */
    public String getState() 
        return state;
    

    /**
     * @param state the state to set
     */
    public void setState(String state) 
        this.state = state;
    

    /**
     * @return the zipCode
     */
    public String getZipCode() 
        return zipCode;
    

    /**
     * @param zipCode the zipCode to set
     */
    public void setZipCode(String zipCode) 
        this.zipCode = zipCode;
    

    /**
     * @return the result
     */
    public String getResult() 
        return result;
    

    /**
     */
    public void setResult() 

        WebTarget webTarget;
        Client client;
        String BASE_URI = "http://localhost:8080/DataForm/webresources/";
        client = javax.ws.rs.client.ClientBuilder.newClient();
        webTarget = client.target(BASE_URI).path("generic");
        WebTarget resource = webTarget;
        result = resource.request(javax.ws.rs.core.MediaType.TEXT_html).get(String.class);

    


实体类:

@Entity
public class DataEntity implements Serializable 
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    private String firstName;
    private String middleName;
    private String lastName;
    private String birthDay;
    private String birthMonth;
    private String birthYear;
    private String unit;
    private String street;
    private String city;
    private String states;
    private String zipCode;


    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 DataEntity)) 
            return false;
        
        DataEntity other = (DataEntity) 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 "entity.DataEntity[ id=" + id + " ]";
    

    /**
     * @return the firstName
     */
    public String getFirstName() 
        return firstName;
    

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) 
        this.firstName = firstName;
    

    /**
     * @return the middleName
     */
    public String getMiddleName() 
        return middleName;
    

    /**
     * @param middleName the middleName to set
     */
    public void setMiddleName(String middleName) 
        this.middleName = middleName;
    

    /**
     * @return the lastName
     */
    public String getLastName() 
        return lastName;
    

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) 
        this.lastName = lastName;
    

    /**
     * @return the birthDay
     */
    public String getBirthDay() 
        return birthDay;
    

    /**
     * @param birthDay the birthDay to set
     */
    public void setBirthDay(String birthDay) 
        this.birthDay = birthDay;
    

    /**
     * @return the birthMonth
     */
    public String getBirthMonth() 
        return birthMonth;
    

    /**
     * @param birthMonth the birthMonth to set
     */
    public void setBirthMonth(String birthMonth) 
        this.birthMonth = birthMonth;
    

    /**
     * @return the birthYear
     */
    public String getBirthYear() 
        return birthYear;
    

    /**
     * @param birthYear the birthYear to set
     */
    public void setBirthYear(String birthYear) 
        this.birthYear = birthYear;
    

    /**
     * @return the unit
     */
    public String getUnit() 
        return unit;
    

    /**
     * @param unit the unit to set
     */
    public void setUnit(String unit) 
        this.unit = unit;
    

    /**
     * @return the street
     */
    public String getStreet() 
        return street;
    

    /**
     * @param street the street to set
     */
    public void setStreet(String street) 
        this.street = street;
    

    /**
     * @return the city
     */
    public String getCity() 
        return city;
    

    /**
     * @param city the city to set
     */
    public void setCity(String city) 
        this.city = city;
    

    /**
     * @return the states
     */
    public String getStates() 
        return states;
    

    /**
     * @param states the states to set
     */
    public void setStates(String states) 
        this.states = states;
    

    /**
     * @return the zipCode
     */
    public String getZipCode() 
        return zipCode;
    

    /**
     * @param zipCode the zipCode to set
     */
    public void setZipCode(String zipCode) 
        this.zipCode = zipCode;
    


JSF:

<?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://xmlns.jcp.org/jsf/html">
    <h:head>
       #formbean.persist()
    </h:head>
    <h:body>
        <img src="city-logo.jpg"   />
        <br/>
        #formbean.setResult()
        <h:outputText value="#formbean.result" escape="false"/>
    </h:body>
</html>

【问题讨论】:

【参考方案1】:

这篇文章将回答您的问题: JPA and JSF: right way of injecting EntityManager

长话短说:您尝试在 EJB 之外使用需要容器管理事务的 EntityManager。

【讨论】:

您的意思是我需要为容器管理的事务使用 EJB。我不能在支持 bean 或 jsf 中做到这一点。你给我的那个帖子似乎澄清了这个问题。不过我还没试过。

以上是关于使用支持 bean 和 jsf 的 Java EE 持久化实体 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

JSF。在每个页面加载时调用支持 bean 方法[重复]

Java EE开发技术课程第六周(jsffacelets)

从支持 Bean 刷新 JSF/PrimeFaces 接口

JSF 不更新某些支持 bean 属性

Spring 安全性和 JSF:在登录时调用支持 bean 的方法?

使用MyEclipse开发Java EE应用:用XDoclet创建EJB 2 Session Bean项目