Using SetJoin in JPA2 - jpa-2.0

how can i code the sql below using JPA2 CriteriaBuilder?
select p from parent p, child c where c.parent_id=p.id and c.code='222' and c.type='SD';
i tried to use SetJoin but couldn't figure how to use.
thanks in advance

One alternative matching your query:
public void foo() {
// select p from parent p, child c where c.parent_id=p.id and c.code='222' and c.type='SD';
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery <Parent> query = cb.createQuery(Parent.class);
Root <Parent> r1 = query.from(Parent.class);
Root <Child> r2 = query.from(Child.class);
Predicate[] predicates = new Predicate[] {
cb.equal(r1.get(Parent_.id), r2.get(Child_.parentId)),
cb.equal(r2.get(Child_.code), "222"),
cb.equal(r2.get(Child_.type, "SD"))
};
query.select(r1);
query.where(predicates);
List <Parent> result = entityManager.createQuery(query).getResultList();
}
There are numerous ways to do what you want but as I don't know anything about your model it's hard to give a proper example other than the one above.
Provided that the Parent and Child are associated you could also do something like:
public void foo() {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery <Parent> query = cb.createQuery(Parent.class);
Root <Child> child = query.from(Child.class);
Join <Child, Parent> parent = child.join(Child_.parent);
Predicate[] predicates = new Predicate[] {
cb.equal(child.get(Child_.code), "222"),
cb.equal(child.get(Child_.type, "SD"))
};
query.select(parent);
query.where(predicates);
List <Parent> result = entityManager.createQuery(query).getResultList();
}
But that's based on a lot of assumptions.

Thanks #Magnus for detailed information, i ve solved my problem like below:
predicate = criteriaBuilder.and(predicate, criteriaBuilder.equal(root.join("children", JoinType.INNER).get("code"), location.getCode()));

Related

Criteria API in hibernate 5

I am migrating from Hibernate 3 to Hibernate 5 and have the following function
public static <T extends Saveable> T searchByNaturalKeys(T entity, Map<String, Object>
naturalKeysMap)
throws Exception{
// CriteriaQuery<?> criteria = null;
try {
Session session = getSession();
NaturalIdentifier naturalIdentifier = new NaturalIdentifier();
for (Entry<String, Object> entry : naturalKeysMap.entrySet()) {
naturalIdentifier.set(entry.getKey(), entry.getValue());
}
criteria = session.createCriteria(entity.getClass()).add(naturalIdentifier);
}
I tried to do the same thing for Hibernate 5 here is my approach
criteria = builder.createQuery(entity.getClass());
Root<?> root = criteria.from(entity.getClass());
criteria.where(builder.equals(naturalKeysMap));
List<?> categories = session.createQuery(criteri).getSingleResult();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<?> criteriaQuery = criteriaBuilder.createQuery(entity.getClass());
return (T) criteria.uniqueResult();
I know I am doing something wrong here. Can anybody give me right approach for this?
Another approach to do above thing is also welcomed

Convert SDT data to BC in Genexus

I populated a SDT with data in Genexus 15 and need to save it on a transaction. The SDT has a two level structure with header information and detail items.
I tried with this DataProvider but something is wrong, because I get an foreign key violation error when tring to execute it:
Rules:
parm(in: &NewInvoice); // SDT variable
Source:
DPInvoice
{
StampId = &NewInvoice.StampId
InvoiceNumber = &NewInvoice.InvoiceNumber
CustomerId = &NewInvoice.CustomerId
Concept
{
ProductId = &NewInvoice.Concept.CurrentItem.ProductId
ConceptQty = &NewInvoice.Concept.CurrentItem.ConceptQty
}
}
The Event triggering the DP in the webpanel:
&Invoice = DPInvoice(&NewInvoice)
&Invoice.Insert()
commit
This is the transaction structure
This is the SDT Structure
Please help me!
You have to iterate over the second level of &NewInvoice.
Define a variable &NewInvoiceItem of type SDTInvoiceCopy1.ConceptItem in the DataProvider and use the following Source code:
DPInvoice
{
StampId = &NewInvoice.StampId
InvoiceNumber = &NewInvoice.InvoiceNumber
CustomerId = &NewInvoice.CustomerId
Concept Input &NewInvoiceItem in &NewInvoice.Concept
{
ProductId = &NewInvoiceItem.ProductId
ConceptQty = &NewInvoiceItem.ConceptQty
}
}

C++ Pugixml get children of parent by attribute id

For example:
<levels>
<level id="1">
<somestuff></somestuff>
</level>
<level id="2">
<somestuff></somestuff>
</level>
</levels>
How do you get the data of level with id 1?
Now i am using pugi::xml_node level = levels.child("level") But that return all levels..
Regards,
GJJ
levels.find_child_by_attribute("level", "id", "1")
Try it:
for (pugi::xml_node ambil = doc.child("levels").child("level"); ambil; ambil = ambil.next_sibling("level"))
{
int id = ambil.attribute("id").as_int();
CCLog("%d",id);
}
foreach children & compare attribute value.
e.g.
for (const auto& node : levels.children("level"))
{
if (node.attribute("id").as_int() == 1)
{
// TODO: add ur code here
}
}

Xpath How remove child node by attribute c++ libxml2

How do I remove a child with a specific attribute? I´m using c++/libxml2. My attempt so far (in the example I want to remove child node with id="2"):
Given XML:
<p>
<parent> <--- current context
<child id="1" />
<child id="2" />
<child id="3" />
</parent>
</p>
xmlNodePtr p = (parent node)// Parent node, in my example "current context"
xmlChar* attribute = (xmlChar*)"id";
xmlChar* attribute_value = (xmlChar*)"2";
xmlChar* xml_str;
for(p=p->children; p!=NULL; p=p->next){
xml_str = xmlGetProp(p, attribute);
if(xml_str == attribute_value){
// Remove this node
}
}
xmlFree(xml_str);
Call xmlUnlinkNode to remove a node. Call xmlFreeNode to free it afterward, if you want:
for (p = p->children; p; ) {
// Use xmlStrEqual instead of operator== to avoid comparing literal addresses
if (xmlStrEqual(xml_str, attribute_value)) {
xmlNodePtr node = p;
p = p->next;
xmlUnlinkNode(node);
xmlFreeNode(node);
} else {
p = p->next;
}
}
Haven't used this library in a while, but check out this method. Note that per the description, you need to call xmlUnlinkNode first.

How to count elements in TinyXml?

I think problem is with wrong using function or something else.
This part of code is working but the result isn't well.
TiXmlElement* e = hDoc.FirstChildElement().Element(); // think problem is there
while (e)
{
e = e->NextSiblingElement(); //or may be there
count++;
}
The result of count is 1.
Xml file is:
<doc>
<state> ... </state>
<state> ... </state>
...
</doc>
Can't find work example.
if you read the documentation you can find the following example (which seems neater than your approach):
for( child = parent->FirstChild(); child; child = child->NextSibling() )
count++;
But you are probably only trying to count the states so I would suggest:
for( child = parent->FirstChild("state"); child; child = child->NextSibling("state") )
You probably also want something like this:
TiXmlElement *parent = hDoc.RootElement();