-1

Until now I add the things I want to have in my list over the getter which makes no sense and end in a mess up in my database, when adding a new entry.

My model until now:

@Data
@Entity
public class Telefonbuch {
    /*@PostConstruct
    public void init() {
        //geschaeftsstellen = new ArrayList<String>();
        geschaeftsstellen.add("Dortmund");
        System.out.println("TEEEEEEEEEEEST");
        geschaeftsstellen.add("Essen");
        geschaeftsstellen.add("Stralsund");
        geschaeftsstellen.add("Stuttgart");
        geschaeftsstellen.add("Zürich");
        geschaeftsstellen.add("Istanbul");
        geschaeftsstellen.add("Köln");
        geschaeftsstellen.add("Aachen");
        geschaeftsstellen.add("Berlin");
    }*/

    public List<String> getGeschaeftsstellen() {
        geschaeftsstellen = new ArrayList<String>();
        geschaeftsstellen.add("Dortmund");
        System.out.println("TEEEEEEEEEEEST");
        geschaeftsstellen.add("Essen");
        geschaeftsstellen.add("Stralsund");
        geschaeftsstellen.add("Stuttgart");
        geschaeftsstellen.add("Zürich");
        geschaeftsstellen.add("Istanbul");
        geschaeftsstellen.add("Köln");
        geschaeftsstellen.add("Aachen");
        geschaeftsstellen.add("Berlin");

        //ArrayList<String> a = new ArrayList<String>();
        //a.add("Test");
        return geschaeftsstellen;

    }

    public void setGeschaeftsstellen(List<String> geschaeftsstellen) {
        this.geschaeftsstellen = geschaeftsstellen;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column
    private String vorname;
    @Column
    private String nachname;
    @Column
    private String telefonnummer;
    @Column
    private String handynummer;
    @Column
    private String geschaeftsstelle;

    @Column
    @ElementCollection
    private List<String> geschaeftsstellen; 

I just show the other variables to let you know what is in this. I know the @Column annotation for the List is not necessary. But it's a question for me. Should data like this be in a database or not? I will never use it again somewhere else. What is the right way to insert the data of the list for these selection buttons? When I just uncomment the PostConstruct it won't get called. When I add @ManagedBean I get the error: DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled. But in my .xhtml I already have selection="#{telefonbuchList.selectedEntry}" selectionMode="single" rowKey="#{telefonbuch.id}"

How do i know which import is the right one? javax.annotation or javax.faces.bean? Just with the annotation one it gets called.

enter image description here

Here can see where I use it. It's the tab "Neuer Eintrag" for a newEntry. The tab "Telefonbuch" is the list with a table for all entities.

Edit: TelefonbuchListController on Request:

@Scope (value = "session")
@Component (value = "telefonbuchList")
@ELBeanName(value = "telefonbuchList")
@Join(path = "/", to = "/eintraege-liste.jsf")
public class TelefonbuchListController {
    @Autowired
    private TelefonbuchRepository telefonbuchRepository;
    private List<Telefonbuch> eintraege;

    @Deferred
    @RequestAction
    @IgnorePostback
    public void loadData() { 
        eintraege = telefonbuchRepository.findAll(); 
    }
    public List<Telefonbuch> getEintraege() {
        return eintraege;
    }

    private Telefonbuch selectedEntry;

    public Telefonbuch getSelectedEntry() {
        return selectedEntry;
    }

    public void setSelectedEntry(Telefonbuch selectedEntry) {
        this.selectedEntry = selectedEntry;
    }

    public void deleteEntry() {
        telefonbuchRepository.delete(selectedEntry);
        eintraege.remove(selectedEntry);
        selectedEntry = null;
    }
CptDayDreamer
  • 1,526
  • 6
  • 25
  • 61
  • The class `Telefonbuch` is an entity class, persisted via JPA or the like. `@PostConstruct` is an annotation used for initializer methods on Managed Beans (not entitiy classes). Move `init()` to your `telefonbuchList` bean you did not provide the source code for. In `getGeschaeftsstellen()` only return the list prepared by the init() method. – Selaron Jan 21 '19 at 13:45
  • @Selaron do you mean I put in the init this: List geschaeftsstellen = new ArrayList(); then add the items and then Telefonbuch telefonbuch = new Telefonbuch(); and then telefonbuch.setGeschaeftsstellen(geschaeftsstellen); It won't show me anything. I added the TelefonbuchListController – CptDayDreamer Jan 21 '19 at 14:02

1 Answers1

0

I got your question that you are asking for a List getting filled with options usable with a selectOneRadio component - and why @PostConstruct does not work. @PostConstruct is ignored by JPA or other persistence frameworks - it's usable with CDI/Managed beans as far as I know.

Move it to your TelefonbuchList bean:

public class TelefonbuchListController {
    @PostConstruct
    public void init() {
        geschaeftsstellen = new ArrayList<String>();
        geschaeftsstellen.add("Dortmund");
        geschaeftsstellen.add("Essen");
       ...
    }

    public List<String> getGeschaeftsstellen() {
        return geschaeftsstellen;
    }
}

You can use this list to fill your selectOneRadio component select items.

Selaron
  • 6,105
  • 4
  • 31
  • 39
  • I didn't realised. It was the wrong controller. It would be TelefonbuchController only. My namings are bad. But it works. Thank you very much! – CptDayDreamer Jan 21 '19 at 14:30