JPA entity joining issues (i think)
Im learing JPA and having problems, im using entity generation stuff(too tired, cant think of the name) in my persistance file so my entitys are upto scratch. But im having problems joining them.
my problem is that i dont think im joining the entitys properly
Exception
javax.servlet.ServletException: java.lang.reflect.InvocationTargetException at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:326) at org.glassfish.webservices.monitoring.WebServiceTesterServlet.invoke(WebServiceTesterServlet.java:102) at org.glassfish.webservices.EjbWebServiceServlet.service(EjbWebServiceServlet.java:110) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at com.sun.grizzly.http.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:195) at com.sun.grizzly.http.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:139) at com.sun.grizzly.http.servlet.ServletAdapter.doService(ServletAdapter.java:376) at com.sun.grizzly.http.servlet.ServletAdapter.service(ServletAdapter.java:329) at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:166) at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:100) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:245) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57) at com.sun.grizzly.ContextTask.run(ContextTask.java:69) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309) at java.lang.Thread.run(Thread.java:619) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.glassfish.webservices.monitoring.WebServiceTesterServlet.doPost(WebServiceTesterServlet.java:297) ... 24 more Caused by: com.sun.xml.ws.streaming.XMLStreamReaderException: unexpected XML tag. expected: {http://test.com/}getDepartmentResponse but found: {http://schemas.xmlsoap.org/soap/envelope/}Envelope at com.sun.xml.ws.streaming.XMLStreamReaderUtil.verifyTag(XMLStreamReaderUtil.java:214) at com.sun.xml.ws.streaming.XMLStreamReaderUtil.verifyTag(XMLStreamReaderUtil.java:222) at com.sun.xml.ws.client.sei.ResponseBuilder$DocLit.readResponse(ResponseBuilder.java:546) at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:121) at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:89) at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:140) at $Proxy362.getDepartment(Unknown Source) ... 29 more
org.eclipse.persistence.internal.jpa.EntityManagerImpl.findInternal(EntityManagerImpl.java
:647) at
org.eclipse.persistence.internal.jpa.EntityManagerImpl.find(EntityManagerImpl.java:581) at
org.eclipse.persistence.internal.jpa.EntityManagerImpl.find(EntityManagerImpl.java:460)
com.sun.enterprise.container.common.impl.EntityManagerWrapper.find(EntityManagerWrapper.ja
va:309) at com.test.TestBean.getDepartment(TestBean.开发者_运维技巧java:126)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at
java.lang.reflect.Method.invoke(Method.java:597) at
org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.jav
a:1056) at
org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1
128) at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5292) at
com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:615) at
com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.ja
va:797) at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:567)
com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy
.java:157)
com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorP
roxy.java:139) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597) at
com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.j
ava:858)
com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.ja
va:797) at
com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:3
67) at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5264) at
com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5252)
com.sun.ejb.containers.WebServiceInvocationHandler.invoke(WebServiceInvocationHandler.java
:188) ... 66 more
Called From (with em is injected into the bean)
public Department getDepartment(int id){
Department dep = null;
dep = em.find(Department.class,id);
return dep;
}
Department
package com.test1;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
@Entity
public class Department {
@Id
private int id;
private String name;
@OneToMany(mappedBy="department")
@OrderBy("name ASC")
private List<Employee> employees;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
Employee
package com.test1;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
@Id private int id;
private String name;
private long salary;
private Department department;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public void setDepartment(Department department) {
this.department = department;
}
public Department getDepartment() {
return department;
}
}
Persistance
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="testConnection" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/testingManagment</jta-data-source>
<non-jta-data-source>jdbc/testingManagment</non-jta-data-source>
<class>com.test1.Employee</class>
<class>com.test1.Department</class>
<properties>
<!-- maintains a most frequently used subcache that uses soft references -->
<property name="eclipselink.cache.type.default" value="SoftWeak"/>
<!-- makes cashe accsessable to other clients attached to the session -->
<property name="eclipselink.cache.shared.default" value="true"/>
<property name="eclipselink.flush-clear.cache" value="Drop"/>
<!-- JDBC definition -->
<property name="eclipselink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="eclipselink.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="eclipselink.jdbc.user" value="system" />
<property name="eclipselink.jdbc.password" value="nightmare11" />
<property name="eclipselink.jdbc.read-connections.min" value="1" />
<property name="eclipselink.jdbc.write-connections.min" value="1" />
<property name="eclipselink.jdbc.batch-writing" value="JDBC" />
<!-- Logging -->
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.timestamp" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.logging.thread" value="false" />
<!-- Table Creation Stuff -->
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
<property name="eclipselink.application-location" value="generatedSQL"/>
<!-- -->
</properties>
</persistence-unit>
</persistence>
If anyone can give me a hand that would be amazing Thanks again Jon
Your primary key (marked with @Id
) is int
, and you try to pass Long
to em.find(..)
- pass int
instead.
It looks like you're using Web Service and the content of the response doesn't match the WSDL.
Caused by: com.sun.xml.ws.streaming.XMLStreamReaderException: unexpected XML tag. expected: {http://test.com/}getDepartmentResponse but found: {http://schemas.xmlsoap.org/soap/envelope/}Envelope
How are you building the response?
精彩评论