Ich arbeite gerad an einem Project mit JSF, EclipseLink. Wenn ich mein Formular abschicke rufe ich
Java Code:
Die DAO sieht hierbei wie folgt aus
Der EntityManager sieht wie folgt aus
Die Entity sieht wie folgt aus
Die persistence.xml
Wenn ich jetzt allerdings das Formular abschicke über die obige save-Methode
wird mir ausgeaben
Weis einer von euch wieso ich den Fehler erhalte?
Java Code:
Java:
public String save() {
PersonDAO dao = new PersonDAO();
dao.create(user);
return "/registry.xhtml";
}
Die DAO sieht hierbei wie folgt aus
Java:
public class PersonDAO extends GenericDAO<Person> {}
Java:
public class GenericDAO<T> implements AbstractBasicDAO<T> {
/**
* the entity manager
*
* @var EntityManager
*/
protected EntityManager em;
/**
* this is the class type to handle
*
* @var Class<T>
*/
private Class<T> type;
/**
* default constructor
*/
public GenericDAO() {
Type t = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) t;
type = (Class) pt.getActualTypeArguments()[0];
this.em = EntityManagerUtil.getEntityManager();
}
/**
* Make an instance managed and persistent.
*
* @param t the entity to store
* @return the stored entity
*/
@Override
public T create(final T t) {
this.em.persist(t);
return t;
}
/**
* Find by a spezified primary key.
*
* @param id the id to find
* @return the found entity
*/
@Override
public T find(final Object id) {
return (T) this.em.find(type, id);
}
/**
* Find all entites by an given query
*
* @param sql the sql query
* @return the found entity
*/
@Override
public List find(String sql) {
return em.createQuery(sql).getResultList();
}
/**
* Merge the state of the given entity into the current persistence context.
*
* @param t the entity to store
* @return the stored entity
*/
@Override
public T update(final T t) {
return this.em.merge(t);
}
/**
* delete an object by his pramary key
*
* @param id the priamary key to delete
*/
@Override
public void delete(final Object id) {
this.em.remove(this.em.getReference(type, id));
}
}
Der EntityManager sieht wie folgt aus
Java:
public class EntityManagerUtil {
final static org.apache.log4j.Logger logger = Logger.getLogger(EntityManagerUtil.class);
/**
* the entity manager
*
* @var EntityManager
*/
private static EntityManager entityManager;
/**
* default constructor
*
* @throw ExceptionInInitializerError
*/
static {
try {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("JAPPU");
entityManager = factory.createEntityManager();
} catch( Throwable ex ) {
logger.error("Error on EntityManager inializing", ex);
throw new IllegalStateException(ex);
}
}
/**
* get the EntityManager
*
* @return EntityManager
*/
public static EntityManager getEntityManager() {
return entityManager;
}
}
Die Entity sieht wie folgt aus
Java:
package com.abado.jap.entities.user;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlTransient;
@Entity
@Table(name="person")
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "id")
private Long id;
@OneToMany(mappedBy = "createdBy")
private Collection<Person> creators;
@JoinColumn(name = "created_by", referencedColumnName = "id")
@ManyToOne
private Person createdBy;
@Column(name = "created_at")
@Temporal(TemporalType.TIMESTAMP)
private Date created_at;
@OneToMany(mappedBy = "modifiedBy")
private Collection<Person> modifiers;
@JoinColumn(name = "modified_by", referencedColumnName = "id")
@ManyToOne
private Person modifiedBy;
@Column(name = "modified_at")
@Temporal(TemporalType.TIMESTAMP)
private Date modified_at;
@Basic(optional = false)
@NotNull
@Column(name = "block")
private boolean block;
@Size(max = 255)
@Column(name = "active_code")
private String activeCode;
@Column(name = "gender")
private EGender gender;
@Column(name = "firstname")
private String firstname;
@Column(name = "lastname")
private String lastname;
@Column(name = "birthdate")
@Temporal(TemporalType.TIMESTAMP)
private Date birthdate;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
/**
* default constructor
*/
public Person() {
}
//--- GETTER / SETTER
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Person getCreatedby() {
return createdBy;
}
public void setCreatedBy(Person created_by) {
this.createdBy = created_by;
}
@XmlTransient
public Collection<Person> getCreators() {
return creators;
}
public void setCreators(Collection<Person> creators) {
this.creators = creators;
}
public Date getCreatedAt() {
return created_at;
}
public void setCreatedAt(Date created_at) {
this.created_at = created_at;
}
public Person getModifiedBy() {
return modifiedBy;
}
public void setModified_by(Person modified_by) {
this.modifiedBy = modified_by;
}
@XmlTransient
public Collection<Person> getModifiers() {
return modifiers;
}
public void setModifiers(Collection<Person> modifiers) {
this.modifiers = modifiers;
}
public Date getModified_at() {
return modified_at;
}
public void setModified_at(Date modified_at) {
this.modified_at = modified_at;
}
public boolean getBlock() {
return block;
}
public void setBlock(boolean block) {
this.block = block;
}
public String getActiveCode() {
return activeCode;
}
public void setActiveCode(String activeCode) {
this.activeCode = activeCode;
}
public EGender getGender() {
return gender;
}
public void setGender(EGender gender) {
this.gender = gender;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
*
* @return
*/
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
/**
*
* @param object
* @return
*/
@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 Person)) {
return false;
}
Person other = (Person) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
/**
*
* @return
*/
@Override
public String toString() {
return "com.abado.jap.entities.user.Person[ id=" + id + " ]";
}
}
Die 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="JAPPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.abado.jap.entities.user.Person</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jap?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
Wenn ich jetzt allerdings das Formular abschicke über die obige save-Methode
wird mir ausgeaben
javax.faces.el.EvaluationException: java.lang.IllegalArgumentException: Object: com.abado.jap.entities.user.Person[ id=null ] is not a known entity type.
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Object: com.abado.jap.entities.user.Person[ id=null ] is not a known entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4228)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)
at com.abado.jap.utils.database.impl.GenericDAO.create(GenericDAO.java:52)
at com.abado.jap.services.UserService.save(UserService.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 36 more
Weis einer von euch wieso ich den Fehler erhalte?