Hallo Leute,
Ich will JBOSS SEAM, JPA, Hibernate verwenden.
Ich habe Book Entitiy erstellt und versucht in DB zu speichern. Leider gibt dieser Fehlermeldung aus.
Exception in thread "main" java.lang.NullPointerException
at org.domain.wonderbib.main.BookMain.main(BookMain.java:25)
Ich verstehe nicht wo liegt das Problem ?
ich schicke all die Datein, villeicht können Sie mir sagen, wo ist mein Problem?
hibernate-console-properties:
#File used by hibernate tools to override <datasource> and other container specific settings in persistence.xml
hibernate.connection.password=root
hibernate.connection.username=
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.connection.url=jdbc:mysql://localhost:3306/wonder_bib
hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider
hibernate.datasource=
hibernate.transaction.manager_lookup_class=
components.xml:
<?xml version="1.0" encoding="UTF-8"?>
<components xmlns="http://jboss.com/products/seam/components"
xmlns:core="http://jboss.com/products/seam/core"
xmlns:drools="http://jboss.com/products/seam/drools"
xmlns:mail="http://jboss.com/products/seam/mail"
xmlns
ersistence="http://jboss.com/products/seam/persistence"
xmlns:security="http://jboss.com/products/seam/security"
xmlns:transaction="http://jboss.com/products/seam/transaction"
xmlns:web="http://jboss.com/products/seam/web"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.2.xsd">
<!-- core:init debug="true" jndi-pattern="@jndiPattern@"/ -->
<core:manager concurrent-request-timeout="500"
conversation-id-parameter="cid" conversation-timeout="120000" parent-conversation-id-parameter="pid"/>
<core:resource-loader>
<core:bundle-names>
<value>messages</value>
</core:bundle-names>
</core:resource-loader>
<!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
<web:hot-deploy-filter url-pattern="*.seam"/>
<persistence:entity-manager-factory name="entityManagerFactory" persistence-unit-name="WonderBib"/>
<persistence:managed-persistence-context auto-create="true"
entity-manager-factory="#{entityManagerFactory}" name="entityManager"/>
<transaction:entity-transaction entity-manager="#{entityManager}"/>
<drools:rule-base name="securityRules">
<drools:rule-files>
<value>/security.drl</value>
</drools:rule-files>
</drools:rule-base>
<security:rule-based-permission-resolver security-rules="#{securityRules}"/>
<security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
<event type="org.jboss.seam.security.notLoggedIn">
<action execute="#{redirect.captureCurrentView}"/>
</event>
<event type="org.jboss.seam.security.loginSuccessful">
<action execute="#{redirect.returnToCapturedView}"/>
</event>
<mail:mail-session host="localhost" password="123456" port="25" username="melih"/>
<!-- For use with jBPM pageflow or process management
<bpm:jbpm> <bpm
rocess-definitions></bpm
rocess-definitions> <bpm
ageflow-definitions></bpm
ageflow-definitions>
</bpm:jbpm> -->
</components>
Danke
Ich will JBOSS SEAM, JPA, Hibernate verwenden.
Ich habe Book Entitiy erstellt und versucht in DB zu speichern. Leider gibt dieser Fehlermeldung aus.
Exception in thread "main" java.lang.NullPointerException
at org.domain.wonderbib.main.BookMain.main(BookMain.java:25)
Ich verstehe nicht wo liegt das Problem ?
ich schicke all die Datein, villeicht können Sie mir sagen, wo ist mein Problem?
Java:
package org.domain.wonderbib.dao;
import org.domain.wonderbib.exeption.DAOException;
import javassist.NotFoundException;
/**
* Common methods which should be implemented for an Entity
* The first type parameter K is the type to use as the key and the second type parameter E is the type of the entity
*/
public interface BaseDAO<K,T> {
/**
* find Object by Id
* @param id
* @return found Object
*/
T find(K id) throws DAOException, NotFoundException;
void create(T entity) throws DAOException;
void edit(T entity) throws DAOException;
void remove(T entity) throws DAOException;
}
Java:
package org.domain.wonderbib.dao;
import java.util.List;
import org.domain.wonderbib.entity.Book;
public interface BookDAO extends BaseDAO<Long, Book> {
public List<Book> getAllBook();
public Book findBookWihtName(String titel, String auto);
}
Java:
package org.domain.wonderbib.dao.hibernate;
import javax.persistence.EntityManager;
import javassist.NotFoundException;
import org.domain.wonderbib.exeption.DAOException;
import org.jboss.seam.annotations.In;
import org.jboss.seam.log.Log;
/**
* Implementation of common methods for an Entity.
* @author Gulsun Sahin
*/
abstract class AbstractDAO<T> {
@org.jboss.seam.annotations.Logger Log log;
@In EntityManager entityManager;
private Class<T> entityClass;
public AbstractDAO(Class<T> entityClass) {
this.entityClass = entityClass;
}
public void create(T entity) throws DAOException {
entityManager.persist(entity);
}
public void edit(T entity) throws DAOException {
entityManager.merge(entity);
}
public void remove(T entity) throws DAOException {
entityManager.remove(entityManager.merge(entity));
}
public T find(Long id) throws DAOException, NotFoundException {
log.debug("Find Object with id " + id + " and entityClass " + entityClass);
T object = entityManager.find(entityClass, id);
if (object == null) {
throw new NotFoundException("Object with id " + id + " is not found!");
}
return object;
}
}
Java:
package org.domain.wonderbib.dao.hibernate;
import java.util.List;
import javax.persistence.EntityManager;
import org.domain.wonderbib.dao.BookDAO;
import org.domain.wonderbib.entity.Book;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
@Name("bookDAO")
@AutoCreate()
public class BookDAOImpl extends AbstractDAO <Book>implements BookDAO {
@In EntityManager entityManager;
public BookDAOImpl(Book book) {
super(Book.class);
// TODO Auto-generated constructor stub
}
@Override
public List <Book>getAllBook() {
List<Book> listBooks = this.entityManager.createNamedQuery(Book.ALL_BOOKS).getResultList();
return null;
}
@Override
public Book findBookWihtName(String titel, String auto) {
// TODO Auto-generated method stub
return null;
}
}
Java:
package org.domain.wonderbib.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Entity
@Table(name = "book")
@NamedQueries({
@NamedQuery(name = Book.ALL_BOOKS, query = "from Book "),
})
public class Book {
int id;
String ibnNummer;
String titel;
String autor;
String sprache;
int seite;
public final static String ALL_BOOKS = "all_books";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "titel")
public String getTitel() {
return titel;
}
public void setTitel(String titel) {
this.titel = titel;
}
@Column(name = "ibn_nummer")
public String getIbnNummer() {
return ibnNummer;
}
public void setIbnNummer(String ibnNummer) {
this.ibnNummer = ibnNummer;
}
@Column(name = "autor")
public String getAutor() {
return autor;
}
public void setAutor(String autor) {
this.autor = autor;
}
@Column(name = "sprache")
public String getSprache() {
return sprache;
}
public void setSprache(String sprache) {
this.sprache = sprache;
}
@Column(name = "seite")
public int getSeite() {
return seite;
}
public void setSeite(int seite) {
this.seite = seite;
}
}
Java:
package org.domain.wonderbib.session;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import org.domain.wonderbib.dao.BookDAO;
import org.domain.wonderbib.dao.hibernate.BookDAOImpl;
import org.domain.wonderbib.entity.*;
import org.domain.wonderbib.exeption.DAOException;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
@Name("bookAction")
@Scope(ScopeType.SESSION)
public class BookAction {
List <Book> bookList = new ArrayList<Book>();
@In BookDAO bookDAO;
@In Book book;
@Create
public void create() {
System.out.println("creating BookAction");
this.bookList=bookDAO.getAllBook();
}
public void createBook(Book book) throws DAOException{
bookDAO.create(book);
}
// public List<Book> getAllBooks(){
//
// bookList=bookDAO.getAllBook();
// return bookList;
// }
public List<Book> getBookList() {
return bookList;
}
public void setBookList(List<Book> bookList) {
this.bookList = bookList;
}
}
Java:
package org.domain.wonderbib.main;
import org.domain.wonderbib.entity.Book;
import org.domain.wonderbib.exeption.DAOException;
import org.domain.wonderbib.session.BookAction;
import org.jboss.seam.annotations.In;
public class BookMain {
@In
static BookAction bookAction;
public static void main (String args[]) throws DAOException {
Book book = new Book();
book.setAutor("autor");
book.setIbnNummer("1234564");
book.setId(2);
book.setSeite(150);
book.setSprache("Englisch");
book.setTitel("The Walking Dead");
bookAction.createBook(book);
//bookAction.create();
//System.out.println(bookAction.getBookList().get(0).getTitel()+" " +bookAction.getBookList().get(0).getAutor());
}
}
hibernate-console-properties:
#File used by hibernate tools to override <datasource> and other container specific settings in persistence.xml
hibernate.connection.password=root
hibernate.connection.username=
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.connection.url=jdbc:mysql://localhost:3306/wonder_bib
hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider
hibernate.datasource=
hibernate.transaction.manager_lookup_class=
components.xml:
<?xml version="1.0" encoding="UTF-8"?>
<components xmlns="http://jboss.com/products/seam/components"
xmlns:core="http://jboss.com/products/seam/core"
xmlns:drools="http://jboss.com/products/seam/drools"
xmlns:mail="http://jboss.com/products/seam/mail"
xmlns
xmlns:security="http://jboss.com/products/seam/security"
xmlns:transaction="http://jboss.com/products/seam/transaction"
xmlns:web="http://jboss.com/products/seam/web"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.2.xsd">
<!-- core:init debug="true" jndi-pattern="@jndiPattern@"/ -->
<core:manager concurrent-request-timeout="500"
conversation-id-parameter="cid" conversation-timeout="120000" parent-conversation-id-parameter="pid"/>
<core:resource-loader>
<core:bundle-names>
<value>messages</value>
</core:bundle-names>
</core:resource-loader>
<!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
<web:hot-deploy-filter url-pattern="*.seam"/>
<persistence:entity-manager-factory name="entityManagerFactory" persistence-unit-name="WonderBib"/>
<persistence:managed-persistence-context auto-create="true"
entity-manager-factory="#{entityManagerFactory}" name="entityManager"/>
<transaction:entity-transaction entity-manager="#{entityManager}"/>
<drools:rule-base name="securityRules">
<drools:rule-files>
<value>/security.drl</value>
</drools:rule-files>
</drools:rule-base>
<security:rule-based-permission-resolver security-rules="#{securityRules}"/>
<security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
<event type="org.jboss.seam.security.notLoggedIn">
<action execute="#{redirect.captureCurrentView}"/>
</event>
<event type="org.jboss.seam.security.loginSuccessful">
<action execute="#{redirect.returnToCapturedView}"/>
</event>
<mail:mail-session host="localhost" password="123456" port="25" username="melih"/>
<!-- For use with jBPM pageflow or process management
<bpm:jbpm> <bpm
</bpm:jbpm> -->
</components>
Danke