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

Why I get an invalid schema error in a servlet, with same code in main function not?
JPA Entities as module in wildfly shared across webapp and EJB deployments
JPA specification for NoSQL databases
beginner having trouble making a persistent javadb database in java ee 7 and glassfish 4
Unable to lookup JNDI name
JPA:find() vs query
JPA: Query on point of exception thrown?
WebSphere OpenJPA performance issue
Searching user by its firstname and/or lastname in any order
Is it possible to make JPA throw an exception on a nontransactional write?
JPA relationship and Inheritance configuration
JPA: EntityManager delete/update
Is it possible to have a JPA entity that doesn't have a table itself, but is an aggregate of other entities?
Integrate BoneCP and EclipseLink with LOCAL_RESOURCE transaction-type
Need help in creating CriteriaQuery
How to map ALL names directly by JPA?

Categories

HOME
flex
mysql-workbench
numbers
vscode-extensions
entity-framework-core
scripting
jquery-ui
recordrtc
bing-maps
project
openstreetmap
activecollab
line
jxl
frequency-distribution
atlassian
selectize.js
red5
wolframalpha
imessage
sencha-touch
breadth-first-search
joptionpane
event-store
expressionengine3
fable-f#
bulletphysics
underscore.js
memory-address
jett
android-gridview
pdw-file-browser
slacktextviewcontroller
roblox
latitude-longitude
salesforce-communities
openalpr
vmware-fusion
naming
linqpad
subclipse
puredata
double-click
magic-draw
custom-font
tinkerpop
adafruit
resourcemanager
windows-azure-queues
model-associations
swizzling
deep
kiosk
httpruntime.cache
qbxml
blitline
spring-retry
directdraw
roadkill-wiki
.bash-profile
aspectj-maven-plugin
infosphere-spl
ejb-2.x
addressing-mode
sonarqube-5.0
contrast
webfinger
insight.database
buffering
android-audiorecord
windows-embedded-standard
pagedown
uimanageddocument
database-create
flex-mobile
role
http-daemon
office-2007
jqueryform
wordprocessingml
cuda.net
localizable.strings
dsoframer
change-management

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