Ich beschäftige mich gerad zum erstnmal mit JSF. Als erstes kleines Projekt will ich mir ein Login erstellen. Hierzu habe ich mir mit NetBeans 7.4 mit Glassfisch 4.0 folgendes Projekt erstellt.
WebPages/index.xhtml
src/com/jsflogin/entity/Customer.hbm.xml
[XML]
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 5, 2013 10:57:32 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.javaknowledge.entity.Customer" table="customer" catalog="jsfhibercrud">
<id name="custId" type="java.lang.Integer">
<column name="cust_id" />
<generator class="identity" />
</id>
<property name="firstName" type="string">
<column name="first_name" length="45" not-null="true" />
</property>
<property name="lastName" type="string">
<column name="last_name" length="45" not-null="true" />
</property>
<property name="email" type="string">
<column name="email" length="45" not-null="true" />
</property>
<property name="dob" type="date">
<column name="dob" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>
[/XML]
lt. Netbeans ist die Verbindung zur Datenbank für Hibernate korrekt.
Beim starten erhalte ich aber
und ich weis damit noch nichts anzufangen. Kann mir bitte einer hier weiterhelfen?
WebPages/index.xhtml
HTML:
<?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"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>JSF Hibernate CRUD Example</title>
<style>
tfoot{
text-align: center;
}
thead{
background-color: magenta;
color: white;
}
td{
font-weight: bold;
}
.msg{
font-size: small;
color: blue;
}
</style>
</h:head>
<h:body>
<h:form>
<h:panelGrid id="panel1" columns="2" border="1"
cellpadding="5" cellspacing="1">
<f:facet name="header">
<h:outputText value="Add Customer Information"/>
</f:facet>
<h:outputLabel value="First Namer:"/>
<h:inputText value="#{customer.firstName}" id="fn"/>
<h:outputLabel value="Last Name:"/>
<h:inputText value="#{customer.lastName}" id="ln"/>
<h:outputLabel value="Email:"/>
<h:inputText value="#{customer.email}" id="eml"/>
<h:outputLabel value="Date of Birth:"/>
<h:inputText value="#{customer.sd}" id="s"/>
<f:facet name="footer">
<h:outputLabel value="#{customer.msg}" id="msg" styleClass="msg"/>
<h:commandButton value="Save" action="#{customer.saveCustomer}">
<f:ajax render="fn ln eml s msg" execute="@form"/>
</h:commandButton>
</f:facet>
</h:panelGrid>
</h:form>
<h:form>
<h:panelGrid id="panel2" columns="2" border="1"
cellpadding="5" cellspacing="1">
<f:facet name="header">
<h:outputText value="Update/Delete Customer Info"/>
</f:facet>
<h:outputLabel value="Select Customer:"/>
<h:selectOneMenu value="#{customer.selectedname}" id="ulist">
<f:selectItems value="#{customer.allCustomers}"/>
<f:ajax event="change" render="cid fname lname email sd" listener="#{customer.fullInfo}"/>
</h:selectOneMenu>
<h:outputLabel value="Customer ID:"/>
<h:inputText value="#{customer.custId}" id="cid" readonly="true"/>
<h:outputLabel value="First Name:"/>
<h:inputText value="#{customer.firstName}" id="fname"/>
<h:outputLabel value="Last Name:"/>
<h:inputText value="#{customer.lastName}" id="lname"/>
<h:outputLabel value="Email:"/>
<h:inputText value="#{customer.email}" id="email"/>
<h:outputLabel value="Date of Birth:"/>
<h:inputText value="#{customer.sd}" id="sd"/>
<f:facet name="footer">
<h:outputLabel value="#{customer.msg}" id="msg2" styleClass="msg"/>
<h:commandButton value="Update Info" action="#{customer.updateCustomer}">
<f:ajax render="ulist cid fname lname email sd msg2" execute="@form"/>
</h:commandButton>
<h:commandButton value="Delete Info" action="#{customer.deleteCustomer}">
<f:ajax render="ulist cid fname lname email sd msg2" execute="@form"/>
</h:commandButton>
</f:facet>
</h:panelGrid>
</h:form>
</h:body>
</html>
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.jsflogin.dao;
import com.jsflogin.entity.Customer;
import com.jsflogin.utils.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
*
* @author javaknowledge
*/
public class CustomerDao {
public void addCustomer(Customer cust) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
session.save(cust);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
public void deleteCustomer(int custid) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
Customer cust = (Customer) session.load(Customer.class, new Integer(custid));
session.delete(cust);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
public void updateCustomer(Customer cust) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
session.update(cust);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
public List<Customer> getAllCustomers() {
List<Customer> users = new ArrayList<Customer>();
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
users = session.createQuery("select concat(first_name, ' ', last_name) as name from Customer").list();
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
session.flush();
session.close();
}
return users;
}
public List<Customer> getCustomerById(String custid) {
System.out.println(custid);
// Customer cust = null;
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
String queryString = "from Customer where concat(first_name, ' ', last_name) = :id";
Query query = session.createQuery(queryString);
query.setString("id", custid);
//cust = (Customer) query.uniqueResult();
List<Customer> list = query.list();
if (list.size() > 0) {
return list;
}
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
session.flush();
session.close();
}
return null;
}
}
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.jsflogin.dao;
import com.jsflogin.entity.Customer;
import com.jsflogin.utils.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
*
* @author javaknowledge
*/
public class CustomerDao {
public void addCustomer(Customer cust) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
session.save(cust);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
public void deleteCustomer(int custid) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
Customer cust = (Customer) session.load(Customer.class, new Integer(custid));
session.delete(cust);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
public void updateCustomer(Customer cust) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
session.update(cust);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
public List<Customer> getAllCustomers() {
List<Customer> users = new ArrayList<Customer>();
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
users = session.createQuery("select concat(first_name, ' ', last_name) as name from Customer").list();
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
session.flush();
session.close();
}
return users;
}
public List<Customer> getCustomerById(String custid) {
System.out.println(custid);
// Customer cust = null;
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
String queryString = "from Customer where concat(first_name, ' ', last_name) = :id";
Query query = session.createQuery(queryString);
query.setString("id", custid);
//cust = (Customer) query.uniqueResult();
List<Customer> list = query.list();
if (list.size() > 0) {
return list;
}
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
session.flush();
session.close();
}
return null;
}
}
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.jsflogin.utils;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory=buildSessionFactory();
public static SessionFactory buildSessionFactory(){
try {
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
src/com/jsflogin/entity/Customer.hbm.xml
[XML]
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 5, 2013 10:57:32 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.javaknowledge.entity.Customer" table="customer" catalog="jsfhibercrud">
<id name="custId" type="java.lang.Integer">
<column name="cust_id" />
<generator class="identity" />
</id>
<property name="firstName" type="string">
<column name="first_name" length="45" not-null="true" />
</property>
<property name="lastName" type="string">
<column name="last_name" length="45" not-null="true" />
</property>
<property name="email" type="string">
<column name="email" length="45" not-null="true" />
</property>
<property name="dob" type="date">
<column name="dob" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>
[/XML]
lt. Netbeans ist die Verbindung zur Datenbank für Hibernate korrekt.
Beim starten erhalte ich aber
HTML:
java.lang.ExceptionInInitializerError
und ich weis damit noch nichts anzufangen. Kann mir bitte einer hier weiterhelfen?