0

It's sad that I have to ask for this, but how can I store a char in my Postgres DB, but show String for the selection of the value. I'm creating a web application with JSF.

So my .xhtml:

<p:outputLabel for="gender" value="Geschlecht:" />
<p:selectOneMenu id="gender" value="#{telefonbuchController.telefonbuch.gender}" style="width:125px">
    <f:selectItem itemLabel="Auswählen" itemValue="" />
    <f:selectItems value="#{telefonbuchController.genderList}" var="c" itemLabel="#{gender}" itemValue="#{gender}"/>
</p:selectOneMenu>

Model:

@Column
private char gender;

In the controller for the form I create a list of Strings for male and female and init them with PostConstruct.

private List<String> geschaeftsstellen;

This

Here you see how it should look. I implemented it with a String, but I want to just store 'm' and 'f' in my database instead of the whole string.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
CptDayDreamer
  • 1,526
  • 6
  • 25
  • 61

1 Answers1

0

Implement it like this:

public enum Gender {
MALE('m'),
FEMALE('f')
;

private char shortGender;

public Gender (char shortGender) {
    this.shortGender = shortGender;
}

public char getShortGender() {
    return shortGender;
}

} 

And in your template use different values for itemLabel and itemValue, i.e.:

itemLabel="#{gender}" itemValue="#{gender.shortGender}"
smsnheck
  • 1,563
  • 3
  • 21
  • 33
  • Many of these basic jsf things have Q/A with many upvotes. Sorting on votes and reading the, and remember their existence/titles helps a lot. Saves you writing questions! https://stackoverflow.com/questions/8229638/how-to-use-enum-values-in-fselectitems Voting as duplicate instead of answering saves time too ;-) And the company is? (Just curious) – Kukeltje Jan 24 '19 at 09:59
  • adesso @Kukeltje – smsnheck Jan 26 '19 at 21:01
  • @smsnheck +1 then – CptDayDreamer Jan 28 '19 at 11:14
  • How do I store it in the database? I mean I get the error that it cannot convert the type String to class Gender... – CptDayDreamer Jan 28 '19 at 12:19