@Override public String toString() { return getId(); }
getLogger().debug( "Searching for parent-POM: " + parentModel.getId() + " of project: " + project.getId() + " in relative path: " + parentRelativePath ); getLogger().debug( "Parent-POM: " + parentModel.getId() + " for project: " + project.getId() + " cannot be loaded from relative path: " + parentDescriptor + "; path does not exist." ); "\n Specified: " + parentModel.getId() + "\n Found: " + candidateParent.getId() ); "Parent-POM: " + parentModel.getId() + " not found in relative path: " + parentRelativePath ); getLogger().debug( "Retrieving parent-POM: " + parentModel.getId() + " for project: " + project.getId() + " from the repository." );
@Override public String toString() { return getId(); }
public Child findChildByIndex(Parent parent, int index) { return (Child) em.createNativeQuery("SELECT * FROM childs WHERE parent_id = ?" , Child.class) .setParameter(0, parent.getId()) .setFirstResult(index) .setMaxResults(1) .getResultList().get(0); }
public List<Child> findByParent(final Parent parent) { return getHibernateTemplate().executeFind(new HibernateCallback<List<Child>>() { @Override public List<Child> doInHibernate(Session session) throws HibernateException, SQLException { return session.createCriteria(Child.class) .add(Restrictions.eq("parent.id", parent.getId())) .list(); } }); }
import org.codehaus.jackson.map.JsonSerializer import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.SerializerProvider; import java.io.IOException; public class ParentReferenceSerializer extends JsonSerializer<Parent> { @Override public void serialize(Parent parent, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeStartObject(); writeNumberField(jgen, "id", parent.getId()); // ... other fields ... jgen.writeEndObject(); } }
@ManagedBean public class MyManagedBean { private List<Parent> parentList;//+getter private Map<Long, Long> relatedChildrenB = new HashMap<Long,Long>();//+getter @EJB private ParentFacade parentFacade; @PostConstruct public void init() { parentList = parentFacade.findAll(); for (Parent parent : parentList) { relatedChildrenB.put(parent.getId(), parentFacade.getNumberOfRelatedChildrenB(parent)); } }
child.setParent(parent); parent.getChildren().add(child); Parent parentWithId = em.merge(parent); em.flush(); // make sure that the persistence context and database are in sync parentWithId.getId(); // works parentWithId.getChildren().get(0).getId(); // should also work
String hql = "select distinct p from Parent p" // get all the parents + " left join fetch p.children"; // with their children List<Parent> parents = session.createQuery(hql).list(); // now transform this list of parents into a Map Map<Long, List<Child>> result = new HashMap<Long, List<Child>>(parents.size()); for (Parent parent : parents) { result.put(parent.getId(), parent.getChildren()); }
Set<Integer> s = parents.asMap().keySet(); Iterator<Integer> it = s.iterator(); while (it.hasNext()) { int n = it.next(); try { List<Parent> list = parents.get(n); for (Parent l : list) { System.out.println(String.format( "id = %s, description = %s, home = %s", l.getId(), l.getDescription(), l.getHome())); // you can do more than printing to standard output } } catch (ExecutionException e) { LOG.error(e.getMessage(), e); // you can do whatever you want } }
public class Child extends Parent { private int id; private String name = "Child"; ... // getter and setters... } public class Parent { private int id; private String name = "Parent"; public Parent(Parent parent) { this.setId(parent.getId()); // option#1 this.setName(parent.getName()); // option#2 // this.name = parent.name; } ... // getter and setters... }
@Test public void testParent() { Parent p = new Parent(); HashMap<String, String> text = new HashMap<>(); text.put("en", "foo"); text.put("de", "bar"); p.setText(text); entityManager.persist(p); entityManager.flush(); Parent parent = entityManager.find(Parent.class, p.getId()); System.out.println("en: " + parent.getText().get("en")); System.out.println("de: " + parent.getText().get("de")); }
Map<String, String> idToColorMap = new HashMap<String, String>(); int colorIndex = 0; String colorPallete = ...; for (Parent parent : myParentList) { // if is not in idIdColorMap then I add it if (!idToColorMap.containsKey(parent.getParentId()) { idToColorMap.put(parent.getParentId(), colorPallete[colorIndex]); // increment colorIndex if (colorIndex + 1 == colorPallete.length) { colorIndex = 0; } else { colorIndex++; } } // Set Parent's Color parent.setColor(idToColorMap.get(parent.getId()); }
@RequestMapping(value = "/saveParent", method = RequestMethod.POST) public String saveParent(CommandName cn, BindingResult bindingResult) { Parent parent = cn.getParent(); if(parent.getId()!=null)//if parent id is not null, then retrieve the parent from database. and update it. parent = parentRepository.findOne(cn.getParent().getId()); for(Child c : cn.getNewChildren()){ c.setParent(parent); parent.getChildren().add(c); } parentRepository.save(parent); return "redirect:/view"; }
pref.clear(); MyDBHelper dbHelper = new MyDBHelper(this,"JobAroundU_DB", null, 1); SQLiteDatabase db=dbHelper.getWritableDatabase(); String sqlSavedPreferences="SELECT * FROM MyPreferences"; Cursor cursor = db.rawQuery(sqlSavedPreferences, null); while (cursor.moveToNext()){ Parent p= new Parent(); p.setPosizione(cursor.getString(1)); p.setAzienza(cursor.getString(2)); p.setId(cursor.getString(4)); Log.i("Parent ID", p.getId()); Child cp = new Child(); cp.setDescrizione(cursor.getString(3)); p.setChildren(new ArrayList<Child>()); p.getChildren().add(cp); pref.add(p); } db.close(); cursor.close(); // Refresh ExpandableListView data ((MyExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged();
em.getTransaction().begin(); Parent parent= new Parent(); parent.setName("Parent1"); em.persist(parent); // here parent id is valuated Set<Child> children=new HashSet<Child>(); Child child1=new Child(); child1.setName("Child1"); child1.setParentID(parent.getId()); children.add(child1); parent.setChildren(children); em.merge(parent); em.getTransaction().commit();
final Long parentId = doInTransaction(new TransactionCallable<Long>() { @Override public Long execute(Session session) { Parent parent = new Parent(); Child son = new Child("Bob"); Child daughter = new Child("Alice"); parent.addChild(son); parent.addChild(daughter); session.persist(parent); session.flush(); return parent.getId(); } });
test(){ Parent p1 = new Parent(); Child c1 = new Child(); entityManager.persist(p1); entityManager.persist(c1); p1.getChilds.add(c1); entityManager.merge(p); //!!:using another instance of entitymanager just to simulate the case of detached entities. Parent p2 =entityManager1.find(p1.getId(),Parent.class); child c2 =entityManager1.find(c1.getId(),Child.class); boolean contains=p1.getChilds().contains(c2); // problem1: contains==false //Then if we add c2 to the childs set we will have //a duplication inside the Set p2.getChilds.add(c2);//problem2:childs contains c1 and c2 boolean remove=p2.getChilds.remove(c2);//problem3:remove==false entityManager1.merge(p2);//problem4: hibernate will deal with c2 //like a new entity then an insert operation is //triggered on c2 (an exception=> violation of unique id) }
Parent parent = new Parent(); parent = parentDomainService.save(parent); parent.setChildren(Lists.newArrayList(new Child(), new Child())); for (Child child: parent.getChildren()) { child.setParentId(parent.getId()); } childDomainService.save(parent.getChildren());