// 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()); }