在Vaadin中绑定外键(EclipseLink) [英] Binding foreign key in Vaadin (EclipseLink)

查看:129
本文介绍了在Vaadin中绑定外键(EclipseLink)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Vaadin和EclipseLink.有2个表,一个表是 Mail ,第二个表是 Customer .客户的PK为customer_id,邮件的PK为mail_id. 表邮件具有Customer_id作为外键.如何绑定它? 我试过了:

I'm using Vaadin and EclipseLink. There are 2 tables, one is Mail, second in Customer. PK for Customer is customer_id, PK for mail is mail_id. Table Mail has Customer_id as a foreign key. How do I bind it? I tried:

binder.forField(fkCustomerId)
        .withConverter(new StringToBigDecimalConverter(FormMessages.NUMBERS_ONLY))
        .bind(Mail::getCustomerId, Mail::setCustomerId);

然后我检查了Mail-entity类并发现

Then I checked the Mail-entity class and found

@JoinColumn(name = "CUSTOMER_ID", referencedColumnName = "CUSTOMER_ID")
@ManyToOne
private Customer customerId;

我检查了此页面- https://vaadin.com/docs/v8/framework/datamodel/datamodel-forms.html ,但是与fk绑定无关.

I checked this page - https://vaadin.com/docs/v8/framework/datamodel/datamodel-forms.html but there was nothing about fk binding.

推荐答案

选项1:转换客户ID

在这里,我假设Customer类的ID字段称为id,并且其类型为Long.这使您可以通过生成未格式化字符串表示形式的转换器访问该ID字段.请注意,这不能很好地处理null值.

Here I am assuming that ID field on Customer class is called id and that it is of type Long. This allows you to access that ID field through converter that produces unformatted string presentation. Note that this does not handle null value nicely.

binder.forField(fkCustomerId)
        .withConverter(Long::valueOf, String::valueOf)
        .bind("customerId.id");

选项2:转换客户对象

此示例将使用客户"对象,并将其转换为客户ID"字段所需的格式.这样,您就可以处理null值,并根据整个对象的状态进行更高级的格式化.

This example would take Customer object and convert that to desired format for the customer ID field. This allows you to handle null values and do more advanced formatting based on state of the whole object.

binder.forField(fkCustomerId)
        .withConverter(new CustomerToCustomerIdConverter())
        .bind(Mail::getCustomerId, Mail::setCustomerId);

您可以省略转换器中的convertToModel结果,因为您不应使用用户键入的ID值来创建客户对象.

You can omit convertToModelresult in the converter because you should not be creating customer objects from ID values that user types.

public class CustomerToCustomerIdConverter implements Converter<String, Customer> {
    @Override
    public Result<Customer> convertToModel(String s, ValueContext valueContext) {
        return Result.error("not supported");
    }

    @Override
    public String convertToPresentation(Customer customer, ValueContext valueContext) {
        return Objects.toString(customer.getCustomerId(), "");
    }
}

文本字段样式

为了设置TextField样式,您需要将其设置为只读模式.而且,如果您根本不想获得文本字段的边框,则需要添加额外的样式.

In order to setup TextField style, you need to set it to read-only mode. And if you do not want to get the text field border at all then you need to add extra style.

TextField fkCustomerId = new TextField();
fkCustomerId.addStyleName(ValoTheme.TEXTFIELD_BORDERLESS);
fkCustomerId.setReadOnly(true);

这篇关于在Vaadin中绑定外键(EclipseLink)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆