OneToMany Bean Problem

Hi,
Sorry for my bad english :smiley:

I want do to a OneToMany Relationship with JPA / Beans.
The Database seems to be correct but my ComboBox does not Represent the Value of my Entity and did not save the Value.
The other fields are working fine.

[code]
@Entity
public class Mwm implements Serializable {
private static final long serialVersionUID = 1L;

public static final String PRIMARY_KEY = null;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String Bezeichnung;
private Integer Nummer;

@OneToMany (mappedBy="mwm")
java.util.Set<Art> arts;

[/code][code]
@Entity
public class Art implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String ArtNr;
private String Bezeichnung;



@ManyToOne
private Mwm mwm;

[/code]I Try to Represent the Values Like This.

[code]
mwm = new ComboBox(ā€œMWMā€);
mwm.setContainerDataSource(mwmContainer);
//mwm.setConverter(converter);
mwm.setConverter(MwmConverter.class);

    // create FieldGroup
    editorFields = new BeanFieldGroup<Art>(Art.class);
    editorFields.setItemDataSource(new BeanItem<Art>(a));
    editorFields.bindMemberFields(this);

[/code]And I have a Converter ā€¦

[code]
public class MwmConverter implements Converter<String, Set>
{
private static final long serialVersionUID = -220989690852205509L;

@Override
public Set convertToModel(String value,
Class<? extends Set> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
return null; // Unused
}

@Override
public String convertToPresentation(Set value,
Class<? extends String> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
return value.toString(); // Java 8
}

@Override
public Class<Set> getModelType() {
return (Class<Set>) new HashSet().getClass().getSuperclass();
}

@Override
public Class getPresentationType() {
return String.class;
}

}
[/code]I hope someone can help me
I think the mistake is in the Converter or maybe at the Entity

Hi Miller.

Your entities are OK.

I suggest to use de JPAContainer add-on and use the built-in converter

import com.vaadin.addon.jpacontainer.fieldfactory.SingleSelectConverter
.
.
.
mwm.setConverter(new SingleSelectConverter(mwm));

thatā€™s work for me

Hi,

Iā€™d suggest to use Viritin. It overcomes some fundamental issues in relation handling with basic Vaadin select components and also makes the API simpler due to better typed API. In this case Iā€™d uses TypedSelect and populate your possible Mwm instances with its setOptions method. Then data binding should work just fine.

Viritin also contains other suitable fields for ElementCollection, OneToMany or ManyToMany fields that you probably face soon as well. And there is a LazyList/LazyCombobox/MTable for efficient binding of large tables.

One note though, for correct functionality in all situations, you should ensure you have properly implemented equals/hashCode methods in your entity.
An example of a simple approach
using a mapped superclass.

BTW. What comes to JPAContainer, I donā€™t know a situation where Iā€™d use it instead of helpers in Viritin myself. And I have worked with both libraries, a lot :wink:

cheers,
matti

Hi,
I wonder whether you need a Converter at all.
Probably following will work :

[code]
mwm = new ComboBox(ā€œMWMā€);
mwm.setContainerDataSource(mwmContainer);
mwm.setItemCaptionMode(ItemCaptionMode.PROPERTY);
mwm.setItemCaptionPropertyId(ā€œbezeichnungā€);

// create FieldGroup
editorFields = new BeanFieldGroup(Art.class);
editorFields.setItemDataSource(new BeanItem(a));
editorFields.bindMemberFields(this);
[/code]Please note the fieldname ā€œbezeichnungā€ is starting with a lowercase
Fieldnames in your entities should start with a lowercase. Maybe this is also causing you trouble.

cheers,
Koenraad

Hey,

Thank you for your ReplyĀ“s.
I try to solve my problems with this solutions.
:smiley: