Filtering collection on condition nested in another collection - lambdaj

Having this bean structure
class User {
private List<Permission> permissions;
...
}
class Permission {
private Detail detail;
...
}
class Detail {
private String name;
...
}
How can I filter list of users to contain only users with at least one permission with Permission.Detail.name containing string "abc"?

Ok, I found it
select(
values,
having(
on(User.class).gePpermissions(),
hasItem(
having(
on(Permission.class).getDetail().getName(),
containsString("abc")
)
)
)
);

Related

Getting Relationship properties using spring data neo4j node repository

If we have a Company Node like this which is linked to another Node (Location) using OWNS relationship.
#NodeEntity ("Company")
public class Company {
#Id
#Property
private String companyId;
#Property
private String partyId;
#Relationship (type = "OWNS", direction = Relationship . OUTGOING )
private Set<Location> ownedLocations;
}
OWNS relationship
#RelationshipEntity (type = "OWNS")
public class Owns {
#Property
private String ownsProperty;
#StartNode
private Company company;
#EndNode
private Location location;
}
Here is the Repository
#Repository
public interface CompanyRepository extends Neo4jRepository<Company, String> {
#Query ("match (c:Company)-[x:OWNS]-(y) where c.partyId = $0 return c,x,y")
Stream<Company> getCompaniesWithRelationsByPartyId(final String partyId);
}
So when i run this query i am getting only Company and Location entities but no OWNS ownsProperty. How to get relationship properties using above repository?
NOTE: I don't want to use #QueryResult
enter code here
You have to define the Owns relationship entity as the type for the relationship in the Company like this
#Relationship (type = "OWNS", direction = Relationship . OUTGOING )
private Set<Owns> ownedLocations;

Unable to make general purpose query, without Domain entity

I am new to springbootneo4j. I have difficulties making general purpose queries. I want to be able to make any kind of query and get result without domain entity.
I am making a query like this in repository class:
#Query("MATCH (p:Employee) RETURN ID(p) as id, p.name as name, p.salary as salary ")
that is not working, but the following query is working:
#Query("MATCH (p:Employee) RETURN p ")
My domain entity class is something like this:
#NodeEntity
public class Employee {
#Id
#GeneratedValue
private Long id;
private String name;
private int salary;
#Relationship(type = "IS_BOSSOF", direction = Relationship.UNDIRECTED) Set<Employee> reporties = new HashSet<>();
public Employee() {}
// some more code
}
Create a command is like this:
(laksmi:Employee{name:"Laksmi",salary:200}),(ashwini:Employee{name:"AshwiniV",salary:300}), (harish:Employee{name:"Harish",salary:400}), (jay)-[:IS_BOSSOF]->(mukesh), (xyz)-[:IS_BOSSOF]->(mukesh), (harish)-[:IS_BOSSOF]->(ashwini),
Whenever you are distributing properties you need to use #QueryResult annotation on your class
SDN

How to retrieving a list object in the entity?

I have an entity that has a List object. I can see the object that currently contain one element. However, when I tried to retrieve the list, I always get null.
There are three classes: two entity classes and an endpoint class
public class Buddy{
...
#Persistent (mappedBy="owner")
#Order (extensions = #Extension(vendorName="datanucleus",key="list-ordering", value="sharedOn desc"))
#Element(dependent = "true")
#JsonManagedReference
private List<Sticky> listOfObjects;
...
public List<Sticky> getStickies(){
return listOfObjects;
}
}
public Class Sticky{
...
#Persistent (dependent="true")
#JsonBackReference
private Buddy owner;
...
public Buddy getBuddy(){
return this.owner;
}
...
}
public class StickyEndpoint{
...
#ApiMethod(name = "createSticky")
public Sticky createSticky(Sticky sticky){
Buddy buddy = sticky.getBuddy(); // confirmed that buddy returned is not null
List<Sticky> sticky = buddy.getStickies(); //sticky always return null
...
return sticky;
}
....
}
Can someone help? Thanks!
JDO will not let u insert custom type, so List will not run. I ended up installing Objectify....

spring-data-neo4j in "Advanced Mapping" mode - How can I read an entity-value without starting a transaction manually?

In my JavaEE7-project, I am using spring-data-neo4j standalone in "Advanced Mapping" mode (using spring-aspects). Everything works fine so far: CRUD on entities within a transaction, where the transaction is started manually or via #Transactional-annotation.
In my usecase, my view accesses an entity "directly":
// User
#NodeEntity
public class User {
private String firstName;
// getter, setter, ...
}
// SessionBean
#SessionScoped
#Named
public class SessionBean {
#Transactional
public User getUser() {
User user = ...;
System.out.println(user.getFirstName()); // (1) gives firstName-value.
return user;
}
}
// sometpl.xhtml
${sessionBean.user.firstName} // (2) gives "null".
Somehow, this behavior (difference between (1) and (2)) is wanted, as spring-data-neo4j supposes read-access only within a transaction.
But I want to have my usecase(2) working (returning the user's firstName, not "null"). Is there any way to achieve this? So let's say, starting transaction automatically in read-access-case? Implicit read-transactions-support?
My workaround:
Use a RequestScoped bean to start a transaction in "preRenderView" and to close this tx when the bean is destroyed.
This does not work on ajax-calls!
#Named
#RequestScoped
public class SpringDataNeo4jHelperBean {
#Inject
#Named
private Neo4jTemplate neoTemplate;
private Transaction tx;
#PreDestroy
public void finishTransaction() {
if (this.tx != null) {
this.tx.success();
this.tx.finish();
}
}
public void startReadOnlyTransaction() {
if (!this.neoTemplate.getGraphDatabase().transactionIsRunning()) {
this.tx = this.neoTemplate.getGraphDatabaseService().beginTx();
}
}
}
In some template, for example s.th. like a central layout.xhtml:
<f:metadata>
<f:event type="preRenderView" listener="#{springDataNeo4jHelperBean.startReadOnlyTransaction()}" />
</f:metadata>

how to create query in Criteria api

I have a class:
#Entity
public class Resume {
private Long id;
#Embedded
private DesiredPositionAndSalary desiredPositionAndSalary;
}
and class:
#Embeddable
public class DesiredPositionAndSalary {
#ManyToMany
private Set<Specialization> specializations;
}
and class ;)
#Entity
public class Specialization {
private Long id;
}
now i have some Specializations that i need filtered by.
For example i need to select all resume with one of specialization like programmer or manager. Something like
select * from resume r inner join resume_to_specialization rts on r.id = rts.id inner join specialization s on rts.spec_id in(1,2)
how can i write this query in Criteria api? If i miss some major details i can give more.
Ok, i handle it with this:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Resume> cq =cb.createQuery(Resume.class);
Root<Resume> root = cq.from(Resume.class);
cq.select(root);
Set<Specialization> filter = getFilter();
SetJoin<DesiredPositionAndSalary, Specialization> join = root.join(Resume_.desiredPositionAndSalary, JoinType.INNER).join(
DesiredPositionAndSalary_.specializations, JoinType.INNER);
cq.where(cb.and(cq.getRestriction(), join.in(filter)));
cq.distinct(true);/*it is major, or we get duplicate of resume for every
specialization overlap with filter*/
List<Resume> result = em.createQuery(cq).getResultList();
.