public static class PersonSerializer implements JsonSerializer<Person> { public JsonElement serialize(final Person person, final Type type, final JsonSerializationContext context) { JsonObject result = new JsonObject(); result.add("id", new JsonPrimitive(person.getId())); result.add("name", new JsonPrimitive(person.getName())); Person parent = person.getParent(); if (parent != null) { result.add("parent", new JsonPrimitive(parent.getId())); } return result; } }
tx.commit(); System.out.println("Person id: " + person.getId()); } catch (RuntimeException e) { tx.rollback();
Table<String, Long, Person> personsByNameAndId = HashBasedTable.create(); Person bob = ...; //put it in the table personsByNameAndId.put(bob.getName(), bob.getId(), bob); //lookup by name and ID Person bobWithId1 = personsByNameAndId.get("Bob", 1l); //get all Bobs Collection<Person> allPersonsNamedBob = personsByNameAndId.row("bob").values(); //get person with ID=2 regardless of name Person personWithId2 = personsByNameAndId.column(2l).values().iterator().next();
public class PersonAdatper extends TypeAdapter<Person> { @Override public void write(JsonWriter writer, Person value) throws IOException { writer.beginObject(); writer.name("id").value(value.getId()); writer.name("name").value(value.getName()); Person parent = value.getParent(); if (parent != null) { writer.name("parent").value(parent.getId()); } writer.endObject(); } @Override public Person read(JsonReader in) throws IOException { // do something you need return null; } }
public void testReactiveSavePerson() { Person person = new Person("Jane", "Doe"); assertNull(person.getId()); //null before save
StringBuilder builder = new StringBuilder(); for (Person p: person) { builder.append(p.getId() + " " + p.getName() + " " + p.getHotness() + " " + p.getAge()); } tv.setText(builder.toString());
public class PersonExample{ public static void main(String[] args) { Session session = null; try { SessionFactory sessionFactory = new Configuration().configure() .buildSessionFactory(); session = sessionFactory.openSession(); System.out.println("Starting select"); List<Person> persons= session.createSQLQuery("select {p.*} from Person p").addEntity("p", Person.class).list(); for (Iterator<Person> it = persons.iterator(); it.hasNext();) { Person stObject = it.next(); System.out.println("ID: " + stObject.getId()); System.out.println("Surname: " + stObject.getSurname()); System.out.println("Name: " + stObject.getName()); System.out.println("Address: " + stObject.getAddress()); } System.out.println("Finished select"); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } finally { // Actual contact insertion will happen at this step session.flush(); session.close(); } } }
public class PersonTest { @Test public testFind(@Mocked final WebService ws) { final int id = 123; new NonStrictExpectations() {{ ws.getPersonById(id); result = new Person(id); }}; Person personFound = new Person().find(id); assertEquals(id, personFound.getId()); } }
Map<String,Person> peopleMap = new Map<String,Person>(); Person person = new Person(); //let's imagine this person has a string id attribute with a get method defined peopleMap.put(person.getId(),person); //adds the person to the list peopleMap.get("p71"); //assume we're looking for a person which id="p71";
for (Iterator<Person> iterator = listPerson .iterator(); iterator.hasNext(); ) { Person person= iterator.next(); //this peace of code works for update value if(person.getName().equals(name) && !person.getId().equals(id)){ person.setId(id); }else if (!person.getName().equals(name) && person.getId().equals(id)){ person.setName(name); } }
@RepositoryEventHandler @Component public class PersonEventHandler { @PersistenceContext private EntityManager entityManager; @HandleBeforeSave public void handlePersonSave(Person newPerson) { entityManager.detach(newPerson); Person currentPerson = personRepository.findOne(newPerson.getId()); if (!newPerson.getName().equals(currentPerson.getName)) { //react on name change } } }
List<Person> persons = Lists.newArrayList(); persons.add(new Person("1", "aaa",23, true)); persons.add(new Person("2", "bbb",16, false)); persons.add(new Person("3", "ccc",56, true)); Person per = persons.stream().filter(p -> p.getAge() > 50).findFirst().get(); System.out.println(per.getId());
Map<String, Person> mapOfObjects = new HashMap<String, Person>(); mapOfObjects.put("map1", new Person(1, 1000)); mapOfObjects.put("map2", new Person(1, 1000)); mapOfObjects.put("map3", new Person(1, 1000)); mapOfObjects.put("map4", new Person(2, 1000)); mapOfObjects.put("map5", new Person(2, 1000)); mapOfObjects.put("map6", new Person(3, 1000)); Map<Integer, Integer> mapOfSum = new HashMap<Integer, Integer>(); for (Person person : mapOfObjects.values()) { Integer value= mapOfSum.get(person.getId()); if (value == null) { value = 0; } value += person.getAmount(); mapOfSum.put(person.getId(), value); } System.out.println(mapOfSum);
public static void main(String[] args) throws Exception { Person person = new Person("name1", 1); Address address = new Address("home1", "office1"); StringWriter out = new StringWriter(); JsonWriter writer = new JsonWriter(out); writer.setIndent(" "); writer.beginObject(); writer.name("person"); writer.beginArray().beginObject(); writer.name("name").value(person.getName()); writer.name("id").value(person.getId()); writer.endObject().endArray(); writer.name("address"); writer.beginObject(); writer.name("homeaddress").value(address.getHomeaddress()); writer.name("officeaddress").value(address.getOfficeaddress()); writer.endObject(); writer.endObject(); writer.close(); System.out.println(out); }
private void saveGooglePlusUserData(Person currentPerson) { int gender_id = currentPerson.getGender(); Person.Name name = currentPerson.getName(); String account_id = currentPerson.getId(); String email = mPlusClient.getAccountName(); String bDate = currentPerson.getBirthday(); int ageGroup = countGPlusUserAge(bDate); // Person.Name name = {"familyName":"surname","givenName":"Name"} // BirthDate = 1988-04-24 String firstName = ""; String lastName = ""; try { JSONObject jObj = new JSONObject(name.toString()); firstName = jObj.getString("givenName"); lastName = jObj.getString("familyName"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.v(TAG_GPLUS, "FirstName = " + firstName + " Name = " + name); Log.v(TAG_GPLUS, "LastName = " + lastName); Log.v(TAG_GPLUS, "Account_id = " + account_id); Log.v(TAG_GPLUS, "Email = " + email); Log.v(TAG_GPLUS, "Gender ID = " + gender_id); Log.v(TAG_GPLUS, "BirthDate = " + bDate); }
for(Person p : collection{ pstmnt.setString(1,p.getName()); pstmnt.setString(2,p.getSurname()); pStmnt.setInt(3,p.getId()); pStmnt.setString(3,p.getPhone()); pStmnt.execteUpdate(); }
Person p1 = new Person(); Person p2 = new Person(); Relationship r = new Relationship(); // create the personFrom bi-directional association r.setPersonFrom(p1); List<Relationship> relationships = new ArrayList<Relationship>(); relationships.add(r); p1.setRelationships(relationships); // these four lines should be moved to some // link management method (see update below). // create the personTo uni-directional association r.setPersonTo(p2); session.persist(p2); session.persist(p1); assertNotNull(p2.getId()); assertNotNull(p1.getId()); assertNotNull(r.getId());
@Service("userService") @Transactional public class UserService { @Resource(name = "sessionFactory") private SessionFactory sessionFactory; public void mergeWithExistingAndUpdate(final Person personFromPost) { final Session session = sessionFactory.getCurrentSession(); final Person existingPerson = (Person) session.get(Person.class, personFromPost.getId()); // set here explicitly what must/can be overwritten by the html form POST existingPerson.setName(personFromPost.getName()); existingPerson.setEmail(personFromPost.getEmail()); existingPerson.setPassword(Utils.encodePassword(personFromPost.getPassword())); // to get the idea existingPerson.setDateModified(new Date()); existingPerson.setUserModified(Utils.getCurrentUser()); session.update(existingPerson); } }
String page = ServerContextFactory.get().getContextPath() + "/reverseajax/clock.html"; Browser.withPage(page, new Runnable() { public void run() { Util.setValue("clockDisplay", output); } }); // Creates a new Person bean. Person person = new Person(true); // Creates a multi-dimensional array, containing a row and the rows column data. String[][] data = { {person.getId(), person.getName(), person.getAddress(), person.getAge()+"", person.isSuperhero()+""} }; // Call DWR's util which adds rows into a table. peopleTable is the id of the tbody and // data conta ins the row/column data. Util.addRows("peopleTable", data);
public static void main(String[] args) { // Setup 100 persons in a linked circle. Person startingPerson = new Person(1); Person currentPerson = startingPerson; for (int i = 2; i <= 100; i++) { currentPerson.setNext(new Person(i)); currentPerson = currentPerson.getNext(); } currentPerson.setNext(startingPerson); // Loop around until a single person is left. currentPerson = startingPerson; while (currentPerson != currentPerson.getNext()) { currentPerson.killNext(); currentPerson = currentPerson.getNext(); } System.out.println("Surviving person: " + currentPerson.getId()); }