jpa


ManyToOne OneToMany JPA mapping not working right


It seems I have a problem with OneToMany, ManyToOne mapping.
I'm using a CrudRepository named "ur" here:
ur.save(new User("Bx", "B", "O", "email#email.com", "Bax")); //userRepo save
User u1 = ur.findOneByUserName("Bx");
MyToken t1 = new MyToken("X5");
u1.addToken(t1);
ur.save(u1);
MyToken t2 = TokenRepo.findOneByToken("X5"); // a different crudRepository
String foundUser = t2.getUser().getUserName(); // THIS "user" is null.
relevant sections of User.java (extends AbstractPersistable<Long>):
#OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL}, mappedBy = "user")
private Set<Role> roles = new HashSet<Role>(1);
#OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL}, mappedBy = "user")
private Set<MyToken> token = new HashSet<MyToken>();
MyToken.java (extends AbstractPersistable<Long>):
#ManyToOne(fetch = FetchType.EAGER, cascade={CascadeType.ALL})
#JoinColumn(name="user") // commenting this out or not does nothing...
private User user;
My debugger says 'user' is 'null' at line "String foundUser" even though that should be completely false according to the code.
As you can see, all is "eager" so I don't see why MyToken.setUser() is not automatically done. How are they not linked already? AnyInitiatedToken.getUser() should not be null if you already did User.addToken, and UserRepo.save().
NOTE: I have also tried .LAZY for the MyToken.java and Role.java class (but still doesn't work).
Since you did not post the setters, I gonna assume they look like default setter.
User.token is set, but it has a mappedby, so it is really irrelevant for what is stored in the DB. Token.user matters, but that is still an NULL so that's what get's saved and retrieved.
You have two options:
Change User.setToken() to update Token.user of the passed in Token (and of the one that was previously set.
Whenever you call User.setToken() also update Token.users to make both directions of the relationship match.
I think it's not related to the fetch strategy whether it's eager or lazy fetch type. I think you gave column alias for the user's id and missed that the name atributte's value of the #JoinColumn should reference that primary key column alias.
Like in the User you have:
#Id
#Column(name = "userid", unique = true, nullable = false)
public String getUserId() {
return this.userId;
}
then in the Token should be
#ManyToOne(fetch = FetchType.EAGER, cascade={CascadeType.ALL})
#JoinColumn(name="userid") // commenting this out or not does nothing...
private User user;

Related Links

JPA Criteria API Join
JPQL, OR only returning result of one condition?
Named query to work out average
when an entity stops to be managed in JPA
Fetch a collection with a lazy relationship in JPA
Does orphanRemoval = true imply cascade delete
Google Guice persistence + Spring Data JPA
Having errors using MAX in JPA
Default name for database objects
Is there a way to transform objects that spring data repositories return?
EclipseLink-6015 Invalid query key [customer] in expression
Is it possible to drop-create some table with JPA
EcllipseLink: JPA Entities with same name. Error: Entity name must be unique in a persistence unit
How to use prepared statement in JPA
How to compare an uppercase query with JPA and the QueryBuilder
How to change EclipseLink in GlashFish 4.0?

Categories

HOME
sonarqube
notepad++
kentico
adal
wakanda
webdriver
jax-rs
rsa
hessian
static-site
vxml
vsm
banner
jtextpane
barcode
little-proxy
github-enterprise
azure-iot-hub
powershell-v5.0
chargify
google-data-studio
asp.net-core-webapi
selinux
user-experience
mongoid6
object-storage
roundup
draft-js-plugins
google-cloud-print
fat
jasmine-node
8051
subclass
cppreference
eviews
scatter3d
dst
complex-numbers
zend-debugger
cognos-bi
linkedhashmap
arduino-ide
processwire
video-capture
sql-server-ce
xcode7.3
graphhopper
cd
jvmti
signals-slots
setup.py
dcmtk
android-checkbox
berkeley-db-je
opencmis
reportservice2010
payload
universal-analytics
vcloud-director-rest-api
winusb
preprocessor
model-associations
dup
simevents
swizzling
http-status-code-401
lua-telegram-bot
nss
php-socket
invoke-sqlcmd
inf
wiredep
poppler
rpart
pyobjc
xmltype
bcache
procedural-programming
apache-spark-1.3
entry-point
template-deduction
ss7
amslidemenu
breakout
hidapi
kaleidoscope
jquery-1.9
argouml
robotlegs
vfs
listitem
box2d-iphone
luabind
node-redis
procedures
entity-attribute-value
xcelsius
tournament
undefined-index
folding
google-wave
software-lifecycle
entitykey

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