tenho o seguinte exemplo em jsf2 e nao consigo adicionar bugs em projects, seguinte erro:
entidades:
package entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Bug {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", nullable=false, columnDefinition="integer")
private Long id;
private String description;
private String severity;
@ManyToOne
private Project project;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSeverity() {
return severity;
}
public void setSeverity(String severity) {
this.severity = severity;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
@Override
public int hashCode() {
return this.description.length() * 23;
}
@Override
public String toString() {
return description;
}
@Override
public boolean equals(Object obj) {
if ((obj instanceof Bug)
&& (((Bug) obj).getDescription().equals(this.description))) {
return true;
} else {
return false;
}
}
}
package entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Project {
@Id
@GeneratedValue
@Column(name="id", nullable=false, columnDefinition="integer")
private Long id;
private String name;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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;
}
@Override
public int hashCode() {
return this.name.length() * 23;
}
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object obj) {
if ((obj instanceof Project)
&& (((Project) obj).getName().equals(this.name))) {
return true;
} else {
return false;
}
}
}
MBeans
package managedbeans;
import java.io.Serializable;
import java.util.List;
import javax.enterprise.inject.Model;
import javax.faces.bean.RequestScoped;
import javax.inject.Inject;
import sessionbeans.BugRepository;
import sessionbeans.ProjectRepository;
import entities.Bug;
import entities.Project;
@Model
@RequestScoped
public class BugMB implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1339958063986622041L;
@Inject
private BugRepository bugRepository;
@Inject
private ProjectRepository projectRepository;
private Bug bug = new Bug();
private Long projectId;
private List<Bug> bugs;
public void save() {
Project project = this.projectRepository.findById(this.projectId);
this.bug.setProject(project);
if (this.getBug().getId() == null) {
this.bugRepository.add(this.getBug());
} else {
this.bugRepository.edit(this.getBug());
}
this.bug = new Bug();
this.bugs = null;
}
public void delete(Long id) {
this.bugRepository.removeById(id);
this.bug = null;
}
public void prepareEdit(Long id) {
this.bug = this.bugRepository.findById(id);
}
public Bug getBug() {
return bug;
}
public List<Bug> getBugs() {
if (this.bugs == null) {
this.bugs = this.bugRepository.findAll();
}
return bugs;
}
public void setProjectId(Long projectId) {
this.projectId = projectId;
}
public Long getProjectId() {
return projectId;
}
public BugRepository getBugRepository() {
return bugRepository;
}
public void setBugRepository(BugRepository bugRepository) {
this.bugRepository = bugRepository;
}
public ProjectRepository getProjectRepository() {
return projectRepository;
}
public void setProjectRepository(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}
public void setBug(Bug bug) {
this.bug = bug;
}
public void setBugs(List<Bug> bugs) {
this.bugs = bugs;
}
}
[code]package managedbeans;
import java.io.Serializable;
import java.util.List;
import javax.enterprise.inject.Model;
import javax.faces.bean.RequestScoped;
import javax.inject.Inject;
import sessionbeans.ProjectRepository;
import entities.Project;
@SuppressWarnings(“serial”)
@Model
@RequestScoped
public class ProjectMB implements Serializable {
@Inject
private ProjectRepository projectRepository;
private Project project = new Project();
private List<Project> projects;
public void save() {
if (this.getProject().getId() == null) {
this.projectRepository.add(this.getProject());
} else {
this.projectRepository.edit(this.getProject());
}
this.project = new Project();
this.projects = null;
}
public void delete(Long id) {
this.projectRepository.removeById(id);
this.projects = null;
}
public void prepareEdit(Long id) {
this.project = this.projectRepository.findById(id);
}
public ProjectRepository getProjectRepository() {
return projectRepository;
}
public void setProjectRepository(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
public List<Project> getProjects() {
if (this.projects == null) {
this.projects = this.projectRepository.findAll();
}
// List<Project> projects = new ArrayList<Project>();
return projects;
}
public void setProjects(List<Project> projects) {
this.projects = projects;
}
}
[/code]
paginas
bugs.xhtml
[code]
<h:head>
Bugs
</h:head>
<h:body>
<ui:include src="/menu.xhtml" />
<hr />
<h1>New Bug</h1>
<h:form>
<h:panelGrid>
<h:inputHidden value="#{bugMB.bug.id}" />
<h:outputLabel value="Severity:" />
<h:selectOneMenu value="#{bugMB.bug.severity}">
<f:selectItem itemValue="LOW" />
<f:selectItem itemValue="MEDIUM" />
<f:selectItem itemValue="HIGH" />
</h:selectOneMenu>
<h:outputLabel for="description" value="Description:" />
<h:inputTextarea id="description" value="#{bugMB.bug.description}" />
<h:outputLabel for="selecaoProjeto" value="Project:" />
<h:selectOneMenu id="selecaoProjeto" value="#{bugMB.projectRepository}">
<f:selectItems value="#{projectMB.projects}" var="project"
itemLabel="#{project.description}" itemValue="#{project}"/>
</h:selectOneMenu>
<h:commandButton action="#{bugMB.save}" value="Save" />
</h:panelGrid>
</h:form>
<hr />
<h1>Bug List</h1>
<h:dataTable value="#{bugMB.bugs}" var="bug"
rendered="#{not empty bugMB.bugs}" border="1">
<h:column>
<f:facet name="header">Id</f:facet>
#{bug.id}
</h:column>
<h:column>
<f:facet name="header">Project</f:facet>
#{bug.project.name}
</h:column>
<h:column>
<f:facet name="header">Severity</f:facet>
#{bug.severity}
</h:column>
<h:column>
<f:facet name="header">Description</f:facet>
#{bug.description}
</h:column>
<h:column>
<f:facet name="header">Delete</f:facet>
<h:form>
<h:commandLink action="#{bugMB.delete(bug.id)}">delete</h:commandLink>
</h:form>
</h:column>
<h:column>
<f:facet name="header">Edit</f:facet>
<h:form>
<h:commandLink action="#{bugMB.prepareEdit(bug.id)}">edit</h:commandLink>
</h:form>
</h:column>
</h:dataTable>
</h:body>
[/code]projects.xhtml
<!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://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Projects</title>
</h:head>
<h:body>
<ui:include src="/menu.xhtml" />
<hr />
<h1>New Project</h1>
<h:form id="form">
<h:panelGrid id="grid">
<h:inputHidden value="#{projectMB.project.id}" />
<h:outputLabel for="input1" value="Name:" />
<h:inputText id="input1" value="#{projectMB.project.name}" />
<h:outputLabel for="input2" value="Description:" />
<h:inputTextarea id="input2" value="#{projectMB.project.description}" />
<h:commandButton action="#{projectMB.save}" value="Save" />
</h:panelGrid>
</h:form>
<hr />
<h1>Project List</h1>
<h:dataTable id="dataTable" value="#{projectMB.projects}" var="project"
rendered="#{not empty projectMB.projects}" border="1">
<h:column>
<f:facet name="header">Id</f:facet>
#{project.id}
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>
#{project.name}
</h:column>
<h:column>
<f:facet name="header">Description</f:facet>
#{project.description}
</h:column>
<h:column>
<f:facet name="header">Delete</f:facet>
<h:form>
<h:commandLink action="#{projectMB.delete(project.id)}">delete</h:commandLink>
</h:form>
</h:column>
<h:column>
<f:facet name="header">Edit</f:facet>
<h:form>
<h:commandLink action="#{projectMB.prepareEdit(project.id)}">edit</h:commandLink>
</h:form>
</h:column>
</h:dataTable>
</h:body>
</html>
Alguem pode ajudar?