Relacionamento ManytoMany com Vraptor e Hibernate

Boa tarde galera,

Tenho um relacionamento de manytomany , eu consigo criar a tabela associativa pelo hibernate mas não sei como nem o que fazer no jsp e no controller para que a tabela associativa seja prenchida.
Tabela1:


//outros campos
@ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="profissional_unidade", joinColumns = @JoinColumn(name="idProfissional"),
                                       inverseJoinColumns = @JoinColumn(name="idUnidade"))
    @Cascade(CascadeType.ALL)
    private Collection<Unidade> unidade;
//getters e setters

Tabela2:

[code]//outros campos
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name=“profissional_unidade”, joinColumns=@JoinColumn(name=“idUnidade”),
inverseJoinColumns=@JoinColumn(name=“idProfissional”))

private Collection<Profissional> profissional;

//getters e setters[/code]

JSP com a listagem da tabela que deve ser relacionada na associativa:

<c:forEach var="uni" items="${unidades}"> <label> <input type="checkbox" name="profissional.unidade.idUnidade.id" value="${uni.id}" />${uni.unidade} </label> <br /> </c:forEach>

Controler:

[code]public void salvar(final Profissional profissional){

	// validações
	
			
		dao.add(profissional);					
	
        result.include("notice", "Profissional " + profissional.getNome() + " adicionado(a) com sucesso.");
	    result.use(Results.logic()).redirectTo(ProfissionalController.class).list();
}[/code]

vc pode usar:

profissional.unidade[0].idUnidade.id
profissional.unidade[1].idUnidade.id
...
profissional.unidade[4].idUnidade.id

como nomes dos inputs

Assim deu erro.

Quando nao uso assim nao acontece o erro mas também nao popula a tabela associativa e da a seguinte mensagem:

16:32:08,860 DEBUG [VRaptor ] VRaptor received a new request 16:32:08,865 DEBUG [DefaultRequestExecution] executing stack DefaultRequestExecution 16:32:08,899 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ResourceLookupInterceptor 16:32:08,899 DEBUG [DefaultResourceTranslator] trying to access /profissional 16:32:08,900 DEBUG [DefaultResourceTranslator] found resource [DefaultResourceMethod: ProfissionalController.salvarProfissionalController.salvar(Profissional, Collection)] 16:32:08,903 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor FlashInterceptor 16:32:08,906 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor InterceptorListPriorToExecutionExtractor 16:32:08,913 DEBUG [InstantiatedInterceptorHandler] Invoking interceptor HibernateTransactionInterceptor 16:32:08,913 DEBUG [InstantiatedInterceptorHandler] Invoking interceptor InstantiateInterceptor 16:32:08,934 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ParametersInstantiatorInterceptor 16:32:08,934 DEBUG [OgnlParametersProvider] Applying profissional.confirmaSenha with [7] 16:32:08,935 DEBUG [OgnlParametersProvider] Applying profissional.descricao with [] 16:32:08,936 DEBUG [OgnlParametersProvider] Applying profissional.matricula with [] 16:32:08,937 DEBUG [OgnlParametersProvider] Applying profissional.cpf with [] 16:32:08,941 DEBUG [OgnlParametersProvider] Applying profissional.numRegistro with [] 16:32:08,942 DEBUG [OgnlParametersProvider] Applying profissional.permissao with [] 16:32:08,944 DEBUG [OgnlParametersProvider] Applying profissional.login with [lalale] 16:32:08,945 DEBUG [OgnlParametersProvider] Applying profissional.email with [] 16:32:08,945 DEBUG [OgnlParametersProvider] Applying profissional.fone with [, ] 16:32:08,946 DEBUG [OgnlParametersProvider] unable to parse expression 'profissional.fone' ognl.OgnlException: fone [br.com.caelum.vraptor.VRaptorException: Unable to find converter for java.lang.String] at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:103) at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162) at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225) at ognl.ASTProperty.setValueBody(ASTProperty.java:127) at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220) at ognl.SimpleNode.setValue(SimpleNode.java:279) at ognl.ASTChain.setValueBody(ASTChain.java:227) at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220) at ognl.SimpleNode.setValue(SimpleNode.java:279) at ognl.Ognl.setValue(Ognl.java:737) at ognl.Ognl.setValue(Ognl.java:783) at br.com.caelum.vraptor.http.ognl.OgnlParametersProvider.createViaOgnl(OgnlParametersProvider.java:132) at br.com.caelum.vraptor.http.ognl.OgnlParametersProvider.createRoot(OgnlParametersProvider.java:108) at br.com.caelum.vraptor.http.ognl.OgnlParametersProvider.getParametersFor(OgnlParametersProvider.java:90) at br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor.getParametersFor(ParametersInstantiatorInterceptor.java:104) at br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor.intercept(ParametersInstantiatorInterceptor.java:77) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.interceptor.InstantiateInterceptor.intercept(InstantiateInterceptor.java:42) at br.com.caelum.vraptor.core.InstantiatedInterceptorHandler.execute(InstantiatedInterceptorHandler.java:47) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.util.hibernate.HibernateTransactionInterceptor.intercept(HibernateTransactionInterceptor.java:45) at br.com.caelum.vraptor.core.InstantiatedInterceptorHandler.execute(InstantiatedInterceptorHandler.java:47) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.interceptor.InterceptorListPriorToExecutionExtractor.intercept(InterceptorListPriorToExecutionExtractor.java:46) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:81) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.interceptor.ResourceLookupInterceptor.intercept(ResourceLookupInterceptor.java:67) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.core.DefaultRequestExecution.execute(DefaultRequestExecution.java:70) at br.com.caelum.vraptor.VRaptor$1.insideRequest(VRaptor.java:92) at br.com.caelum.vraptor.ioc.spring.SpringProvider.provideForRequest(SpringProvider.java:56) at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:89) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source) Caused by: br.com.caelum.vraptor.VRaptorException: Unable to find converter for java.lang.String at br.com.caelum.vraptor.core.DefaultConverters.to(DefaultConverters.java:57) at br.com.caelum.vraptor.http.ognl.VRaptorConvertersAdapter.convert(VRaptorConvertersAdapter.java:62) at br.com.caelum.vraptor.http.ognl.VRaptorConvertersAdapter.convertValue(VRaptorConvertersAdapter.java:58) at ognl.OgnlRuntime.getConvertedType(OgnlRuntime.java:1040) at ognl.OgnlRuntime.getConvertedTypes(OgnlRuntime.java:1057) at ognl.OgnlRuntime.getConvertedMethodAndArgs(OgnlRuntime.java:1083) at ognl.OgnlRuntime.getAppropriateMethod(OgnlRuntime.java:1169) at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1183) at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1454) at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85) ... 50 more /-- Encapsulated exception ------------\ br.com.caelum.vraptor.VRaptorException: Unable to find converter for java.lang.String at br.com.caelum.vraptor.core.DefaultConverters.to(DefaultConverters.java:57) at br.com.caelum.vraptor.http.ognl.VRaptorConvertersAdapter.convert(VRaptorConvertersAdapter.java:62) at br.com.caelum.vraptor.http.ognl.VRaptorConvertersAdapter.convertValue(VRaptorConvertersAdapter.java:58) at ognl.OgnlRuntime.getConvertedType(OgnlRuntime.java:1040) at ognl.OgnlRuntime.getConvertedTypes(OgnlRuntime.java:1057) at ognl.OgnlRuntime.getConvertedMethodAndArgs(OgnlRuntime.java:1083) at ognl.OgnlRuntime.getAppropriateMethod(OgnlRuntime.java:1169) at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1183) at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1454) at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85) at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162) at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225) at ognl.ASTProperty.setValueBody(ASTProperty.java:127) at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220) at ognl.SimpleNode.setValue(SimpleNode.java:279) at ognl.ASTChain.setValueBody(ASTChain.java:227) at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220) at ognl.SimpleNode.setValue(SimpleNode.java:279) at ognl.Ognl.setValue(Ognl.java:737) at ognl.Ognl.setValue(Ognl.java:783) at br.com.caelum.vraptor.http.ognl.OgnlParametersProvider.createViaOgnl(OgnlParametersProvider.java:132) at br.com.caelum.vraptor.http.ognl.OgnlParametersProvider.createRoot(OgnlParametersProvider.java:108) at br.com.caelum.vraptor.http.ognl.OgnlParametersProvider.getParametersFor(OgnlParametersProvider.java:90) at br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor.getParametersFor(ParametersInstantiatorInterceptor.java:104) at br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor.intercept(ParametersInstantiatorInterceptor.java:77) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.interceptor.InstantiateInterceptor.intercept(InstantiateInterceptor.java:42) at br.com.caelum.vraptor.core.InstantiatedInterceptorHandler.execute(InstantiatedInterceptorHandler.java:47) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.util.hibernate.HibernateTransactionInterceptor.intercept(HibernateTransactionInterceptor.java:45) at br.com.caelum.vraptor.core.InstantiatedInterceptorHandler.execute(InstantiatedInterceptorHandler.java:47) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.interceptor.InterceptorListPriorToExecutionExtractor.intercept(InterceptorListPriorToExecutionExtractor.java:46) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:81) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.interceptor.ResourceLookupInterceptor.intercept(ResourceLookupInterceptor.java:67) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:54) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:56) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:65) at br.com.caelum.vraptor.core.DefaultRequestExecution.execute(DefaultRequestExecution.java:70) at br.com.caelum.vraptor.VRaptor$1.insideRequest(VRaptor.java:92) at br.com.caelum.vraptor.ioc.spring.SpringProvider.provideForRequest(SpringProvider.java:56) at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:89) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source) \--------------------------------------/ 16:32:08,947 DEBUG [OgnlParametersProvider] Applying profissional.estado with [] 16:32:08,948 DEBUG [OgnlParametersProvider] Applying profissional.senha with [7] 16:32:08,948 DEBUG [OgnlParametersProvider] Applying profissional.sexo with [1] 16:32:08,949 DEBUG [OgnlParametersProvider] Applying profissional.nome with [oi] 16:32:08,950 DEBUG [OgnlParametersProvider] Applying profissional.id with [] 16:32:08,951 DEBUG [OgnlParametersProvider] Applying profissional.dtNascimento with [10/10/1990] 16:32:08,954 DEBUG [OgnlParametersProvider] Applying profissional.profissao.id with [1] 16:32:08,956 DEBUG [OgnlParametersProvider] Applying profissional.conselho with [] 16:32:08,957 DEBUG [OgnlParametersProvider] Applying profissional.unidade.idUnidade.id with [2, 1] 16:32:08,960 DEBUG [OgnlParametersProvider] cant find property for expression profissional.unidade.idUnidade.id ignoring 16:32:08,960 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for ProfissionalController.salvar(Profissional, Collection) as [profissional, unidades] 16:32:08,960 DEBUG [ParametersInstantiatorInterceptor] Parameter values for [DefaultResourceMethod: ProfissionalController.salvarProfissionalController.salvar(Profissional, Collection)] are [br.com.branet.sias.model.Profissional@d6ee82, null] 16:32:08,978 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ExecuteMethodInterceptor 16:32:08,978 DEBUG [ExecuteMethodInterceptor] Invoking ProfissionalController.salvar(Profissional, Collection) 16:32:08,993 DEBUG [DefaultLogicResult ] redirecting to class ProfissionalController 16:32:08,993 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for ProfissionalController.list() as [] 16:32:08,993 DEBUG [DefaultLogicResult ] redirecting to /sias/profissional/list 16:32:08,997 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor OutjectResult 16:32:09,002 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ForwardToDefaultViewInterceptor 16:32:09,003 DEBUG [ForwardToDefaultViewInterceptor] Request already dispatched and commited somewhere else, not forwarding. 16:32:09,006 DEBUG [VRaptor ] VRaptor ended the request 16:32:09,009 DEBUG [VRaptor ] VRaptor received a new request 16:32:09,014 DEBUG [DefaultRequestExecution] executing stack DefaultRequestExecution 16:32:09,034 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ResourceLookupInterceptor 16:32:09,034 DEBUG [DefaultResourceTranslator] trying to access /profissional/list 16:32:09,034 DEBUG [DefaultResourceTranslator] found resource [DefaultResourceMethod: ProfissionalController.listProfissionalController.list()] 16:32:09,037 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor FlashInterceptor 16:32:09,039 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor InterceptorListPriorToExecutionExtractor 16:32:09,044 DEBUG [InstantiatedInterceptorHandler] Invoking interceptor HibernateTransactionInterceptor 16:32:09,045 DEBUG [InstantiatedInterceptorHandler] Invoking interceptor InstantiateInterceptor 16:32:09,064 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ParametersInstantiatorInterceptor 16:32:09,064 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for ProfissionalController.list() as [] 16:32:09,064 DEBUG [ParametersInstantiatorInterceptor] Parameter values for [DefaultResourceMethod: ProfissionalController.listProfissionalController.list()] are [] 16:32:09,076 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ExecuteMethodInterceptor 16:32:09,076 DEBUG [ExecuteMethodInterceptor] Invoking ProfissionalController.list() 16:32:09,086 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor OutjectResult 16:32:09,087 DEBUG [OutjectResult ] outjecting profissionalList=[br.com.branet.sias.model.Profissional@3f45a8, br.com.branet.sias.model.Profissional@1f622fa, br.com.branet.sias.model.Profissional@726ef8, br.com.branet.sias.model.Profissional@d36d73, br.com.branet.sias.model.Profissional@b69c85, br.com.branet.sias.model.Profissional@ddf191, br.com.branet.sias.model.Profissional@11e680f, br.com.branet.sias.model.Profissional@141ce14, br.com.branet.sias.model.Profissional@140c244, br.com.branet.sias.model.Profissional@920d5d, br.com.branet.sias.model.Profissional@ab5809, br.com.branet.sias.model.Profissional@120e76d, br.com.branet.sias.model.Profissional@139c789, br.com.branet.sias.model.Profissional@5b0676, br.com.branet.sias.model.Profissional@568a8, br.com.branet.sias.model.Profissional@119efa7, br.com.branet.sias.model.Profissional@1c4fe21] 16:32:09,097 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ForwardToDefaultViewInterceptor 16:32:09,097 DEBUG [ForwardToDefaultViewInterceptor] forwarding to the dafault page for this logic 16:32:09,104 DEBUG [DefaultPageResult ] forwarding to /WEB-INF/jsp/profissional/list.jsp 16:32:09,105 DEBUG [DefaultStaticContentHandler] Deferring request to container: /sias/WEB-INF/jsp/profissional/list.jsp 16:32:09,130 DEBUG [VRaptor ] VRaptor ended the request

Acho que o problema está nessa parte:

16:32:08,957 DEBUG [OgnlParametersProvider] Applying profissional.unidade.idUnidade.id with [2, 1] 16:32:08,960 DEBUG [OgnlParametersProvider] cant find property for expression profissional.unidade.idUnidade.id ignoring

atualiza pro vraptor 3.3.0 e vê se dá o mesmo erro

Não funcionou, está com os mesmos erros.

Seria certo eu mesmo criar a entidade correspondente a tabela associativa e trabalhar isso no controller?

Não devo mudar nada no controller sera?

Ja consegui resolver.Obrigado pela atenção.

Applying profissional.unidade.idUnidade.id with [2, 1]

vc não mudou o nome dos inputs…

deveria ser:

profissional.unidade[0].idUnidade.id
profissional.unidade[1].idUnidade.id
etc

Funcionou da seguinte forma:

jsp:

<c:forEach var="uni" items="${unidades}" varStatus="s"> <label> <input type="checkbox" name="profissional.unidade[${s.count}].id" value="${uni.id}" />${uni.unidade} </label> <br /> </c:forEach>

E tive que por @Cascade(CascadeType.SAVE_UPDATE) no list de unidade da entidade Profissional:

@ManyToMany(fetch=FetchType.LAZY) @JoinTable(name="profissional_unidade", joinColumns = @JoinColumn(name="idProfissional"), inverseJoinColumns = @JoinColumn(name="idUnidade")) @Cascade(CascadeType.SAVE_UPDATE) private List<Unidade> unidade;

Nos Daos:

[code]public void add(Profissional profissional) {
this.session.merge(profissional);
this.session.flush();
}

public void update(Profissional profissional) {
this.session.merge(profissional);
this.session.flush();
}[/code]

Assim está funcionando perfeitamente.

Meu problema agora é em trazer o checkbox preenchido na edição.

Estou tentando fazer assim mas nao funciona:

Opa.

Ja consegui valeu mesmo assim.