У меня есть этот код, который я делаю, чтобы узнать о JavaEE и JPA. Вся идея в том, что у меня есть три объекта: пользователь, профиль и позиция. Профиль - это своего рода уровень доступа для пользователя especifi c, и у пользователя может быть более одного профиля. Когда я сопоставляю эту связь, сервер выдает NotSerializableExeception.
Пользовательский объект:
package lemaf.zetta.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lemaf.zetta.EnSexo;
import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity
@NamedQueries({
@NamedQuery(name = "Usuario.ALL2",
query = "SELECT u, p FROM Usuario u INNER JOIN Perfil p INNER JOIN USUARIO_POSSUI_PERFIL up ON u.cpf = up.USUARIO AND p.id = up.PERFIL"
),
@NamedQuery(name = "Usuario.ALL",
query = "SELECT u FROM Usuario u"
),
@NamedQuery(name = "Usuario.COUNT_ALL", query = "SELECT COUNT(u) FROM Usuario u"),
//@NamedQuery(name = "Usuario.BY_ID", query = "SELECT u FROM Usuario u WHERE u.id = :id"),
@NamedQuery(name = "Usuario.BY_CPF", query = "SELECT u FROM Usuario u WHERE u.cpf = :cpf")
})
public class Usuario implements Serializable {
private static final long serialVersionUID = 1L;
String nome;
@Id
String cpf;
LocalDate dataNascimento;
EnSexo sexo;
@ManyToOne
Cargo cargo;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "USUARIO_POSSUI_PERFIL",
joinColumns = {@JoinColumn(name = "usuario", referencedColumnName = "cpf")},
inverseJoinColumns = {@JoinColumn(name = "perfil", referencedColumnName = "id")}
)
@JsonIgnore
List<Perfil> perfis = new ArrayList<>();
public Usuario(String nome, String cpf, LocalDate dataNascimento, EnSexo sexo, Cargo cargo, List<Perfil> perfis) {
this.cargo = cargo;
this.perfis = perfis;
this.nome = nome;
this.cpf = cpf;
this.dataNascimento = dataNascimento;
this.sexo = sexo;
}
public Usuario() {
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public LocalDate getDataNascimento() {
return dataNascimento;
}
public void setDataNascimento(LocalDate dataNascimento) {
this.dataNascimento = dataNascimento;
}
public EnSexo getSexo() {
return sexo;
}
public void setSexo(EnSexo sexo) {
this.sexo = sexo;
}
public Cargo getCargo() {
return cargo;
}
public void setCargo(Cargo cargo) {
this.cargo = cargo;
}
public List<Perfil> getPerfis() {
return perfis;
}
public void setPerfis(List<Perfil> perfis) {
this.perfis = perfis;
}
}
Профиль профиля:
package lemaf.zetta.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jdk.jfr.Name;
import org.apache.openjpa.persistence.jdbc.Unique;
import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Entity
@NamedQueries({
@NamedQuery(name = "Perfil.ALL", query = "SELECT p FROM Perfil p")
})
public class Perfil implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
@Unique
String nome;
public Perfil(String nome) {
this.nome = nome;
}
public Perfil(){}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
Stacktrace:
ava.lang.reflect.UndeclaredThrowableException
com.sun.proxy.$Proxy112.getUsuarios(Unknown Source)
lemaf.zetta.controller.UsuarioController.get(UsuarioController.java:32)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:566)
org.apache.openejb.server.cxf.rs.PojoInvoker.performInvocation(PojoInvoker.java:43)
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:201)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:104)
org.apache.openejb.server.cxf.rs.AutoJAXRSInvoker.invoke(AutoJAXRSInvoker.java:68)
org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59)
org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:267)
org.apache.openejb.server.cxf.rs.CxfRsHttpListener.doInvoke(CxfRsHttpListener.java:258)
org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:99)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.apache.openejb.server.httpd.EEFilter.doFilter(EEFilter.java:65)
Root Cause
java.io.NotSerializableException: org.apache.openjpa.enhance.RedefinitionHelper$1
java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1185)
java.base/java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1553)
java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1510)
java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1433)
java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1179)
java.base/java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1553)
java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1510)
java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1433)
java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1179)
java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
java.base/java.util.ArrayList.writeObject(ArrayList.java:896)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:566)
java.base/java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1145)
java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1497)
java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1433)
java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1179)
java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
org.apache.openejb.core.ivm.BaseEjbProxyHandler.copyObj(BaseEjbProxyHandler.java:597)
org.apache.openejb.core.ivm.BaseEjbProxyHandler.copy(BaseEjbProxyHandler.java:373)
org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:350)
com.sun.proxy.$Proxy112.getUsuarios(Unknown Source)
lemaf.zetta.controller.UsuarioController.get(UsuarioController.java:32)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:566)
org.apache.openejb.server.cxf.rs.PojoInvoker.performInvocation(PojoInvoker.java:43)
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:201)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:104)
org.apache.openejb.server.cxf.rs.AutoJAXRSInvoker.invoke(AutoJAXRSInvoker.java:68)
org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59)
org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:267)
org.apache.openejb.server.cxf.rs.CxfRsHttpListener.doInvoke(CxfRsHttpListener.java:258)
org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:99)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.apache.openejb.server.httpd.EEFilter.doFilter(EEFilter.java:65)
Репо : https://github.com/vhal9/ProvaZetta.git