我刚开始用jsf学习hibernate,这是我自己创建的示例应用程序。我得到的课程没有发现异常(即POJO课程)以下是我的代码,请帮我编码。
HibernateBean是我的POJO类
package com.bectran.employee;
public class HibernateBean {
private int id;
private String userName;
private String passwd;
public HibernateBean() {
// TODO Auto-generated constructor stub
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
连接类
package com.bectran.employee;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class LoginMain {
public LoginMain() {
// TODO Auto-generated constructor stub
}
public static void save1(){
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
registry.applySettings(cfg.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
HibernateBean hb=new HibernateBean();
Transaction tx = session.beginTransaction();
session.save(hb);
System.out.println("Object saved successfully.....!!");
tx.commit();
session.close();
sessionFactory.close();
}
}
hbm file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.bectran.employee.HibernateBean" table="logintable">
<id name="id" column="id">
<generator class="assigned" />
</id>
<property name="userName" type="String" column="username" />
<property name="passwd" type="String" column="pass" />
</class>
</hibernate-mapping>
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/testdb</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/bectran/employee/HibernateBean.hbm.xml" ></mapping>
</session-factory>
</hibernate-configuration>
home.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html">
<h:body>
<h1><marquee>Login Page</marquee></h1>
<h:form>
UserName : <h:inputText value="#{userBean.userName}" />
Password : <h:inputSecret value="#{userBean.passwd}" />
<h:commandButton value="Submit" action="#{loginBean.save1}" />
</h:form>
</h:body>
</html>