Hallo.
Ich versuche über Events eine lose Koppelung herzustellen. Allerdings kann ich die em.persist Methode nicht ausführen, da eine NullPointerException geworfen wird. Zum vereinfachten Testen inwieweit der User übergeben wird, habe ich in jeder Methode die verwendet wird, eine System.out.println Ausgabe eingefügt. Laut Ausgabe, wird der User korrekt übergeben. Bis zu dem Zeitpunkt, an dem er in die Datenbank gespeichert werden soll.
Hier die Klassen:
User:
UserListController:
UserEditController:
UserListProducer:
UserServiceBean:
persistence.xml:
Der EntityManager wird in einer anderen Klasse injiziert, weshalb die @Inject Annotation nicht vorhanden ist.
In einer JSF Datei wird zuerst die Methode doAddUser des UserListControllers aufgerufen.
Ich versuche über Events eine lose Koppelung herzustellen. Allerdings kann ich die em.persist Methode nicht ausführen, da eine NullPointerException geworfen wird. Zum vereinfachten Testen inwieweit der User übergeben wird, habe ich in jeder Methode die verwendet wird, eine System.out.println Ausgabe eingefügt. Laut Ausgabe, wird der User korrekt übergeben. Bis zu dem Zeitpunkt, an dem er in die Datenbank gespeichert werden soll.
Hier die Klassen:
User:
Code:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;
@Entity
@Table(name = "user")
@TableGenerator(name="userPkGen", table ="id_gen", pkColumnName ="gen_name",
valueColumnName ="gen_value", pkColumnValue="user_id_gen", allocationSize = 1)
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.TABLE, generator="userPkGen")
@Basic(optional = false)
@Column(name = "userId")
private Integer userId;
@Column(name = "userRegistrationDate")
@Temporal(TemporalType.DATE)
private Date userRegistrationDate;
@Basic(optional = false)
@Column(name = "username")
private String username;
@Basic(optional = false)
@Column(name = "password")
private String password;
@Column(name = "userTitle")
@Enumerated(EnumType.STRING)
private UserTitle userTitle;
@Column(name = "userFirstName")
private String userFirstName;
@Column(name = "userLastName")
private String userLastName;
@Column(name = "userAdress")
private String userAdress;
@Column(name = "userHouseNumber")
private Integer userHouseNumber;
@Column(name = "userZipCode")
private Integer userZipCode;
@Size(max = 55)
@Column(name = "userCity")
private String userCity;
@Size(max = 75)
@Column(name = "userEmail")
private String userEmail;
@Column(name = "userState")
private Integer userState;
@OneToMany(mappedBy="user")
private List<Company> companyList = new ArrayList<>();
@OneToMany(mappedBy="user")
private List<Purchaser> purchaserList = new ArrayList<>();
public User() {
}
public User(Integer userId) {
this.userId = userId;
}
public User(Integer userId, String username, String password, String userFirstName) {
this.userId = userId;
this.username = username;
this.password = password;
this.userFirstName = userFirstName;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Date getUserRegistrationDate() {
return userRegistrationDate;
}
public void setUserRegistrationDate(Date userRegistrationDate) {
this.userRegistrationDate = userRegistrationDate;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserTitle getUserTitle() {
return userTitle;
}
public void setUserTitle(UserTitle userTitle) {
this.userTitle = userTitle;
}
public String getUserFirstName() {
return userFirstName;
}
public void setUserFirstName(String userFirstName) {
this.userFirstName = userFirstName;
}
public String getUserLastName() {
return userLastName;
}
public void setUserLastName(String userLastName) {
this.userLastName = userLastName;
}
public String getUserAdress() {
return userAdress;
}
public void setUserAdress(String userAdress) {
this.userAdress = userAdress;
}
public Integer getUserHouseNumber() {
return userHouseNumber;
}
public void setUserHouseNumber(Integer userHouseNumber) {
this.userHouseNumber = userHouseNumber;
}
public Integer getUserZipCode() {
return userZipCode;
}
public void setUserZipCode(Integer userZipCode) {
this.userZipCode = userZipCode;
}
public String getUserCity() {
return userCity;
}
public void setUserCity(String userCity) {
this.userCity = userCity;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public Integer getUserState() {
return userState;
}
public void setUserState(Integer userState) {
this.userState = userState;
}
public List<Company> getCompanyList() {
return companyList;
}
public void setCompanyList(List<Company> companyList) {
this.companyList = companyList;
}
public List<Purchaser> getPurchaserList() {
return purchaserList;
}
public void setPurchaserList(List<Purchaser> purchaserList) {
this.purchaserList = purchaserList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (userId != null ? userId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof User)) {
return false;
}
User other = (User) object;
if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "de.zerrenda.zerrenda.User[ userId=" + userId + " ]";
}
}
UserListController:
Code:
import de.zerrenda.zerrendaee6.controller.UserEditController.Mode;
import de.zerrenda.zerrendaee6.entities.User;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@SessionScoped
public class UserListController implements Serializable{
@Inject
private UserEditController userEditController;
public String doAddUser(){
System.out.println("doAddUser ruft die Methode SetUserToEdit mit Parameter Add auf.");
userEditController.setUserToEdit(Mode.ADD);
return "registerSuccess" ;
}
public String doUpdateUser (User user){
userEditController.setUserToEdit(Mode.EDIT);
System.out.println("setUserToEdit wird mit EDIT aufgerufen");
return null;
}
public void doDeleteUser(User user){
userEditController.setUserToDelete(user);
}
}
UserEditController:
Code:
package de.zerrenda.zerrendaee6.controller;
import de.zerrenda.zerrendaee6.entities.User;
import de.zerrenda.zerrendaee6.qualifiers.Added;
import de.zerrenda.zerrendaee6.qualifiers.Updated;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@SessionScoped
public class UserEditController implements Serializable{
@Added
@Inject
private Event<User> userAddEventSrc;
@Updated
@Inject
private Event<User> userUpdateEventSrc;
private Mode mode;
private User user;
@PostConstruct
public void init(){
user = new User();
}
public enum Mode {
ADD, EDIT;
}
public void setUserToEdit(Mode mode){
setUserToEdit(mode, getUser());
}
public void setUserToEdit(Mode mode, User user){
this.mode = mode;
this.user = user;
doSave();
}
public String doSave() {
if(getMode() == Mode.ADD){
System.out.println("doSave Methode des UserEdtController wird mit ADD gefired");
System.out.println(user.getUserFirstName());
userAddEventSrc.fire(user);
}else {
System.out.println("doSave Methode des UserEdtController wird mit EDIT gefired");
userUpdateEventSrc.fire(user);
}
return "registerSuccess";
}
public void setUserToDelete(User user){
}
public Mode getMode() {
return mode;
}
public void setMode(Mode mode) {
this.mode = mode;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
UserListProducer:
Code:
package de.zerrenda.zerrendaee6.data;
import de.zerrenda.zerrendaee6.entities.User;
import de.zerrenda.zerrendaee6.qualifiers.Added;
import de.zerrenda.zerrendaee6.qualifiers.Deleted;
import de.zerrenda.zerrendaee6.qualifiers.Updated;
import de.zerrenda.zerrendaee6.services.UserService;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
@RequestScoped
public class UserListProducer {
@Inject
private UserService userService;
public void onUserAdded(@Observes @Added User user){
System.out.println("onUserAdded des UserListProducer wird ausgeführt");
System.out.println(user.getUserFirstName());
userService.addUser(user);
}
public void onUserUpdated(@Observes @Updated User user){
userService.updateUser(user);
System.out.println("onUserUpdated des UserListProducer wird ausgeführt");
}
public void onUserDeleted(@Observes @Deleted User user){
userService.deleteUser(user);
System.out.println("onUserDeleted des UserListProducer wird ausgeführt");
}
}
UserServiceBean:
Code:
package de.zerrenda.zerrendaee6.beans;
import de.zerrenda.zerrendaee6.entities.User;
import de.zerrenda.zerrendaee6.services.UserService;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
@Stateless
public class UserServiceBean implements UserService{
EntityManager em;
@Override
public User addUser(User user) {
em.persist(user);
System.out.println("addUser der UserServiceBean wird aufgerufen");
return user;
}
@Override
public User updateUser(User user) {
return em.merge(user);
}
@Override
public void deleteUser(User user) {
User userToDelete = em.find(User.class, user.getUserId());
em.remove(userToDelete);
}
@Override
public <List>User getAllUsers() {
return null;
}
}
persistence.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ZerrendaPU" transaction-type="JTA">
<jta-data-source>jdbc/zerrendadb</jta-data-source>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
Der EntityManager wird in einer anderen Klasse injiziert, weshalb die @Inject Annotation nicht vorhanden ist.
In einer JSF Datei wird zuerst die Methode doAddUser des UserListControllers aufgerufen.
Zuletzt bearbeitet: