Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding jsf form fields in data base using hibernate

 
nejla kach
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good afternon, i have two fields fist Name and Last Name in ajout.xhtml when i press the button Save ,it is supposed to add first name and last name in table "Person" but it doesn't add data and it doesn't show nothing ,please i need your help.


//ajout.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"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
<link href="style.css" rel="stylesheet" type="text.css"/>
</h:head>
<h:body>
<p>
<hutputText value="Last Name" />
<h:inputText value="#{PersonBean.per.lastName}" />
</p>
<p>
<hutputText value="First Name" />
<h:inputText value="#{PersonBean.per.firstName}" />
</p>
<h:commandButton value="Save" action="#{PersonBean.SavePerson}" />
</h:body>
</html>

//PersonBean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import com.util.Person;
import java.util.List;
@ManagedBean(name="PersonBean")
@SessionScoped
public class PersonBean {

PersonDao perdao;
Person per;
List<Person> perList;


public PersonBean() {
perdao = new PersonDao();
}

public Person getPer()
{return per;}
public void setPer(Person per)
{this.per=per;}

public String SavePerson(){
perdao.savepersonne(per);
return "rose";
}

}
//PersonDao:

import org.hibernate.Session;
import com.util.HibernateUtil;

public class PersonDao {

Session session = null;

public PersonDao(){
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
}

public void savepersonne(com.util.Person per) {

org.hibernate.Transaction tx =session.beginTransaction();

per.setId(per.getId());
per.setFirstName(per.getFirstName());
per.setLastName(per.getLastName());
session.save(per);
tx.commit();
session.close();
}

}
//Person :

package com.util;
public class Person implements java.io.Serializable {

private static final long serialVersionUID = 1L;
private Integer id;
private String lastName;
private String firstName;

public Person() {
}

public Person(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
}

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getLastName() {
return this.lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getFirstName() {
return this.firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

}

//hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>

<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

<property name="hibernate.show_sql">true</property>

<mapping resource="com/util/Person.hbm.xml" />


</session-factory>
</hibernate-configuration>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic