JPA可以在表中嵌入绑定字段和setVisibleColumns吗?

时间:2014-08-08 05:01:09

标签: java jpa vaadin vaadin7

我在表格中尝试setVisibleColumns()来显示@Embeddable类的值。我有两节Nucleo和Endereco。 Endereco是一个Embeddable类,Nucleo使用Endereco。 尝试表的setVisibleColumns会返回一些例外,我不知道如何解决这个问题。

其他问题,如何将BeanFieldGroup与此Embeddable类一起使用。

我正在尝试这个。

@Entity
public class Nucleo {

    @Id @GeneratedValue
    private Long id;

    @NotNull @NotEmpty @Size(min=3, max=50) @Column(unique=true)
    private String descricao;

    //embedded
    private Endereco endereco;

    @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY) 
    @JoinColumn(name="id_unidadeescolar")
    private UnidadeEscolar unidadeEscolar;

    public Nucleo() {
        this(0L, "", null);
    }

    public Nucleo(Long id, String descricao, Endereco endereco) {
        super();
        this.id = id;
        this.descricao = descricao;
        this.endereco = endereco;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public Endereco getEndereco() {
        return endereco;
    }

    public void setEndereco(Endereco endereco) {
        this.endereco = endereco;
    }

    public UnidadeEscolar getUnidadeEscolar() {
        return unidadeEscolar;
    }

    public void setUnidadeEscolar(UnidadeEscolar unidadeEscolar) {
        this.unidadeEscolar = unidadeEscolar;
    }
}


@Embeddable
public class Endereco {

    private String endereco;
    private Integer numero;
    private String complemento;
    private String bairro;
    private String cidade;

    @Enumerated(EnumType.STRING)
    private EstadosDoBrasil uf;

    private String cep;

    public Endereco() {
        this("", 0, "",
             "", EstadosDoBrasil.AC, "");
    }

    public Endereco(String endereco, Integer numero, String complemento,
            String bairro, EstadosDoBrasil uf, String cep) {
        super();
        this.endereco = endereco;
        this.numero = numero;
        this.complemento = complemento;
        this.bairro = bairro;
        this.uf = uf;
        this.cep = cep;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public Integer getNumero() {
        return numero;
    }

    public void setNumero(Integer numero) {
        this.numero = numero;
    }

    public String getComplemento() {
        return complemento;
    }

    public void setComplemento(String complemento) {
        this.complemento = complemento;
    }

    public String getBairro() {
        return bairro;
    }

    public void setBairro(String bairro) {
        this.bairro = bairro;
    }

    public EstadosDoBrasil getUf() {
        return uf;
    }

    public void setUf(EstadosDoBrasil uf) {
        this.uf = uf;
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }   
}


//view
Table tabela = new Table("", datasource){
            @Override
            protected String formatPropertyValue(Object rowId,Object colId, Property<?> property) {                 
                if(property.getType() == Long.class){
                    DecimalFormat df = new DecimalFormat("00000");
                    return df.format((Long)property.getValue());
                }               
                return super.formatPropertyValue(rowId, colId, property);
            }
        };
        tabela.setSelectable(true);
        tabela.setVisibleColumns(new Object[]{"id","descricao", "unidadeEscolar.nome", "endereco.cidade", "endereco.uf"});
        tabela.setColumnHeaders(new String[] {"#","Descrição", "Unidade escolar", "Cidade", "UF"}); 
        tabela.addItemClickListener(this);
        tabela.setColumnWidth("id", 50);
        tabela.setColumnWidth("descricao", 200);
        //tabela.setColumnWidth("unidadeEscolar.nome", 200);
        //tabela.setColumnWidth("endereco.cidade", 200);
        //tabela.setColumnWidth("endereco.uf", 25);
        tabela.setSizeFull();
        mainLayout.addComponent(tabela);

Vaadin book chap: 9.4.5。将字段绑定到Bean ,有一个使用buildAndBind("Street", "address.street")的示例,我尝试按照此示例添加endereco.cidade并返回异常。

尝试

field = binder.buildAndBind("Cidade:", "endereco.cidade", TextUpper.class);
        cidade = (TextUpper)field;
        cidade.setWidth("5cm");
        cidade.setMaxLength(50);
        mainLayout.addComponent(cidade);

//例外

ago 08, 2014 3:02:30 AM com.vaadin.server.DefaultErrorHandler doDefault
GRAVE: 
com.vaadin.server.ServerRpcManager$RpcInvocationException: Unable to invoke method click in com.vaadin.shared.ui.button.ButtonServerRpc
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:170)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
    at com.vaadin.server.communication.ServerRpcHandler.handleBurst(ServerRpcHandler.java:207)
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:111)
    at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:91)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1382)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168)
    ... 30 more
Caused by: com.vaadin.event.ListenerMethod$MethodException: Invocation of method buttonClick in br.com.webapp.views.NucleoView failed.
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:528)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:969)
    at com.vaadin.ui.Button.fireClick(Button.java:368)
    at com.vaadin.ui.Button$1.click(Button.java:57)
    ... 35 more
Caused by: com.vaadin.data.fieldgroup.FieldGroup$BindException: Property type for 'endereco.cidade' could not be determined. No property with that id was found.
    at com.vaadin.data.fieldgroup.FieldGroup.getPropertyType(FieldGroup.java:369)
    at com.vaadin.data.fieldgroup.BeanFieldGroup.getPropertyType(BeanFieldGroup.java:42)
    at com.vaadin.data.fieldgroup.FieldGroup.buildAndBind(FieldGroup.java:1037)
    at br.com.webapp.views.NucleoForm.bindingFields(NucleoForm.java:162)
    at br.com.webapp.views.NucleoForm.insert(NucleoForm.java:82)
    at br.com.webapp.views.NucleoView.buttonClick(NucleoView.java:131)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    ... 40 more

1 个答案:

答案 0 :(得分:2)

您的例外消息是:

> Ids must exist in the Container or as a generated column, missing id:
> endereco.cidade

抛出该异常是因为您的容器(假设它是BeanItemContainer)仅添加了Nucleo类的属性。该物业&#39; endereco&#39;是另一个类,它的属性不会添加到您的容器中。

如果您以后需要Endereco课程的其他属性,则可以将其所有属性添加到您的容器中:

datasource.addNestedContainerBean("endereco");

或者你可以添加你的cidade属性。

datasource.addNestedContainerProperty("endereco.cidade");