private void listBeers() { List<Brewery> breweries = server.find(Brewery.class).findList(); for (Brewery brewery : breweries) { List<Beer> beers = brewery.getBeers(); for (Beer beer : beers) { System.out.println(beer.getName()); } } }
private void findBrewery() { System.out.print("brewery to find: "); String n = scanner.nextLine(); Brewery foundBrewery = server.find(Brewery.class).where().like("name", n).findUnique(); if (foundBrewery == null) { System.out.println(n + " not found"); return; } System.out.println(foundBrewery); for (Beer bier : foundBrewery.getBeers()) { System.out.println(" " + bier.getName()); } }
// jos kanta on luotu uudelleen, suoritetaan tämä ja laitetaan kantaan hiukan dataa private void seedDatabase() throws OptimisticLockException { Brewery brewery = new Brewery("Schlenkerla"); brewery.addBeer(new Beer("Urbock")); brewery.addBeer(new Beer("Lager")); // tallettaa myös luodut oluet, sillä Brewery:n OneToMany-mappingiin on määritelty // CascadeType.all server.save(brewery); // luodaan olut ilman panimon asettamista Beer b = new Beer("Märzen"); server.save(b); // jotta saamme panimon asetettua, tulee olot lukea uudelleen kannasta b = server.find(Beer.class, b.getId()); brewery = server.find(Brewery.class, brewery.getId()); brewery.addBeer(b); server.save(brewery); server.save(new Pub("Pikkulintu")); server.save(new Brewery("Paulaner")); }
private void addBeer() { System.out.print("to which brewery: "); String name = scanner.nextLine(); Brewery brewery = server.find(Brewery.class).where().like("name", name).findUnique(); if (brewery == null) { System.out.println(name + " does not exist"); return; } System.out.print("beer to add: "); name = scanner.nextLine(); Beer exists = server.find(Beer.class).where().like("name", name).findUnique(); if (exists != null) { System.out.println(name + " exists already"); return; } brewery.addBeer(new Beer(name)); server.save(brewery); System.out.println(name + " added to " + brewery.getName()); }