Koledzy zerknijcie na ten kod. Co jest nie tak, że nie mogę po kliknieciu przycisku dodac obiektu do bazy danych.
@Entity
public class House implements Serializable
{
@Id
@GeneratedValue
private long id;
private String name;
public House() {
super();
}
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;
}
}
@ManagedBean
@RequestScoped
public class HouseaBean
{
private House house;
public HouseBean() {
super();
}
private House house;
public House getHouse() {
return house;
}
public void setHouse(House house) {
this.house = house;
}
public void addHouse()
{
house = new House();
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("HealthHelper");
EntityManager entityManager = entityManagerFactory
.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(house);
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
}
}
<!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"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="/tampl.xhtml">
<ui:define name="content">
<h:form>
<h:panelGrid id="panel">
<p:outputLabel id="labelName" value="name">
</p:outputLabel>
<p:inputText id="TextName" value="#{HouseBean.house.name}"></p:inputText>
<p:commandButton action="#{HouseBean.addHouse()}"></p:commandButton>
</h:panelGrid>
</h:form>
</ui:define>
</ui:composition>
</html>