Map <string,float =“”> </string,>的JPA映射

时间:2013-12-03 15:11:03

标签: java dictionary jpa persistence

在我的Java EE 6 / JPA 2项目中,Map实体中有advantages属性Employee

  @ElementCollection
   private Map<String, Float> advantages;

键代表advantage name,值代表与优势名称相关联的cost。这会映射到包含三列Employee_Id,优势和Advantages_Key的表格。我想保留一个Map条目,其中Employee_Id是数据表中所选Employee的Id。我怎样才能做到这一点?

UpDate:我在Employee Business类中尝试了这个方法,我注入了接口EmployeeManagerLocal

   public Map<String, Float> addAdvantage(String advantageName, Float cost, String  
    employeeId) {
     Employee emp = employeeManagerLocal.findById(employeeId);
     Map<String, Float> advantages = new HashMap<>();
     advantages.put(advantageName, cost);
     emp.setAdvantages(advantages);
     return advantages;
   }

然后,我在Emlpoyee Managed Bean

中调用此方法
   public String addAdvantage(){
     employeeBusinessLocal.addAdvantage(advantageName, cost, selectedEmployee.getId());
     return "index";
   }

但是显示了这个例外:

   Avertissement: DTX5014: Caught exception in beforeCompletion() callback:
   java.lang.ClassCastException:    
   org.eclipse.persistence.internal.sessions.DirectMapChangeRecord cannot be cast to 
   org.eclipse.persistence.internal.sessions.DirectCollectionChangeRecord
at 
    org.eclipse.persistence.mappings.DirectCollectionMapping.compareCollectionsForChange
    (DirectCollectionMapping.java:578)
at 
     org.eclipse.persistence.mappings.DirectMapMapping.calculateDeferredChanges
   (DirectMapMapping.java:173)
at 
   org.eclipse.persistence.descriptors.changetracking.AttributeChangeTrackingPolicy.
    createObjectChangeSet(AttributeChangeTrackingPolicy.java:75)
at org.eclipse.persistence.descriptors.changetracking.DeferredChangeDetectionPolicy
   .calculateChanges(DeferredChangeDetectionPolicy.java:89)
at 
    org.eclipse.persistence.descriptors.changetracking.AttributeChangeTrackingPolicy.
    calculateChangesForExistingObject(AttributeChangeTrackingPolicy.java:48)
at 
    org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.calculateChanges
   (UnitOfWorkImpl.java:643)
at 
    org.eclipse.persistence.internal.sessions.UnitOfWorkImpl
    .commitToDatabaseWithChangeSet
   (UnitOfWorkImpl.java:1490)
at 
    org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.issueSQLbeforeCompletion
    (UnitOfWorkImpl.java:3143)
at 
    org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.
    issueSQLbeforeCompletion(RepeatableWriteUnitOfWork.java:346)
at 
    org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion
   (AbstractSynchronizationListener.java:157)
at 
    org.eclipse.persistence.transaction.JTASynchronizationListener.beforeCompletion
   (JTASynchronizationListener.java:68)
at 
    com.sun.enterprise.transaction.JavaEETransactionImpl.
   commit(JavaEETransactionImpl.java:435)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.
    commit(JavaEETransactionManagerSimplified.java:855)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5136)
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 com.sun.proxy.$Proxy728.addAdvantage(Unknown Source)
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:606)
at org.jboss.weld.util.reflection.SecureReflections$13.work
    (SecureReflections.java:267)
at org.jboss.weld.util.reflection.SecureReflectionAccess.run
    (SecureReflectionAccess.java:52)
at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation
    (SecureReflectionAccess.java:137)
at org.jboss.weld.util.reflection.SecureReflections.invoke
    (SecureReflections.java:263)
at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke
   (EnterpriseBeanProxyMethodHandler.java:110)
at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke
   (EnterpriseTargetBeanInstance.java:56)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke
    (ProxyMethodHandler.java:105)
at com.gis.plannerplus.business.ejb.
    EmployeeBusinessLocal$-1104346579$Proxy$_$$_Weld$Proxy$.

   addAdvantage(EmployeeBusinessLocal$-1104346579$Proxy$_$$_Weld$Proxy$.java)
at com.gis.palannerplus.jsf.EmployeeManagedBean.addAdvantage 
    (EmployeeManagedBean.java:102)
at com.gis.palannerplus.jsf.EmployeeManagedBean$Proxy$_$$_WeldClientProxy.
    addAdvantage(EmployeeManagedBean$Proxy$_$$_WeldClientProxy.java)
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:606)
at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:779)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:528)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:257)
at com.sun.el.parser.AstValue.invoke(AstValue.java:248)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke
    (ForwardingMethodExpression.java:39)
at org.jboss.weld.el.WeldMethodExpression.invoke
    (WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke
    (TagMethodExpression.java:105)
at javax.faces.event.MethodExpressionActionListener.processAction
    (MethodExpressionActionListener.java:148)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute
   (InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
   (ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter
   (ApplicationFilterChain.java:217)
at com.gis.plannerplus.servlets.SessionFilter.doFilter(SessionFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
    (ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter
   (ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke
   (StandardWrapperValve.java:279)
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:744)

我正在使用Glassfish 3.1.2.2

1 个答案:

答案 0 :(得分:0)

使用

@CollectionTable(name="table_name", joinColumns=@JoinColumn(name="Employee_id"))
@MapKeyColumn(name="Advantages_Key")//the name of the advantage (String)
@Column(name="Advantages")//the cost (Float)
@ElementCollection
private Map<String, Float> advantages;