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

Is there a way to get all managed entities from an EntityManager
Multi-Tenant Seam + JPA Application
JPA mapping error for sequence number
JPA : What is the behaviour of merge with lazy initialized collection?
JPA problem one-to-one association cascade= PERSIST
JPQL we can't CONCAT(String, Integer) EclipseLink?
Cant persist an entity
How to implement self join in JPA?
Unneeded column added trying to set up OneToMany relationship
EntityManager doesn't refresh the data after querying
How To modify Eclipselink JPA 2.0 connection retry behavior
How to have 2 collections of the same type in JPA? (EclipseLink)
How to inject persistence context to different data source programmatically
Join entity with composite key
Entity bean 3.0 composite key issue
DB2 Character Datatype and JPA Not Working

Categories

HOME
shell
jpa
paraview
gdb
merge
css3
case
interface
battery
serialization
parameters
telegram-bot
zip
settings
trac
categorical-data
minecraft-forge
github-enterprise
windows-ce
tput
comsol
shared-libraries
google-content-api
identity-management
simpleitk
microsoft-ocr
short-url
draft-js-plugins
data.stackexchange.com
pic32
custom-post-type
entity-relationship-model
iis-express
boost-asio
database-comparison
keras-layer
jwplayer6
google-knowledge-graph
urlsession
apriori
postgresql-8.4
oracle-service-bus
tcc
oim
httr
dynamic-linq
openresty
textsum
openal
corruption
d3-force-directed
trigger.io
redistributable
uicolor
avspeechsynthesizer
lean
phpwebsocket
uiactionsheet
invoke-sqlcmd
boomerang
uncompress
liquid-layout
dnssec
node-serialport
fail2ban
diff3
user-permissions
cakephp-2.7
qtwebengine
document-repository
execve
filesplitting
appserver
linkedin-jsapi
malformedurlexception
wxformbuilder
authlogic
getopt
mongohq
chart-director
curb
qtconcurrent
jquery-address
flex-mobile
procedures
exponentiation
uipagecontrol
xui
quotation-marks
dataform
tmx
metaweblog
easyb
openfaces
build-environment
cuda.net
windows-identity
int64
xslcompiledtransform
great-circle

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App