2012-11-28 62 views
0

我有一個問題,同時建立相關的表中的一個實體(我使用的是MS SQL Server和NetBeans IDE中的企業應用7.2 - EJB3)如何使用雙主鍵創建ejb實體?

我的數據庫包括:

  • 項目表:主鍵:專案編號(INT)
  • 用戶表:主鍵:用戶名(字符串)
  • UserProject表:
    • 專案編號(FK參考表項目)
    • 用戶名(FK參考表的用戶)
    • ...和其他一些領域...
    • 主鍵:專案編號和用戶名(雙PK)

當我創建從實體類數據庫,UserProjectPK類自動創建。 這裏是類UserProject:

public class UserProject implements Serializable { 
    @JoinColumn(name = "projectId", referencedColumnName = "projectId", insertable = false, updatable = false) 
    @ManyToOne(optional = false) 
    private Projects projects; 

    @JoinColumn(name = "invitedLeader", referencedColumnName = "username") 
    @ManyToOne 
    private Users invitedLeader; 

    @JoinColumn(name = "username", referencedColumnName = "username", insertable = false, updatable = false) 
    @ManyToOne(optional = false) 
    private Users users; 
    private static final long serialVersionUID = 1L; 

    @EmbeddedId 
    protected UserProjectPK userProjectPK; 

    @Size(max = 30) 
    @Column(name = "position") 
    private String position; 

    @Column(name = "startDate") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date startDate; 

    @Basic(optional = false) 
    @NotNull 
    @Column(name = "confirmed") 
    private boolean confirmed; 

    //getter and setter function 

我試圖創建在UserProjectFacade新UserProject實體(會話Bean的實體類):

public boolean join(Projects project, Users user, Users invitedLeader, String position, Date startDate) { 
    UserProject newUserProject = new UserProject(); 
    UserProjectPK newUserProjectPK = new UserProjectPK(user.getUsername(), project.getProjectId()); 
    newUserProject.setUserProjectPK(newUserProjectPK); 
    newUserProject.setProjects(project); 
    newUserProject.setUsers(user); 
    newUserProject.setInvitedLeader(invitedLeader); 
    newUserProject.setPosition(position); 
    newUserProject.setStartDate(startDate); 
    if (invitedLeader == user) { 
     newUserProject.setConfirmed(true); 
    } else { 
     newUserProject.setConfirmed(false); 
    } 
    try { 
     create(newUserProject); 
     return true; 
    } catch (Exception e) {} 
    return false; 
} 

當我運行功能,GlassFish服務器報告出現以下錯誤:

WARNING: EJB5184:A system exception occurred during an invocation on EJB UserProjectFacade, method: public boolean ass2.session.UserProjectFacade.join(ass2.entity.Projects,ass2.entity.Users,ass2.entity.Users,java.lang.String,java.util.Date) 
WARNING: javax.ejb.EJBException 
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5215) 
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5113) 
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901) 
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045) 
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994) 
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222) 
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89) 
at $Proxy379.join(Unknown Source) 
at ass2.UserProjectSessionStub.join(UserProjectSessionStub.java:35) 
at org.apache.jsp.createProject_jsp._jspService(createProject_jsp.java:118) 
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770) 
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411) 
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473) 
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770) 
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) 
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) 
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) 
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) 
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
at java.lang.Thread.run(Thread.java:722) 
Caused by: java.lang.NullPointerException 
at ass2.session.UserProjectFacade.join(UserProjectFacade.java:37) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) 
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) 
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388) 
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) 
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) 
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162) 
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144) 
at sun.reflect.GeneratedMethodAccessor97.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) 
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370) 
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5360) 
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348) 
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214) 
... 34 more 

我是EJB3的新手,所以我不知道如何處理這個問題。 請幫助我(儘快)。萬分感謝!

回答

0

堆棧跟蹤揭示了其中的問題:

... 
Caused by: java.lang.NullPointerException 
at ass2.session.UserProjectFacade.join(UserProjectFacade.java:37) 

檢查您UserProjectFacade班線37,你會得到答案。順便說一句,你確定在這一行

.. 
if (invitedLeader == user) { 
.. 

你想比較引用,而不是他們的主鍵?

相關問題