Adding post and then fetching doesn't work in spring

GitHub
notecz-backend/src/test/java/lol/koblizek/notecz/api/user/UserContr...
Contribute to KoblizekXD/notecz-backend development by creating an account on GitHub.
No description
38 Replies
JavaBot
JavaBot4mo ago
void testGetUserPosts() throws Exception {
void testGetUserPosts() throws Exception {
This post has been reserved for your question.
Hey @Koblížkáč! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st4mo ago
Did you try debugging the controller?
JavaBot
JavaBot4mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Koblížkáč
KoblížkáčOP4mo ago
yeah it will get saved) but findUserById returns user with 0 sized posts for some reason
dan1st
dan1st4mo ago
Can you try findAll()?
Koblížkáč
KoblížkáčOP4mo ago
where exactly?
dan1st
dan1st4mo ago
Can you show your repository?
JavaBot
JavaBot4mo ago
public interface PostRepository extends JpaRepository<Post, Long> {
public interface PostRepository extends JpaRepository<Post, Long> {
dan1st
dan1st4mo ago
that should contain a findAll() method Can you try calling thatIn your /posts endpoint, can you call that and print the result?
Koblížkáč
KoblížkáčOP4mo ago
yeah it extends JpaRepository, should i just run it on debugger? where though oh
dan1st
dan1st4mo ago
yeah or using a debugger works as well
Koblížkáč
KoblížkáčOP4mo ago
right
Koblížkáč
KoblížkáčOP4mo ago
i get
No description
Koblížkáč
KoblížkáčOP4mo ago
like this
No description
Koblížkáč
KoblížkáčOP4mo ago
i was wondering if maybe it could be by the configuration of the posts column?
No description
dan1st
dan1st4mo ago
is user the correct user? How are you inserting it? Can you show the other side of the 1:n relation?
Koblížkáč
KoblížkáčOP4mo ago
like this
No description
Koblížkáč
KoblížkáčOP4mo ago
post side of the relation is:
No description
dan1st
dan1st4mo ago
in here, is user the user with ID 1?
Koblížkáč
KoblížkáčOP4mo ago
yeah
No description
No description
Koblížkáč
KoblížkáčOP4mo ago
you can see posts.size = 0 for some reason yeah ithink why the order of saving is wrong inside addpost any idea how to do it properly?
dan1st
dan1st4mo ago
try that:
post.setUser(user);
user.getPosts().add(post);
postRepository.save(post);
userRepository.save(user);
post.setUser(user);
user.getPosts().add(post);
postRepository.save(post);
userRepository.save(user);
maybe switch the last two lines
Koblížkáč
KoblížkáčOP4mo ago
nope smh still empty array
dan1st
dan1st4mo ago
Does both happen in the same thread?
Koblížkáč
KoblížkáčOP4mo ago
well im not doing any parallel stuff, soo how can i check?
dan1st
dan1st4mo ago
actually I think this shouldn't matter since you get a symptom of it being changed ohhhhhh
Koblížkáč
KoblížkáčOP4mo ago
i mean it should work
dan1st
dan1st4mo ago
lol
Koblížkáč
KoblížkáčOP4mo ago
hm?
dan1st
dan1st4mo ago
try using a List instead of a Set or make sure Post#hashCode and Post#equals don't include the ID or do user.setPosts(new HashSet<>(user.getPosts())); before saving the user
Koblížkáč
KoblížkáčOP4mo ago
okay
dan1st
dan1st4mo ago
but if you want to avoid difficulties, don't persist Sets with JPA
Koblížkáč
KoblížkáčOP4mo ago
okay
dan1st
dan1st4mo ago
Does that work?
Koblížkáč
KoblížkáčOP4mo ago
uno momento nope im clueless at this point lol
dan1st
dan1st4mo ago
Can you show your code?
JavaBot
JavaBot4mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server