jpa


Construct JPA query for a OneToMany relation


I've those 2 entities
Class A {
#OneToMany(mappedBy="a")
private List<B> bs;
}
Class B {
#ManyToOne
private A a;
private String name;
}
1) I would like to construct a query that says get all A's that have at least one B with name ="mohamede1945"
2) I would like to construct a query that says get all A's that don't have any B with name = "mohamede1945"
Could anyone help me?
You can use the ANY and ALL constructs to filter the subquery. So something like
1. FROM A aEntity WHERE 'mohamede1945' = ANY (SELECT bEntity.name FROM aEntity.bs bEntity)
2. FROM A aEntity WHERE 'mohamede1945' <> ALL (SELECT bEntity.name FROM aEntity.bs bEntity)
First of all, I think you can learn the answer by looking at this link and search for JOIN: http://download.oracle.com/docs/cd/E11035_01/kodo41/full/html/ejb3_langref.html
Second of all, here is my approach:
#Entity
#NamedQueries({
#NamedQuery(name="A.hasBName",query="SELECT a FROM A a JOIN a.b b WHERE b.name = :name"),
#NamedQuery(name="A.dontHasBName",query="SELECT a FROM A a JOIN a.b b WHERE b.name <> :name")
})
Class A { /* as you defined */ }
In you DAO, you can make the namedquery like this:
public List<A> findByHasBName( String name ){
Query q = em.createNamedQuery("A.hasBName")
.setParameter("name", name);
try{
return ( (List<A>) q.getResultList());
} catch ( IndexOutOfBoundsException e){
return null;
}
}

Related Links

Insert object with composite primary key
In JPA, having a many-to-one as primary key throws referential integrity constraint violation
JPA never fetcheable #OneToMany
JPA reading/referencing annotations programatically
JPQL “DISTINCT” returns only one result
Fuse ESB JPA Entity not persisting
Spring Data findBy Enum
PersistenceUnitInfo implementation in EclipseLink
Does Guice Persist provide transaction scoped or application managed EntityManager?
One to Many relationship in JPA
How to optionally persist secondary table in Eclipselink
OpenEJB + EclipseLink are not able to create tables on HSQL database
JPA entity field that is not mapping db column but is obtained by query
It is possible to override properties in a persistence.xml that is located in a jar dependency
Jboss 7.1.0 +EJB3 + JPA - Error JBAS014771
JPA using multiple database schemas at runtime dynamically

Categories

HOME
ggplot2
nunit
ns-3
youtube-data-api-v3
static
vsm
jax-ws
vert.x
modal-dialog
rcloud
accessibility
okhttp
endeca
endpoint
scrolltop
do-while
entitymanager
job-scheduling
intershop
dreamweaver
forgot-password
template-engine
sencha-touch
charles
spring-async
angular-routing
imageresizer
mount
expressionengine3
definitelytyped
syntax-highlighting
sqlpackage
userdefaults
complex-numbers
jett
installshield-le
sharding
numpy-broadcasting
engine.io
python-pptx
sim-toolkit
jpad
tex
projects-and-solutions
resolution
postgresql-8.4
softmax
outputcache
facebook-social-plugins
snapchat
opennms
jqchart
cmp
gameplay-kit
filenet
restore
autosar
iotivity
model-associations
lua-telegram-bot
angular-ui-typeahead
boost-serialization
ellucian-scribe
nupic
grass
asyncdisplaykit
libvlc
sapi
dllimport
forwarding
google-books
dock
finalbuilder
omnifaces
showdialog
boost-program-options
unix-socket
with-statement
ora-00904
websphere-esb
menubar
mysql-error-1044
jammer
isqlquery
script#
argouml
booksleeve
web-notifications
intersect
mkv
boost-msm
mapping-by-code
u2netdk
quotation-marks
instantiationexception
updatesourcetrigger
addchild
entitykey
appliance

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App