2016-06-14 22 views
0

ユーザのデータを保持するエンティティクラスを作成しました。 JSFへのバッキングBeanには、エンティティのインスタンスを作成して格納するためのpersistメソッドが含まれています。 JSFページからこのメソッドを呼び出します。私はこの投稿を長くして混乱させたくないので、私はこれら2つのクラスとjsfページだけを投稿します。 jsfページに行くときは、次のようなエラーが出ます:javax.persistence.TransactionRequiredException私はいくつかの方法でコードを変更しようとしましたが、それでも動作しません。何がここに間違っていますか?Java EEバッキングBeanとjsfを使用した永続エンティティ

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" width="200" height="100" alt="city-logo"/> 
     <br/> 
     #{formbean.setResult()} 
     <h:outputText value="#{formbean.result}" escape="false"/> 
    </h:body> 
</html> 

答えて

1

JPA and JSF: right way of injecting EntityManager

かいつまん:あなたはEntityManagerを使用しようと、 EJBの外部にあるコンテナ管理のトランザクションが必要です。

+0

コンテナ管理トランザクションのためにEJBを使用する必要があることを意味します。私は裏打ち豆やjsfでそれをすることはできません。あなたが私に与えたその投稿は、その問題を明確にするようです。私はまだそれを試していない。 – jillian

関連する問題