Estou criando um WS com banco Postgresql e usando o JPA e Jersey. Tenho uma tabela usuario que está populada com alguns usuários, com esta tabela eu consigo dar um GET(getAllUser) tranquilamente e tenho o response esperado. Meu problme está em outra tabela chamada TbProduto que tem um relacionamento ManyToOne com a TbCategoria, nem mesmo o GET eu consigo fazer nesta tabela abaixo estou enviando meu código e código do erro para analisarem.
TbProduto:
Entity
Table(name = "tbproduto", schema = "public")
NamedQuery(name = "TbProduto.findAll", query = TbProduto.BASE_QUERY)
XmlRootElement
public class TbProduto implements Serializable {
private static final long serialVersionUID = 1L;
public static final String BASE_QUERY = "SELECT t FROM TbProduto t";
Id
GeneratedValue(strategy = GenerationType.IDENTITY)
Basic(optional = false)
Column(name = "id")
private Integer id;
Column(name = "description")
private String description;
Column(name = "image")
private byte[] image;
Column(name = "manufacturer")
private String manufacturer;
Column(name = "name")
private String name;
Column(name = "price")
private BigDecimal price;
Column(name = "weight")
private String weight;
ManyToOne
JoinColumn(name = "tbCategoria", referencedColumnName = "id")
private TbCategoria tbCategoria;
public TbProduto() {
}
public TbProduto(String description, byte[] image, String manufacturer, String name, BigDecimal price,
String weight, TbCategoria tbcategoria) {
this.description = description;
this.image = image;
this.manufacturer = manufacturer;
this.name = name;
this.price = price;
this.weight = weight;
this.tbCategoria = tbcategoria;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public byte[] getImage() {
return this.image;
}
public void setImage(byte[] image) {
this.image = image;
}
public String getManufacturer() {
return this.manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPrice() {
return this.price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public String getWeight() {
return this.weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public TbCategoria getTbCategoria() {
return this.tbCategoria;
}
public void setTbCategoria(TbCategoria tbCategoria) {
this.tbCategoria = tbCategoria;
}
}
TbCategoria:
Entity
Table(name = "tbcategoria", schema = "public")
XmlRootElement
NamedQuery(name = "TbCategoria.findAll", query = TbCategoria.BASE_QUERY)
public class TbCategoria implements Serializable {
private static final long serialVersionUID = 1L;
public static final String BASE_QUERY = "SELECT t FROM TbCategoria t";
Id
GeneratedValue(strategy = GenerationType.IDENTITY)
Basic(optional = false)
Column(name = "id")
private Integer id;
Column(name = "name")
private String name;
OneToMany(mappedBy = "tbCategoria")
private Collection<TbProduto> tbProdutos;
public TbCategoria() {
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Collection<TbProduto> getTbProdutos() {
return this.tbProdutos;
}
public void setTbProdutos(Collection<TbProduto> tbProdutos) {
if (tbProdutos == null || tbProdutos.isEmpty()) {
tbProdutos = new ArrayList<>();
}
this.tbProdutos = tbProdutos;
}
public TbProduto addTbProduto(TbProduto tbProduto) {
getTbProdutos().add(tbProduto);
tbProduto.setTbCategoria(this);
return tbProduto;
}
public TbProduto removeTbProduto(TbProduto tbProduto) {
getTbProdutos().remove(tbProduto);
tbProduto.setTbCategoria(null);
return tbProduto;
}
}
METODO getAllProducts na Service
SuppressWarnings("unchecked")
public Collection<TbProduto> getAllProducts() {
return getEm().createQuery(TbProduto.BASE_QUERY).getResultList();
}
METODO getAllProducts na Resource
GET
Produce(MediaType.APPLICATION_JSON)
public Collection<TbProduto> getAllProducts() {
return productService.getAllProducts();
}
ERRO:
Apache Tomcat/7.0.47 - Error reportHTTP Status 500 - A MultiException has 2 exceptions. They are:
type Exception report
message A MultiException has 2 exceptions. They are:
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: A MultiException has 2 exceptions. They are: 1. java.lang.ExceptionInInitializerError 2. java.lang.IllegalStateException: Unable to perform operation: create on br.com.bigfarma.app.resource.ProductResource
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:489) org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)root cause
A MultiException has 2 exceptions. They are: 1. java.lang.ExceptionInInitializerError 2. java.lang.IllegalStateException: Unable to perform operation: create on br.com.bigfarma.app.resource.ProductResource
org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:392) org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487) org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162) org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022) org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:774) org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:737) org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:707) org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172) org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:284) org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:109) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:92) org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:61) org.glassfish.jersey.process.internal.Stages.process(Stages.java:197) org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:318) org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) org.glassfish.jersey.internal.Errors.process(Errors.java:315) org.glassfish.jersey.internal.Errors.process(Errors.java:297) org.glassfish.jersey.internal.Errors.process(Errors.java:267) org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305) org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154) org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473) org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)root cause
java.lang.ExceptionInInitializerError sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) java.lang.reflect.Constructor.newInstance(Unknown Source) org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1375) org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:272) org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:366) org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487) org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162) org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022) org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:774) org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:737) org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:707) org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172) org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:284) org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:109) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:92) org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:61) org.glassfish.jersey.process.internal.Stages.process(Stages.java:197) org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:318) org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) org.glassfish.jersey.internal.Errors.process(Errors.java:315) org.glassfish.jersey.internal.Errors.process(Errors.java:297) org.glassfish.jersey.internal.Errors.process(Errors.java:267) org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305) org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154) org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473) org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)root cause
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: br.com.bigfarma.app.entity.TbProduto.idCategoria in br.com.bigfarma.app.entity.TbCategoria.tbProdutos org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:775) org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:725) org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621) org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589) org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858) org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885) org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58) javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) br.com.bigfarma.app.service.AbstractEntityManager.<init>(AbstractEntityManager.java:8) br.com.bigfarma.app.service.ProductService.<init>(ProductService.java:7) br.com.bigfarma.app.resource.ProductResource.<clinit>(ProductResource.java:23) sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) java.lang.reflect.Constructor.newInstance(Unknown Source) org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1375) org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:272) org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:366) org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487) org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162) org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022) org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:774) org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:737) org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:707) org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172) org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:284) org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:109) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:92) org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:61) org.glassfish.jersey.process.internal.Stages.process(Stages.java:197) org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:318) org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) org.glassfish.jersey.internal.Errors.process(Errors.java:315) org.glassfish.jersey.internal.Errors.process(Errors.java:297) org.glassfish.jersey.internal.Errors.process(Errors.java:267) org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305) org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154) org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473) org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.
Apache Tomcat/7.0.47
Se puderem me ajudar agradeço!
