JPA实体加入问题(我认为)

时间:2010-09-12 20:19:57

标签: java jpa

我正在学习JPA并遇到问题,我在我的persistance文件中使用实体生成的东西(太累了,不能想到这个名字),所以我的实体是最好的。但我加入他们时遇到了问题。

我的问题是我不认为我正确加入实体

异常

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 

调用From(将em注入bean中)

public Department getDepartment(int id){
  Department dep = null;
  dep = em.find(Department.class,id);
 return dep;
}

部门

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;
 }
}

员工

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;
 }
}

持久性

<?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>

如果有人能给我一个令人惊奇的牌 再次感谢 乔恩

2 个答案:

答案 0 :(得分:1)

您的主键(标有@Id)为int,并且您尝试将Long传递给em.find(..) - 改为通过int

答案 1 :(得分:0)

看起来您正在使用Web Service,并且响应的内容与WSDL不匹配。

  

引起:com.sun.xml.ws.streaming.XMLStreamReaderException:意外的XML标记。预期:{http://test.com/} getDepartmentResponse但找到:{http://schemas.xmlsoap.org/soap/envelope/}信封

您是如何构建响应的?