Java EE 6 exception persistance
Bonjour, je m'essaye à Java EE 6 et j'ai l'erreur suivante:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| exception
javax.servlet.ServletException: javax.persistence.TransactionRequiredException: Transaction is required to perform this operation (either use a transaction or extended persistence context)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67)
cause mère
javax.faces.el.EvaluationException: javax.persistence.TransactionRequiredException: Transaction is required to perform this operation (either use a transaction or extended persistence context)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67) |
j'ai cette erreur lorsque j'essaye d'enregistrer en base un widget créer, voici mon code:
Widget.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| package knowledge.tuto1;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Widget implements Serializable
{
private Long id;
private String partNumber;
private String name;
private String description;
@Id
@GeneratedValue
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
// demonstrates a column name override
@Column(name = "partno")
public String getPartNumber()
{
return partNumber;
}
public void setPartNumber(String partNumber)
{
this.partNumber = partNumber;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
/** Default value included to remove warning. Remove or modify at will. */
private static final long serialVersionUID = 1L;
} |
Mon controleur.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
package knowledge.tuto1;
import javax.annotation.PostConstruct;
import javax.ejb.TransactionManagement;
import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
public @Model class Controleur {
private Widget widget;
private final String text = "";
private String letters;
private String numbers;
private String email;
@Inject
@WidgetRepository
EntityManager widgetRepository;
public Controleur() {
}
@PostConstruct
public void initialize() {
widget = new Widget();
System.out
.println(this.getClass().getSimpleName() + " was constructed");
}
public String getText() {
return text;
}
public Widget getWidget() {
return widget;
}
public void setWidget() {
widgetRepository.persist(widget);
}
} |
et mon fichier xhtml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
template="/WEB-INF/templates/default.xhtml">
<ui:define name="content">
<h1>Hello World!</h1>
<p>Your CDI bean <code>HelloWorld</code> says <span style="color: blue;">#{controleur.text}</span> using the Unified EL.</p>
<h:form id="bv">
<h2>Bean Validation examples</h2>
<p>Enforces annotation-based constraints defined on the model class.</p>
<table>
<tr>
<th style="text-align: right;">
<h:outputLabel for="part" value="Partner:"/>
</th>
<td>
<h:inputText id="part" value="#{controleur.widget.partNumber}"/>
<h:message for="part" errorClass="invalid"/>
</td>
</tr>
<tr>
<th style="text-align: right;">
<h:outputLabel for="name" value="Name:"/>
</th>
<td>
<h:inputText id="name" value="#{controleur.widget.name}"/>
<h:message for="name" errorClass="invalid"/>
</td>
</tr>
<tr>
<th style="text-align: right;">
<h:outputLabel for="desc" value="Description:"/>
</th>
<td>
<h:inputText id="desc" value="#{controleur.widget.description}"/>
<h:message for="desc" errorClass="invalid"/>
</td>
</tr>
</table>
<p>
<h:commandButton id="check" action="#{controleur.setWidget()}" value="Check values"/>
<h:outputText value=" All clear!" rendered="#{facesContext.postback and empty facesContext.messageList}" style="color: green;"/>
</p>
</h:form>
<h2>Widgets</h2>
<h:dataTable var="_widget" value="#{widgets}">
<h:column>
<f:facet name="header">Id</f:facet>
#{_widget.id}
</h:column>
<h:column>
<f:facet name="header">Part Number</f:facet>
#{_widget.partNumber}
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
#{_widget.name}
</h:column>
<h:column>
<f:facet name="header">Description</f:facet>
#{_widget.description}
</h:column>
</h:dataTable>
</ui:define>
</ui:composition> |
erreur lors de l'appel à la methode setWidget()
merci beaucoup.