Witam
Czyli wyszłoby coś takiego:
package model;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
*
* @author
*/
//@ManagedBean
//@ApplicationScoped
@Entity
public class Test implements Serializable {
@Id
private int id;
private String test;
public Test() {
}
public Test(String test) {
this.test = test;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
package model;
/**
*
* @author
*/
import model.Test;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import utils.HibernateUtil;
@ManagedBean
@ApplicationScoped
public class TestControler {
Test test=null;
private String tescik;
public TestControler() {
}
public void DodajTest(String tescik){
HibernateUtil.getSessionFactory();
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
try{
Session session = sessionFactory.openSession();
Test t = new Test(tescik);
Transaction tx = session.beginTransaction();
session.save(t);
tx.commit();
}catch(Exception e){
e.printStackTrace();
}
}
public String getTescik() {
return tescik;
}
public void setTescik(String tescik) {
this.tescik = tescik;
}
}
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
PostgreSql Test
<h:form>
<h:panelGrid>
String Test:
<h:inputText id="test" value="#{testControler.tescik}"/>
</h:panelGrid>
<h:commandButton value="Dodaj" action="#{testControler.DodajTest(testControler.tescik)}"/>
</h:form>
</h:body>
</html>
Czy takie odwołanie jest ok?
Pozdrawiam